2

I am looking for a working example of an ESP32 sending telemetry data to Azure IoT hub. I find nothing working around the internet: below is the code I am trying to make it working.

As you can see in the logs, after a successful connection to the WiFi, followed by a successful connection to IoT Hub, the message has been accepted by the drive, but it is never sent successfully: Sound kinda timeout.

I posted the full Arduino SW for you to try.

[UPDATE] You need to install the ESP32MQQTClient lib from Microsoft that you'll find right there: https://platformio.org/lib/show/5462/ESP32%20Azure%20IoT%20Arduino

Bottom note: IoT Hub is fully configured and working, and device is provisioned and the connection string is OK.

And the logs are as follows:

11:36:50.573 -> mode:DIO, clock div:1
11:36:50.573 -> load:0x3fff0018,len:4
11:36:50.573 -> load:0x3fff001c,len:1216
11:36:50.609 -> ho 0 tail 12 room 4
11:36:50.609 -> load:0x40078000,len:10944
11:36:50.609 -> load:0x40080400,len:6388
11:36:50.609 -> entry 0x400806b4
11:36:50.881 -> ============ Starting connecting WiFi.
11:36:51.513 -> ......
11:36:54.011 -> ============ WiFi connected
11:36:54.011 -> IP address: 
11:36:54.011 -> 192.168.1.20
11:36:54.011 -> Info: Initializing SNTP
11:36:54.999 -> Info: SNTP initialization complete
11:36:54.999 -> Info: IoT Hub SDK for C, version 1.1.23
11:36:58.050 -> Info: >>>Connection status: connected

11:36:58.050 -> ============ start sending events. 11:36:58.083 -> Info: >>>IoTHubClient_LL_SendEventAsync accepted message for transmission to IoT Hub.

11:37:08.066 -> Error: Time:Fri Nov 19 10:37:08 2021 File:/Users/stephanedeluca/Library/Arduino15/packages/esp32/hardware/esp32/1.0.6/libraries/AzureIoT/src/Esp32MQTTClient.cpp Func:SendEventOnce Line:316 Waiting for send confirmation, time is up 10006

11:37:08.099 -> Info: >>>Re-connect.

11:37:08.099 -> Error: Time:Fri Nov 19 10:37:08 2021 File:/Users/stephanedeluca/Library/Arduino15/packages/esp32/hardware/esp32/1.0.6/libraries/AzureIoT/src/az_iot/c-utility/pal/src/tlsio_openssl_compact.c Func:tlsio_openssl_destroy Line:213 tlsio_openssl_destroy called while not in TLSIO_STATE_CLOSED.

11:37:08.136 -> Info: >>>Confirmation[0] received for message tracking id = 0 with result = IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY

11:37:08.136 -> Info: Initializing SNTP

11:37:09.109 -> Info: SNTP initialization complete

11:37:09.109 -> Info: IoT Hub SDK for C, version 1.1.23

11:37:09.470 -> Info: >>>IoTHubClient_LL_SendEventAsync accepted message for transmission to IoT Hub.

11:37:10.911 -> Info: >>>Connection status: connected

11:37:18.134 -> Error: Time:Fri Nov 19 10:37:18 2021 File:/Users/stephanedeluca/Library/Arduino15/packages/esp32/hardware/esp32/1.0.6/libraries/AzureIoT/src/Esp32MQTTClient.cpp Func:SendEventOnce Line:316 Waiting for send confirmation, time is up 10049

11:37:18.134 -> xxxxxxxxxxxx Failure sendind data: {"batteries":{"0":{"current": 1234.5}}}...

#include <WiFi.h>
#include "Esp32MQTTClient.h"

// Please input the SSID and password of WiFi
const char* ssid     = "XXX";
const char* password = "XXX";

/*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 = "XXX";
static bool hasIoTHub = false;

void setup() {
  Serial.begin(115200);
  Serial.println("============ Starting connecting WiFi.");
  delay(10);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\n============ WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  if (!Esp32MQTTClient_Init((const uint8_t*)connectionString))
  {
    hasIoTHub = false;
    Serial.println("============ Initializing IoT hub failed.");
    return;
  }
  hasIoTHub = true;
}

void loop() {
  Serial.println("============ start sending events.");
  if (hasIoTHub)
  {
    char buff[128];

    // replace the following line with your data sent to Azure IoTHub
    snprintf(buff, 128, "{\"batteries\":{\"0\":{\"current\": 1234.5}}}");

    if (Esp32MQTTClient_SendEvent(buff))
    {
      Serial.println("============ Sending data succeeded");
    }
    else
    {
      Serial.printf ("xxxxxxxxxxxx Failure sendind data: %s...", buff); Serial.println();
    }
    delay(5000);
  }
}

0 Answers0