A hands on workshop for an agrotech hackathon 🌽
In the previous step you assembled the hardware, and set up the Pi. In this step, you will create the app in Azure IoT Central.
Azure IoT Central is an IoT Software-as-a-service (SaaS) platform. This allows you to define an service that can interact with IoT devices, sending data both ways from device to cloud, and cloud to device. You can define the capabilities of each device, and create dashboards to show data.
To create your app, you can either create it manually from scratch to learn about how to build Azure IoT Central apps, or create it from a pre-defined template if you want to save time and already know how to use Azure IoT Central.
Follow this link to create a new Azure IoT Central app pre-configured for this workshop:
apps.azureiotcentral.com/build/new/c136de48-e7a1-4f3d-83d1-05a8cbfa63ed
Sign in with your Microsoft account if needed
Give your application a name that makes sense to you, such as Agro Hack
Provide a unique URL for your app. This needs to be globally unique, so include things such as the date or your name. For example agrohackjim2020
.
Select a pricing plan. This hack only uses 2 devices, so is free for all tiers, so select the one that makes sense for you. The Free tier is ideal as long as you don’t want your code to run longer than 7 days.
For the free tier you will need to provide a name and phone number, that will be verified. For the other tiers you will need an Azure subscription. You can find details on different free subscriptions here.
Select Create
Your application will be provisioned, and you will see the dashboard once it is ready.
Head to Create a device to configure a device inside the Azure IoT Central app.
Head to apps.azureiotcentral.com
From the menu on the side, select Build
Select Custom Apps
Sign in with your Microsoft account if needed
Fill in your application details:
Agro Hack
agrohackjim2020
.Custom Template
Select a pricing plan. This hack only uses 2 devices, so is free for all tiers, so select the one that makes sense for you. The Free tier is ideal as long as you don’t want your code to run longer than 7 days.
For the free tier you will need to provide a name and phone number, that will be verified. For the other tiers you will need an Azure subscription. You can find details on different free subscriptions here.
Your application will be provisioned, and you will see the dashboard once it is ready.
Azure IoT Central can work with multiple types of device, and multiple devices per device type. Device types are defined using templates - these specify the capabilities of the device including the telemetry that can be received from the device, and commands that can be sent to it.
The environment sensor captures temperature, humidity, air pressure and soil moisture. You will need to define a template that has these values on it, so they can be received from the Pi. It can also receive a message indicating that the plant needs watering and use this to turn on an LED, so the template will need to support this via a command.
From the left-hand menu, select Device Templates
Select the + New button from the top menu
Select the IoT Device template
Select the Next: Customize button
Select the Next: Review button
Select the Create button
Name the template Environment Sensor
Once the template is created, you need to add capabilities to it. These are defined using capability models, which define the capabilities of all devices that will use this template. Capability models are made up of three parts:
Interfaces - these are reusable collections of capabilities, and are grouped into three categories:
Telemetry - actual values detected and sent by the device, for example in a thermostat it could be the current detected temperature
Properties - settings on the device, for example in a thermostat it could be the desired temperature. These can be set by the device, or via Azure IoT Central and synced to the device.
Commands - calls that can be made on the device, optionally passing data. For example in a thermostat it could be called by a mobile app to send a request to change the desired temperature.
Cloud properties - these are properties set in Azure IoT Central against a device, but not synced to the device. For example a device could have a cloud property for the account name of the owner, the devices location, or the date it was last serviced.
Views - these are dashboards for a device that can contain charts, data values and other information allowing you to visualize telemetry or send commands.
The environment sensor needs a capability model created, with an interface defined for the telemetry values being sent, a command to indicate that the plant needs watering, and a view to visualize these values.
Select the Custom capability model
Add a new interface to the capability model by selecting the top level Environment sensor item in the menu and selecting +Add interface
Select Custom
This interface needs 4 telemetry values added to it for the temperature, pressure, humidity and soil moisture. Telemetry values have the following properties:
Select the + Add capability button to add new capabilities, and add the following four values:
Display Name | Name | Capability Type | Semantic Type | Schema | Unit |
---|---|---|---|---|---|
Temperature | temperature | Telemetry | Temperature | Double | °C |
Pressure | pressure | Telemetry | Pressure | Double | kPa |
Humidity | humidity | Telemetry | Humidity | Double | % |
Soil Moisture | soil_moisture | Telemetry | None | Double | None |
The interface also needs a command that can be triggered to indicate that the plant needs watering.
Select the + Add capability button to add one more new capability, and add the following value:
Display Name | Name | Capability Type |
---|---|---|
Needs Watering | needs_watering | Command |
Turn on Request for the command and set the following values:
Display Name | Name | Schema |
---|---|---|
Needs Watering | needs_watering | Boolean |
Select the Save button from the top menu
A view is used to visualize this telemetry, and this needs to be added to the capability model.
Select Views from the menu
Select Visualizing the device
Set the view name to Environment Monitoring
Drag Temperature from the Telemetry section onto the dashboard. This will add a graph showing the temperature recorded over the past 30 minutes to the dashboard. You can configure this in multiple ways using the control buttons on the top of the panel:
Change the visualization to be a number of different chart types, or the last known value
Change the size of the panel on the dashboard
Configure the chart including legend, axis labels and the time range
Configure the chart or last know value to your liking.
Repeat this for the other telemetry values. If you want to plot multiple telemetry values on the same chart use the check boxes to select multiple values and drag them together. You can add telemetry values multiple times to get multiple views over the data.
Drag Needs Watering from the Command section onto the dashboard.
You can also customize the view with labels, markdown or images if desired.
Select the Save button from the top menu
When done you will have a view that will look something like this, depending on how you designed it:
Before the device template can be assigned to a device, it needs to be published. Once published, any interfaces defined on it cannot be changed, instead a new version of the device template needs to be created.
Select the Publish button from the top-most menu
Select Publish
Device templates can be assigned to one or more devices - a device being an actual physical device or a software simulator. The Raspberry Pi that was set up in the last step will be added as a device and configured to use the new device template.
Select Devices from the left-hand menu
Select the Environment Sensor device template in the template list
Select + New
Set the Device Id to raspberry_pi
, the Device Name to Raspberry Pi
. Then select Create
The new device will appear in the devices list.
Each device has a set of connection details that will be used on the actual device to connect to Azure IoT Central and send telemetry.
Select the Raspberry Pi
device in the devices list. This will show the view created earlier with all the telemetry boxes marked as Waiting for data.
Select Connect
Take a note of the ID Scope, Device Id and Primary key as you will need these values in the next step
In this step, you created the app in Azure IoT Central. In the next step you will write the code to capture telemetry from the Raspberry Pi.