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)
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);
``` }
}