How to build a healthier working environment by monitoring CO2, temperature, and humidity levels with an Azure Sphere, an SDC30 sensor, and Azure IoT Central.
This is part of the #JulyOT IoT Tech Community series, a collection of blog posts, hands-on-labs, and videos designed to demonstrate and teach developers how to build projects with Azure Internet of Things (IoT) services. Please also follow #JulyOT on Twitter.
Source code: Track CO2 levels in your workspace to improve health, wellbeing, and productivity.
Learning resources: Azure Sphere Developer Learning Path.
Working from home it is easy to close the door to shut out the noise of everyday life while we get on with work. Closing the door can lead to a build-up of CO2 gas, a by-product of our breathing, which can impact our wellbeing, concentration, and productivity levels.
Check out "Indoor carbon dioxide levels could be a health hazard, scientists warn".
The problem is we cannot see or smell Carbon Dioxide, it just keeps building up, and we have no way of knowing it is happening other than getting tired or a headache. So, with that in mind, I figured it was the Internet of Things to the rescue!
I wanted to build a secure IoT device with Azure Sphere using the Seeed Studio Grove CO2 & Temperature & Humidity Sensor I had in my box of bits. The folks at Sensirion made it super easy to port their SCD30 driver to Azure Sphere. It was just a matter of implementing the I2C init/read/write functions, a microsecond sleep function, plus setting up CMake build. It all just worked. The ported driver is included in this project.
Azure IoT Central provides an easy way to connect, monitor, and manage your Internet of Things (IoT) assets at scale.
I created a free trial of Azure IoT Central and in no time I had CO2, temperature, and humidity telemetry displayed (yes, the data is real, so we have made some changes at home!). By the way, you can continue to connect two devices for free to IoT Central after the trial period expires.
Azure IoT Central is also extensible using rules and workflows. For more information, review Use workflows to integrate your Azure IoT Central application with other cloud services
The solution supports two configurations.
Attach the Azure Sphere to the Grove Breakout board and plug in the following peripherals.
IMPORTANT. Be sure to read up on calibrating the Grove - CO2 & Temperature & Humidity Sensor (SCD30) Calibration sensor. This solution will enable auto-calibration.
Clone the Azure Sphere CO2 Monitor solution to your desktop.
git clone https://github.com/gloveboxes/Track-CO2-levels-in-your-workspace-to-improve-health-wellbeing-and-productivity.git azure-sphere-co2-monitor
Follow the Azure Sphere Developer Learning Path Lab Set Up to set up the developer tools, claim you Azure Sphere, and configure the device WiFi, and enable developer mode.
Your Azure Sphere device can securely connect and communicate with cloud services. Azure Sphere includes built-in library support for both Azure IoT Hub and Azure IoT Central. This lab focuses on Azure IoT Central. Take a moment to read Your IoT journey: simplified and secure with Azure IoT Central and Azure Sphere.
Azure IoT Central provides an easy way to connect, monitor, and manage your Internet of Things (IoT) assets at scale.
So the lab instructions are still visible, right mouse click, and open this link "Azure IoT Central" in a new window.
Click Build a solution.
Next, you will need to sign with your Microsoft Personal, or Work, or School account. If you do not have a Microsoft account, then you can create one for free using the Create one! link.
Expand the sidebar menu by clicking on the Burger menu icon.
Click + New application to create a new Azure IoT Central application.
Select Custom app
Specify the Application name, the URL, select the Free pricing plan, and complete the registration form.
Then click Create.
A device template is a blueprint that defines the characteristics and behaviors of a type of device that connects to an Azure IoT Central application.
For more information on device templates, review the Define a new IoT device type in your Azure IoT Central application article.
Click Device templates, then + New.
Click the IoT device template type.
Create an IoT Device Template.
Click Publish to publish the template. Publishing the template makes it available for devices.
Next, confirm and click Publish
See Define a new IoT device type in your Azure IoT Central application for information on creating your own device templates.
You need to set up a trust relationship between your Azure Sphere tenant and your IoT Central application.
Devices claimed by your Azure Sphere tenant will be automatically enrolled when it first connects to your IoT Central application.
Download the Certificate Authority (CA) certificate for your Azure Sphere tenant:
azsphere tenant download-CA-certificate --output CAcertificate.cer
The output file must have the .cer extension.
In Azure IoT Central, go to Administration > Device Connection > Manage primary certificate.
Click the folder icon next to the Primary box and navigate to the directory where you downloaded the certificate. If you don't see the .cer file in the list, make sure that the view filter is set to All files (*). Select the certificate and then click the gear icon next to the Primary box.
The Primary Certificate dialog box appears. The Subject and Thumbprint fields contain information about the current Azure Sphere tenant and primary root certificate.
Click the Refresh icon to the right of the Verification Code box to generate a verification code. Copy the verification code to the clipboard.
Return to the Azure Sphere Developer Command Prompt.
Download a validation certificate that proves that you own the tenant CA certificate. Replace code in the command with the verification code from the previous step.
azsphere tenant download-validation-certificate --output ValidationCertification.cer --verificationcode <code>
When prompted, navigate to the validation certificate that you downloaded in the previous step and select it. When the verification process is complete, the Primary Certificate dialog box displays the Verified message. Click Close to dismiss the box.
After you complete these steps, any device that is claimed into your Azure Sphere tenant will automatically be enrolled in your Azure IoT Central application when it first connects.
Remember, applications on Azure Sphere are locked down by default, including hardware and network endpoints. You must explicitly allow connections to the network endpoints of your Azure IoT Central application otherwise your Azure Sphere application will not be able to connect.
Follow these steps:
Navigate to the Samples -> AzureIoT -> Tools directory. You cloned the Azure Sphere samples repository in the first lab.
chmod +x ShowIoTCentralConfig
to add execution permissions for the tool.When you run the ShowIoTCentralConfig tool, you will be prompted for input data. The following table outlines what information you will be prompted for and where to obtain the required data.
Input data | From |
---|---|
Are you using a legacy (2018) IoT Central application (Y/N) | Respond N |
IoT Central App URL | This can be found in your browser address bar. For Example https://myiotcentralapp.azureiotcentral.com/ |
API token | This can be generated from your IoT Central application. In the Azure IoT Central application select Administration, select API Tokens, select Generate Token, provide a name for the token (for example, "AzureSphereSample"), select Administrator as the role, and click Generate. Copy the token to the clipboard. The token starts with SharedAccessSignature. |
ID Scope | In the Azure IoT Central application, select Administration > Device Connection and then copy the ID Scope |
Run the ShowIoTCentralConfig tool. Now follow the prompts that the tool provides, and copy the information from the output into the app_manifest.json file in Visual Studio.
Note: Your organization might require consent for the ShowIoTCentralConfig tool to access your Azure IoT Central data in the same way that the Azure API requires such consent. In some organizations, enterprise application permissions must be granted by an IT administrator.
Review the output from the ShowIoTCentralConfig tool. It will look similar to the following text.
Find and modify the CmdArgs, AllowedConnections and DeviceAuthentication lines in your app_manifest.json so each includes the content from the below:
"CmdArgs": [ "0ne000BDC00" ],
"Capabilities": {
"AllowedConnections": [ "global.azure-devices-provisioning.net", "iotc-9999bc-3305-99ba-885e-6573fc4cf701.azure-devices.net", "iotc-789999fa-8306-4994-b70a-399c46501044.azure-devices.net", "iotc-7a099966-a8c1-4f33-b803-bf29998713787.azure-devices.net", "iotc-97299997-05ab-4988-8142-e299995acdb7.azure-devices.net", "iotc-d099995-7fec-460c-b717-e99999bf4551.azure-devices.net", "iotc-789999dd-3bf5-49d7-9e12-f6999991df8c.azure-devices.net", "iotc-29999917-7344-49e4-9344-5e0cc9999d9b.azure-devices.net", "iotc-99999e59-df2a-41d8-bacd-ebb9999143ab.azure-devices.net", "iotc-c0a9999b-d256-4aaf-aa06-e90e999902b3.azure-devices.net", "iotc-f9199991-ceb1-4f38-9f1c-13199992570e.azure-devices.net" ],
"DeviceAuthentication": "--- YOUR AZURE SPHERE TENANT ID---",
}
Copy the output from the ShowIoTCentralConfig tool to Notepad as you will need this information soon.
We need the ID of the Azure Sphere Tenant that is now trusted by Azure IoT Central.
azsphere tenant show-selected
.
text
Default Azure Sphere tenant ID is 'yourSphereTenant' (99999999-e021-43ce-9999-fa9999499994).
This solution supports the AVNET Starter Kit and the Seeed Studio Mini Dev Board with the Grove Breakout. You need to set the configuration that matches your developer board.
The default developer board configuration is for the AVENT Azure Sphere Starter Kit. If you have this board, there is no additional configuration required.
Uncomment the set command that corresponds to your Azure Sphere developer board.
set(AVNET TRUE "AVNET Azure Sphere Starter Kit")
# set(SEEED_STUDIO_RDB TRUE "Seeed Studio Azure Sphere MT3620 Development Kit (aka Reference Design Board or rdb)")
# set(SEEED_STUDIO_MINI TRUE "Seeed Studio Azure Sphere MT3620 Mini Dev Board")
Save the file. This will auto-generate the CMake cache.
Open the app_manifest.json file
Update the Azure IoT Central Application connection properties.
azsphere tenant show-selected
command that you copied to Notepad.Update the network endpoints AllowedConnections with your Azure IoT Central Application endpoint URLs you copied to Notepad.
Review your updated manifest_app.json file. It should look similar to the following.
{
"SchemaVersion": 1,
"Name": "AzureSphereIoTCentral",
"ComponentId": "25025d2c-66da-4448-bae1-ac26fcdd3627",
"EntryPoint": "/bin/app",
"CmdArgs": [ "0ne0099999D" ],
"Capabilities": {
"Gpio": [
"$BUTTON_A",
"$BUTTON_B",
"$LED2",
"$NETWORK_CONNECTED_LED",
"$LED_RED",
"$LED_GREEN",
"$LED_BLUE"
],
"I2cMaster": [ "$I2cMaster2" ],
"PowerControls": [ "ForceReboot" ],
"AllowedConnections": [
"global.azure-devices-provisioning.net",
"iotc-9999bc-3305-99ba-885e-6573fc4cf701.azure-devices.net",
"iotc-789999fa-8306-4994-b70a-399c46501044.azure-devices.net",
"iotc-7a099966-a8c1-4f33-b803-bf29998713787.azure-devices.net",
"iotc-97299997-05ab-4988-8142-e299995acdb7.azure-devices.net",
"iotc-d099995-7fec-460c-b717-e99999bf4551.azure-devices.net",
"iotc-789999dd-3bf5-49d7-9e12-f6999991df8c.azure-devices.net",
"iotc-29999917-7344-49e4-9344-5e0cc9999d9b.azure-devices.net",
"iotc-99999e59-df2a-41d8-bacd-ebb9999143ab.azure-devices.net",
"iotc-c0a9999b-d256-4aaf-aa06-e90e999902b3.azure-devices.net",
"iotc-f9199991-ceb1-4f38-9f1c-13199992570e.azure-devices.net"
],
"DeviceAuthentication": "9d7e79eb-9999-43ce-9999-fa8888888894"
},
"ApplicationType": "Default"
}
Select CMake: [Debug]: Ready from the Visual Studio Code Status Bar.
From Visual Studio Code, press F5 to build, deploy, start, and attached the remote debugger to the application now running the Azure Sphere device.
Open the Visual Studio Code Output tab to view the output from Log_Debug statements in the code.
Pro Tip. You can open the output window by using the Visual Studio Code Ctrl+K Ctrl+H shortcut or click the Output tab.
You may see a couple of ERROR: failure to create IoTHub Handle messages displayed. These messages occur while the connection to IoT Central is being negotiated.
To display your Azure Sphere Device ID, start the Azure Sphere Developer Command Prompt and run the following command.
azsphere device show-attached
You need to Migrate the newly enrolled device to the Carbon Dioxide Monitor template. The template maps the JSON formatted telemetry to the dashboard.
Click Migrate
Select the Carbon Dioxide Monitor Template, and then click migrate.
Azure IoT Central does not update immediately. It may take a minute or two for the temperature, humidity, and pressure telemetry to be displayed.
You can view and set device properties from the Form tab.
The device simulates an HVAC. If the current temperature read by the device is greater than the thermostat setting then the blue LED will turn on to signify the room needs to be cooled. If the room temperature is less than the thermostat setting the red LED will be turned on to signify the heater needs to be turned on.
From the properties view you can set the CO2 alert level. Azure IoT Central uses properties and ultimately Azure IoT Hub device twins to set the alert threshold on the device.
When the CO2 level reaches the alert level on the device you can set an action. The action that has been defined in code is to switch on an LED, but you can go bigger, and instead of setting an LED you could drive a relay to turn on a light, buzz an alarm, or turn on a fan.
Learn more about Azure IoT Central, properties, and device twins from Azure Sphere Developer Learning Path lab 3 Set the room virtual thermostat with Azure IoT Device Twins.
Azure IoT Central is also extensible using rules and workflows. For more information, review Use workflows to integrate your Azure IoT Central application with other cloud services
Have fun and stay safe and be sure to follow us on #JulyOT.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.