Giter VIP home page Giter VIP logo

azure-iot-central-esp32-sample's Introduction

Introduction

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.

Hardwares

Micro-controller

Sensors

  • Temperature and humidity sensors: DHT11
  • Sunlight sensor: SI1145

Statement of Purpose

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.

Getting Started

Requirements

  • 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:
  • An Azure subscription and has owner permission to create resources.
  • A WiFi network is required to run this example.

Development environment

Although Arduino IDE is required for running this example, I found Visual Studio Code with Arduino extension (vscode-arduino) provides better integrated experience.

Setup

A. Clone the repository

git clone https://github.com/microsoft/azure-iot-central-esp32.git

B. Setup Azure IoT Central

  1. Go to Azure IoT Central and create a new IoT Central application.
  2. Go to the device page and click 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.

C. Setup devices

  1. Wire hardwares.

  2. 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"
  3. 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.

D. Create a device and modify data model in IoT Central.

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.

  1. 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.
  2. Modifiy the data model: In the device template menu, select 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.
  3. 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.

About the code

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.

Trademarks

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.

azure-iot-central-esp32-sample's People

Contributors

microsoftopensource avatar momuno avatar vivihung avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

azure-iot-central-esp32-sample's Issues

TLS certificate verification error

s-mbedtls: mbedtls_ssl_handshake returned -0x2700
E (59347) esp-tls: Failed to open new connection
E (59348) TRANSPORT_BASE: Failed to open a new connection
E (59355) MQTT_CLIENT: Error transport connect
2023/5/29 14:00:36 [ERROR] MQTT client in ERROR state.
2023/5/29 14:00:36 [ERROR] esp_tls_stack_err=9984; esp_tls_cert_verify_flags=8;esp_transport_sock_errno=0;error_type=1;connect_return_code=0
2023/5/29 14:00:36 [ERROR] connect_return_code=MQTT_CONNECTION_ACCEPTED
2023/5/29 14:00:36 [INFO] MQTT client disconnected.
2023/5/29 14:00:37 [INFO] MQTT client target uri set to 'mqtts://global.azure-devices-provisioning.net'
2023/5/29 14:00:37 [INFO] MQTT client connecting.

Above are the logs that are shown and this repeats over and over again and sometimes it will randomly connect but my goal is to get a stable connection. I think that this error has something to do with the Digicert root migration that was forced on May 15, 2023(it is may 29th today) and I am wondering if this sample still works or if I need to change something for it to work?

TELEMETRY_FREQUENCY_IN_SECONDS

Good Afternoon.

Changing the figure on iot_configs.h - #define TELEMETRY_FREQUENCY_IN_SECONDS does not seem to change anything.

I tied 60, 600, 6000 nothing changed.

According to my serial monitor, the telemetry gets sent every 10 seconds regardless of the above setting.

Otherwise works very well. Thank you.

Please advise

Figure out a way not to clone the sample code

We got feedback from users that this sample code stop working during mid-Nov, 2022. After further investigation, we realized there were bug fixes in the source (Azure_IoT_Central_ESP32_AzureIoTKit) that are necessary to keep our sample working.

Although manually applying the fixes to our example resolved the problem, it's not a robust solution for long term. Therefore, I create this issue to figure out a way leveraging the source without duplicating the code. So that if there's any changes, developers who follows the instructions here will get the latest.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.