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
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.
Azure IoT SDK for C
library in Arduino IDEAdafruit IO Arduino v2.4.1+
Adafruit Unified Sensor
DHT Sensor Library
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
Connect
. Get the value of ID scope
, Device ID
. Select "Shared access signature (SAS)" as the Authentication type. Get the value of Primary key
. We will use them in later steps.Wire hardwares.
Open iot_configs.h
. Replace YOUR_WIFI_SSID
and YOUR_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
. Replace YOUR_ID_SCOPE
, YOUR_DEVICE_ID
, and YOUR_DEVICE_KEY
with the ID scope
, Device ID
and the Primary 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.
Model
-> {} Edit DTDL
. Copy and paste the content of Azure_IoT_Central\Device_Template\Model\Espressif ESP32 Azure IoT Kit.json
, click Save
. Then click Publish
on the top menu.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.