Unable to send data to Azure IOT Central hub device from NodeMCU ESP8266 device

Manish Yadav 0 Reputation points
2024-03-10T08:16:18.4066667+00:00

I am trying to connect to Azure IoT central device model and send real-time temperature, humidity- sensor data from NodeMCU ESP8266 Wifi enabled IoT device.

How to check if Azure is able to receive data or if there is something wrong with security/certificate etc? I am using Shared Access Signature (SAS) Primary Key to connect the device.

  • ERROR: MQTT client connect attempt failed. Check host, deviceId, username and password. (state -2)
    • Is connected ? NO (4)

X - Error at connection.h:32

Error @ iotc_connect. Code 1
```- ERROR: Client was not connected.

Here is the Code:

 

#include <ESP8266WiFi.h>

#include "src/iotc/common/string_buffer.h"

#include "src/iotc/iotc.h"

#include "DHT.h"

#define DHTPIN 2

#define DHTTYPE DHT11   // DHT 11

#define WIFI_SSID "Madhav"

#define WIFI_PASSWORD "madhav888"

const char* SCOPE_ID = "0ne00BFF6FD";

const char* DEVICE_ID = "DeloitteDHT11";

const char* DEVICE_KEY = "<DeviceKey>";

DHT dht(DHTPIN, DHTTYPE);

void on_event(IOTContext ctx, IOTCallbackInfo* callbackInfo);

#include "src/connection.h"

void on_event(IOTContext ctx, IOTCallbackInfo* callbackInfo) {

  // ConnectionStatus

  if (strcmp(callbackInfo->eventName, "ConnectionStatus") == 0) {

```haskell
LOG_VERBOSE("Is connected ? %s (%d)",

            callbackInfo->statusCode == IOTC_CONNECTION_OK ? "YES" : "NO",

            callbackInfo->statusCode);

isConnected = callbackInfo->statusCode == IOTC_CONNECTION_OK;

return;
```  }

  // payload buffer doesn't have a null ending.

  // add null ending in another buffer before print

  AzureIOT::StringBuffer buffer;

  if (callbackInfo->payloadLength > 0) {

```dockerfile
buffer.initialize(callbackInfo->payload, callbackInfo->payloadLength);
```  }

  LOG_VERBOSE("- [%s] event was received. Payload => %s\n",

```yaml
          callbackInfo->eventName, buffer.getLength() ? *buffer : "EMPTY");
```  if (strcmp(callbackInfo->eventName, "Command") == 0) {

```sql
LOG_VERBOSE("- Command name was => %s\r\n", callbackInfo->tag);
```  }

}

void setup() {

 // pinMode(LED_BUILTIN, OUTPUT); 

   //digitalWrite(LED_BUILTIN, LOW);

  Serial.begin(9600);

  WiFi.mode(WIFI_STA);

  connect_wifi(WIFI_SSID, WIFI_PASSWORD);

  connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY);

  if (context != NULL) {

```sql
lastTick = 0;  // set timer in the past to enable first telemetry a.s.a.p
```  }

   dht.begin();

}

void loop() {

float h = dht.readHumidity();

float t = dht.readTemperature();

  

  if (isConnected) {

```cpp
//digitalWrite(LED_BUILTIN, LOW);

unsigned long ms = millis();

if (ms - lastTick > 1000) {  // send telemetry every 10 seconds

  char msg[64] = {0};

  int pos = 0, errorCode = 0;

  lastTick = ms;

  if (loopId++ % 2 == 0) {  // send telemetry

    pos = snprintf(msg, sizeof(msg) - 1, "{\"Temperature\": %f}",t);

    errorCode = iotc_send_telemetry(context, msg, pos);


    

    pos = snprintf(msg, sizeof(msg) - 1, "{\"Humidity\":%f}",h);

    //digitalWrite(LED_BUILTIN, LOW);

    errorCode = iotc_send_telemetry(context, msg, pos);


      

  } else {  // send property


    

  } 



  msg[pos] = 0;

  if (errorCode != 0) {

    LOG_ERROR("Sending message has failed with error code %d", errorCode);


    

  }

}

iotc_do_work(context);  // do background work for iotc

//digitalWrite(LED_BUILTIN, LOW);
```   // delay(1000);

```sql
//digitalWrite(LED_BUILTIN, HIGH);

//delay(1000);
```  } else {

```sql
iotc_free_context(context);

context = NULL;

connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY);
```  }

}

Azure IoT Central
Azure IoT Central
An Azure hosted internet of things (IoT) application platform.
352 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 13,546 Reputation points
    2024-03-11T14:27:19.0466667+00:00

    Hi @Manish Yadav Greetings! Welcome to Microsoft Q&A forum. Thank you for posting this question here.

    I wanted to check if you are using the Azure SDK for C library to connect to the IoT Central application. When you add this library to your Arduino IDE, you’re getting a fully tested set of APIs, documentation, and sample implementations on popular embedded platforms. Some of the hardware platforms with out-of-the-box support include Espressif ESP32, ESP8266 and the Realtek Ameba D. Please refer the article Arduino library for Azure IoT for more information on the library.

    Please find the GitHub repository sample on Getting started with the ESPRESSIF ESP32 and Azure IoT Central with Azure SDK for C Arduino library which provides all the instructions on how to set up and test the code using Arduino. Even though the code sample is for ESP32, it can be used on ESP8266. Just make sure you configure the Arduino IDE board manager to support ESP8266 device by following the steps in Installing with Boards Manager

    Hope this helps. Please let us know if you run into any issues implementing the sample or need any further assistance.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    0 comments No comments