This is an example code which leverages Azure IoT Central for a smart farm use case. It collects temperature, humidity, and light data from sensors. Then send the data to Azure via Azure IoT Central via wireless network. Most of the code of this project is based on the ESP32-Azure IoT Kit example of Azure SDK for C on Arduino.
Micro-controller
Sensors
- Temperature and humidity sensors: DHT11
- Sunlight sensor: SI1145
This repository aims to demonstrate how to use an ESP32 board to collect data from sensors and send the data to Azure IoT Central. It is not intended to be a released product. Therefore, this repository is not for discussing Azure IoT Central or requesting new features.
- Install Arduino IDE (recommend the Windows executable than the Windows app version).
- Install SiLabs CP2104 Driver
- Install ESP32 package for Arduino IDE
- Install
Azure IoT SDK for C
library in Arduino IDE - Install libraries for the DHT sensor in Arduino IDE:
Adafruit IO Arduino v2.4.1+
Adafruit Unified Sensor
DHT Sensor Library
- Install libraries for the Sunlight sensor in Arduino IDE:
- Manually install Adafruit_SI1145 library from GitHub
- An Azure subscription and has owner permission to create resources.
- A WiFi network is required to run this example.
Although Arduino IDE is required for running this example, I found Visual Studio Code with Arduino extension (vscode-arduino
) provides better integrated experience.
git clone https://github.com/microsoft/azure-iot-central-esp32.git
- Go to Azure IoT Central and create a new IoT Central application.
- Go to the device page and click
Connect
. Get the value ofID scope
,Device ID
. Select "Shared access signature (SAS)" as the Authentication type. Get the value ofPrimary key
. We will use them in later steps.
-
Open
iot_configs.h
. ReplaceYOUR_WIFI_SSID
andYOUR_WIFI_PASSWORD
with your WiFi SSID and password.#define IOT_CONFIG_WIFI_SSID "YOUR_WIFI_SSID" #define IOT_CONFIG_WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
-
Connect with Azure IoT Central.
a. Open
iot_configs.h
. ReplaceYOUR_ID_SCOPE
,YOUR_DEVICE_ID
, andYOUR_DEVICE_KEY
with theID scope
,Device ID
and thePrimary key
from Azure IoT Central setup step 6.#define DPS_ID_SCOPE "YOUR_ID_SCOPE" #define IOT_CONFIG_DEVICE_ID "YOUR_DEVICE_ID" #define IOT_CONFIG_DEVICE_KEY "YOUR_DEVICE_KEY"
b. In IDE, set the board to
Adafruit ESP32 Feather
. and pick the right port. Upload the code to the board. You should see the Wi-Fi connection status, MQTT connection status, and events in Serial Monitor.
After connected Azure IoT Central via the previous step, you will find a device template, "Espressif ESP32 Azure IoT Kit", that is automatically generated in Azure IoT Central portal.
- Create a new device in Azure IoT Central: On the left navigation menu, click on "Devices". Click on the "New" button on the top. Provide a name and an ID for the device. Select "Espressif ESP32 Azure IoT Kit" as the device template.
- Modifiy the data model: In the device template menu, select
Model
->{} Edit DTDL
. Copy and paste the content ofAzure_IoT_Central\Device_Template\Model\Espressif ESP32 Azure IoT Kit.json
, clickSave
. Then clickPublish
on the top menu. - Validate device connection: Once the updated template is published, navigate to "Devices" -> "Espressif ESP32 Azure IoT Kit" -> "Adafruit Feature ESP32" -> The device you created in D-1. You should see the device status change to
Connected
. Click on the "Raw data" tab, you can see the data with the latest data model uploaded from the device.
This code is modified from the Azure_IoT_Central_ESP32
sample code of Azure SDK for C in Arduino library.
Azure_IoT_Central\
: Configurations of Azure IoT Central.Device_Template\Model\Espressif ESP32 Azure IoT Kit.json
: The DTDL file for the data model.
Azure_IoT_Central_ESP32
: The code for the ESP32.Azure_IoT_Central_ESP32.ino
: The main file.Azure_IoT_PnP_Template.cpp
: Where we fetch data from the sensors and prepare payload for sending to the cloud.iot_configs.h
: The secrets for the Azure IoT Central along with other configurations.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft’s Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party’s policies.