lzyms / ESP32IoTCentral

Azure IoT Central example for esp32-azure-kit
3 stars 7 forks source link

Azure IoT Central example for esp32-azure-kit

In this tutorial, you will learn how to use the esp32-azure-kit board to send data to Azure IoT Central and to receive events back from Azure IoT Central to be processed by the board.

Features implemented

What you need

Open the project folder

Start VS Code

Open IoT Workbench Examples

Create Azure IoT Central application

  1. Use you Azure account log into Azure IoT Central.

  2. Create a free custom applicaton.

    IoT Central, crate a free custome application

  3. Click Create Device Templates and enter a template name to create a new template

  4. Click the Edit Template button, then click the New measurement button to add measurements

    IoT Central, add measurement

  5. Create new Telemetry as below:

    Display Name Field Name Unit Minimum Value Maximum Value
    Temperature Temperature -40 100
    Humidity Humidity % 0 100
    Light AmbientLight lx 0 3800
    Pressure Pressure kPa 26 126
    Altitude Altitude M
    MagnetometerX MagnetometerX mgauss
    MagnetometerY MagnetometerY mgauss
    MagnetometerZ MagnetometerZ mgauss
    Pitch Pitch ° -180 180
    Roll Roll ° -180 180

    Note: You can only add the measurements that you care about, you don't need to add all of them

  6. Click Device Explorer -> + -> Real to add a real device. After creating the device, it will navigate to the device template page.

    IoT Central, add real device

  7. Click the Properties tab, then click Edit Tempalte. Select Device Property on the Liabray at the left of the page, then add the following properties

    Display Name Field Name Data Type
    Dice Number dieNumber number
  8. Click the Commands tab, then click Edit Tempalte. Click New Command at the left of the page, then add the following properties

    Display Name Field Name Data Type
    Message echo text

    Click + at the right of Input Fields to add the following field

    Display Name Field Name Data Type
    Send message to device displayedValue text
  9. Click the Settings tab, then click Edit Tempalte. Select Number on the Liabray at the left of th page, then add the following properties

    Display Name Field Name Unit Minimum Value Maximum Value Initial Value
    Fan Speed fanSpeed % 10 100 50

    Click Save and repeat this step to add the follwing properties

    Display Name Field Name Unit Minimum Value Maximum Value Initial Value
    Temperature Threshold temThreshold -40 100 30

    Click the Commands tab again, then click Edit Tempalte. Click New Command at the left of the page, then add the following properties

    Display Name Field Name Data Type
    Start Fan startFan text
    Stop Fan stopFan text

    Note: This step requries a L298N DC motor control connect to the GPIO 12 and 13 on the esp32-azure-kit board, if you don't have the motor connected, you can skip this step

  10. At last, click Connect button at the top right of the page, it will pop Device Conenction page. Download the dps-keygen tool at here based on your OS, use the dps_cstr tool to generate the connectoin string as below.

    IoT Central, device connection

    dps_cstr <scope_id> <device_id> <Primary Key(for device)> 

    scope_id, device_id and Primary Key is listed on the Device Connectin page.

Config Device Code

  1. Open the source file(.ino) for device code and update the following lines with your WiFi ssid and password:

    // Please input the SSID and password of WiFi
    const char* ssid     = "";
    const char* password = "";
  2. Copy and paste the device connection string that generated on the previous step into the following line in device code.

    This copies the connection string that is retrieved from the Create Azure IoT Central application step.

    /*String containing Hostname, Device Id & Device Key in the format:                         */
    /*  "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"                */
    /*  "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>"    */
    static const char* connectionString = "";

Build and upload the device code

  1. Open the command palette and select Azure IoT Device Workbench: Upload Device Code.

    IoT Device Workbench: Device -> Upload

  2. VS Code then starts verifying and uploading the code to your DevKit.

    IoT Device Workbench: Device -> Uploaded

  3. The ESP32 device reboots and starts running the code.

    Note: M5Stack-Core-ESP32 is set as the default board after the IoT project is created. To change the setting, use F1 or Ctrl+Shift+P (macOS: Cmd+Shift+P) to open the command palette, type and select Arduino: Board Config. Change to use ESP32 Wrover Module (esp32) board in the Arduino Board Configuration window.

    change board

Monitor device status in Azure IoT Central

  1. Use you Azure account log into Azure IoT Central

  2. Once the device is connected to WIFI, the WIFI LED will be turned on. Once the device connect and send data to Azure IoT Central, the Azure LED will blink while sending data. The device's defalut screen will show the temperature and humidity.

    device: default screen

  3. Navigate to the device measurements page, you'll see all the telemetry data from the device

    IoT Central: measurments

  4. Navigate to the device Commands page, write a message at the Message grid and click Run, the message will display on the device screen.

    IoT Central: message

    Note: Click the KEY_IO0 button on the device will switch the messaage screen to humiture screen

  5. Shake the device, it will simulate shaking a dice and report a random dice number to IoT central, you can see the dice number at the IoT Central Properties page

    IoT Central: dice

  6. This step requries a L298N DC motor control connect to the GPIO 12 and 13 on the esp32-azure-kit board. Once you have the motor control connect with a fan and the board, navigate to the Commands page, you can use the Start Fan and Stop Fan command to control the motor on and off.

    IoT Central: commands

    Navigate to the settings page, you can set the fan speed and temperature threshold, once the temperature is greate than the defined value, the fan will start automaticlly at the speed.

    IoT Central: settings