azure-sdk-for-c-arduino/examples/Azure_IoT_Hub_ESP8266
Ewerton Scaboro da Silva 815e4ec64d Fix leaks on wifi reconnection, update docs 2023-05-31 09:53:14 -07:00
..
Azure_IoT_Hub_ESP8266.ino Fix leaks on wifi reconnection, update docs 2023-05-31 09:53:14 -07:00
iot_configs.h Rename samples for better readability 2021-12-03 14:26:13 -08:00
readme.md Fix leaks on wifi reconnection, update docs 2023-05-31 09:53:14 -07:00

readme.md

page_type description languages products
sample Connecting an ESP8266 device to Azure IoT using the Azure SDK for Embedded C
c
azure-iot
azure-iot-pnp
azure-iot-dps
azure-iot-hub

How to Setup and Run Azure SDK for Embedded C IoT Hub Client on Espressif ESP8266 NodeMCU

Introduction

This is a "to-the-point" guide outlining how to run an Azure SDK for Embedded C IoT Hub telemetry sample on an Esp8266 NodeMCU microcontroller. The command line examples are tailored to Debian/Ubuntu environments.

What is Covered

  • Configuration instructions for the Arduino IDE to compile a sample using the Azure SDK for Embedded C.
  • Configuration, build, and run instructions for the IoT Hub telemetry sample.

The following was run on Windows 11, with Arduino IDE 2.1.0 and Esp8266 module 3.1.2.

Prerequisites

  • Have an Azure account created.

  • Have an Azure IoT Hub created.

  • Have a logical device created in your Azure IoT Hub using the authentication type "Symmetric Key".

    NOTE: Device keys are used to automatically generate a SAS token for authentication, which is only valid for one hour.

  • Have the latest Arduino IDE installed.

  • Have the ESP8266 board support installed on Arduino IDE. ESP8266 boards are not natively supported by Arduino IDE, so you need to add them manually.

    • ESP8266 boards are not natively supported by Arduino IDE, so you need to add them manually.
    • Follow the instructions in the official Esp8266 repository.
  • Have one of the following interfaces to your Azure IoT Hub set up:

    NOTE: This guide demonstrates use of the Azure CLI and does NOT demonstrate use of Azure IoT Explorer.

Setup and Run Instructions

  1. Run the Arduino IDE.

  2. Install the Azure SDK for Embedded C library.

    • On the Arduino IDE, go to menu Sketch, Include Library, Manage Libraries....
    • Search for and install azure-sdk-for-c.
  3. Install the Arduino PubSubClient library. (PubSubClient is a popular MQTT client for Arduino.)

    • On the Arduino IDE, go to menu Sketch, Include Library, Manage Libraries....
    • Search for PubSubClient (by Nick O'Leary).
    • Hover over the library item on the result list, then click on "Install".
  4. Open the ESPRESSIF ESP 8266 sample.

    • On the Arduino IDE, go to menu File, Examples, azure-sdk-for-c.
    • Click on az_esp8266 to open the sample.
  5. Configure the ESPRESSIF ESP 8266 sample.

    Enter your Azure IoT Hub and device information into the sample's iot_configs.h.

  6. Connect the Esp8266 NodeMCU microcontroller to your USB port.

  7. On the Arduino IDE, select the board and port.

    • Go to menu Tools, Board and select NodeMCU 1.0 (ESP-12E Module).
    • Go to menu Tools, Port and select the port to which the microcontroller is connected.
  8. Upload the sketch.

    • Go to menu Sketch and click on Upload.

      Expected output of the upload:

      Executable segment sizes:
      IROM   : 361788          - code in flash         (default or ICACHE_FLASH_ATTR)
      IRAM   : 26972   / 32768 - code in IRAM          (ICACHE_RAM_ATTR, ISRs...)
      DATA   : 1360  )         - initialized variables (global, static) in RAM/HEAP
      RODATA : 2152  ) / 81920 - constants             (global, static) in RAM/HEAP
      BSS    : 26528 )         - zeroed variables      (global, static) in RAM/HEAP
      Sketch uses 392272 bytes (37%) of program storage space. Maximum is 1044464 bytes.
      Global variables use 30040 bytes (36%) of dynamic memory, leaving 51880 bytes for local variables. Maximum is 81920 bytes.
      /home/user/.arduino15/packages/esp8266/tools/python3/3.7.2-post1/python3 /home/user/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/upload.py --chip esp8266 --port /dev/ttyUSB0 --baud 230400 --before default_reset --after hard_reset write_flash 0x0 /tmp/arduino_build_826987/azure_iot_hub_telemetry.ino.bin
      esptool.py v2.8
      Serial port /dev/ttyUSB0
      Connecting....
      Chip is ESP8266EX
      Features: WiFi
      Crystal is 26MHz
      MAC: dc:4f:22:5e:a7:09
      Uploading stub...
      Running stub...
      Stub running...
      Changing baud rate to 230400
      Changed.
      Configuring flash size...
      Auto-detected Flash size: 4MB
      Compressed 396432 bytes to 292339...
      
      Writing at 0x00000000... (5 %)
      Writing at 0x00004000... (11 %)
      Writing at 0x00008000... (16 %)
      Writing at 0x0000c000... (22 %)
      Writing at 0x00010000... (27 %)
      Writing at 0x00014000... (33 %)
      Writing at 0x00018000... (38 %)
      Writing at 0x0001c000... (44 %)
      Writing at 0x00020000... (50 %)
      Writing at 0x00024000... (55 %)
      Writing at 0x00028000... (61 %)
      Writing at 0x0002c000... (66 %)
      Writing at 0x00030000... (72 %)
      Writing at 0x00034000... (77 %)
      Writing at 0x00038000... (83 %)
      Writing at 0x0003c000... (88 %)
      Writing at 0x00040000... (94 %)
      Writing at 0x00044000... (100 %)
      Wrote 396432 bytes (292339 compressed) at 0x00000000 in 13.0 seconds (effective 243.4 kbit/s)...
      Hash of data verified.
      
      Leaving...
      Hard resetting via RTS pin...
      

  9. Monitor the MCU (microcontroller) locally via the Serial Port.

    • Go to menu Tools, Serial Monitor.

      If you perform this step right away after uploading the sketch, the serial monitor will show an output similar to the following upon success:

      Connecting to WIFI SSID buckaroo
      .......................WiFi connected, IP address:
      192.168.1.123
      Setting time using SNTP..............................done!
      Current time: Thu May 28 02:55:05 2020
      Client ID: mydeviceid
      Username: myiothub.azure-devices.net/mydeviceid/?api-version=2018-06-30&DeviceClientType=c%2F1.0.0
      Password: SharedAccessSignature sr=myiothub.azure-devices.net%2Fdevices%2Fmydeviceid&sig=placeholder-password&se=1590620105
      MQTT connecting ... connected.
      
  10. Monitor the telemetry messages sent to the Azure IoT Hub using the connection string for the policy name iothubowner found under "Shared access policies" on your IoT Hub in the Azure portal.

    $ az iot hub monitor-events --login <your Azure IoT Hub owner connection string in quotes> --device-id <your device id>
    
    Expected telemetry output:

    Starting event monitor, filtering on device: mydeviceid, use ctrl-c to stop...
    {
        "event": {
            "origin": "mydeviceid",
            "payload": "payload"
        }
    }
    {
        "event": {
            "origin": "mydeviceid",
            "payload": "payload"
        }
    }
    {
        "event": {
            "origin": "mydeviceid",
            "payload": "payload"
        }
    }
    {
        "event": {
            "origin": "mydeviceid",
            "payload": "payload"
        }
    }
    {
        "event": {
            "origin": "mydeviceid",
            "payload": "payload"
        }
    }
    {
        "event": {
            "origin": "mydeviceid",
            "payload": "payload"
        }
    }
    ^CStopping event monitor...
    

Certificates - Important to know

The Azure IoT service certificates presented during TLS negotiation shall be always validated, on the device, using the appropriate trusted root CA certificate(s).

The Azure SDK for C Arduino library automatically installs the root certificate used in the United States regions, and adds it to the Arduino sketch project when the library is included.

For other regions (and private cloud environments), please use the appropriate root CA certificate.

Additional Information

For important information and additional guidance about certificates, please refer to this blog post from the security team.

Troubleshooting

  • The error policy for the Embedded C SDK client library is documented here.
  • File an issue via GitHub Issues.
  • Check previous questions or ask new ones on StackOverflow using the azure and c tags.

Contributing

This project welcomes contributions and suggestions. Find more contributing details here.

License

This Azure SDK for C Arduino library is licensed under MIT license.

Azure SDK for Embedded C is licensed under the MIT license.