Utsav Sharma
Utsav Sharma

Reputation: 1

Error in uploading sensor data to Azure IOT central

I have watched this video and tried to implement the same using all the required components and I have been getting errors in getting connected to the portal and am neither getting output from sensors. this is the code that I have been using: `

#include "DHT.h"        // including the library of DHT11 temperature and humidity sensor
#include <ESP8266WiFi.h>

#define DHTTYPE DHT11  
#include "D:/ARDUINO/ESP8266/ESP8266/src/iotc/common/string_buffer.h"
#include "D:/ARDUINO/ESP8266/ESP8266/src/iotc/iot"
#include "D:/ARDUINO/ESP8266/ESP8266/src/connection.h"

#define dht_dpin 12 // creating the object sensor on pin 'D12'
DHT dht(dht_dpin, DHTTYPE); 

#define WIFI_SSID "<ENTER WIFI SSID>"
#define WIFI_PASSWORD "<ENTER WIFI PASSWORD>"

const char* SCOPE_ID = "<ENTER SCOPE ID>";
const char* DEVICE_ID = "<ENTER DEVICE ID>";
const char* DEVICE_KEY = "<ENTER DEVICE KEY>";

int echoPin = D6;
int trigPin = D8;
int pingTravelTime;
float pingTravelDistance;
float distanceToTarget;
float speedOfsound;


void on_event(IOTContext ctx, IOTCallbackInfo* callbackInfo);
void on_event(IOTContext ctx, IOTCallbackInfo* callbackInfo) {
  // ConnectionStatus
  if (strcmp(callbackInfo->eventName, "ConnectionStatus") == 0) {
    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) {
    buffer.initialize(callbackInfo->payload, callbackInfo->payloadLength);
  }

  LOG_VERBOSE("- [%s] event was received. Payload => %s\n",
              callbackInfo->eventName, buffer.getLength() ? *buffer : "EMPTY");

  if (strcmp(callbackInfo->eventName, "Command") == 0) {
    LOG_VERBOSE("- Command name was => %s\r\n", callbackInfo->tag);
  }
  dht.begin();
}

void setup() {
  Serial.begin(9600);

  connect_wifi(WIFI_SSID, WIFI_PASSWORD);
  connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY);

  if (context != NULL) {
    lastTick = 0;  // set timer in the past to enable first telemetry a.s.a.p
  }
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
}

void loop() {

  digitalWrite(trigPin,LOW);
  delayMicroseconds(10);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  
  float h = dht.readHumidity();
  float t = dht.readTemperature();
     // Reading the temperature in Celsius degrees and store in the t variable
     // Reading the humidity index and store in the h variable
  
  
  pingTravelTime = pulseIn(echoPin,HIGH);
  delay(25);
  pingTravelDistance = (pingTravelTime*330*100)/(1000000);
  speedOfsound = (pingTravelDistance*1000000)/pingTravelTime;
  distanceToTarget = pingTravelDistance/2;
  
  if (isConnected) {

    unsigned long ms = millis();
    if (ms - lastTick > 10000) {  // send telemetry every 10 seconds
      char msg[64] = {0};
      int pos = 0, errorCode = 0;

      lastTick = ms;
      if (loopId++ % 4 == 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);
        errorCode = iotc_send_telemetry(context, msg, pos);

        pos = snprintf(msg, sizeof(msg) - 1, "{\"Distance\":%f}",
                       distanceToTarget);
        errorCode = iotc_send_telemetry(context, msg, pos);

        pos = snprintf(msg, sizeof(msg) - 1, "{\"Speed\":%f}",
                       speedOfsound);
        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
  } else {
    iotc_free_context(context);
    context = NULL;
    connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY);
  }
  
  delay(50);
}

`

I tried to implement this project for a thesis of mine and was not getting the results since the connection is not being established. These are the errors i was getting:

ERROR: couldn't fetch the time from NTP. - -

X - Error at connection.h:32 Error @ tcp_connect. Code 1 - ERROR: Client was not connected. - - iot.dps : getting auth... - iotc.dps : getting operation id... - ERROR: DPS endpoint PUT call has failed.

this is the github link for downloading the required header files for connection establishment

Upvotes: 0

Views: 563

Answers (1)

LeelaRajesh_Sayana
LeelaRajesh_Sayana

Reputation: 650

I could reproduce the same error in my NodeMCU. Please find the below image for reference. enter image description here

If you let the code run and observe the NodeMCU serial monitor, it will occasionally spit out additional information as you can find in the below image enter image description here

The error message in my case indicates that it is an issue with Authorization. I had the wrong Primary key provided in the code. In order to be able to connect to the device on Azure IoT Central, make sure the device is not set to be simulated when you create it.

Once you have the device created from the template, navigate to the device and click on connect to get the following details that needs to be entered in the code. Attached the below image for reference. enter image description here

Here is the code snippet I have used to generate data

#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 "<SSID>"
#define WIFI_PASSWORD "<WIFIPASSWORD>"

const char *SCOPE_ID = "<value 2 from above image>";
const char *DEVICE_ID = "<value 3 from above image>";
const char *DEVICE_KEY = "<value 4 from above image>";

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)
{
    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)
{
    buffer.initialize(callbackInfo->payload, callbackInfo->payloadLength);
}

LOG_VERBOSE("- [%s] event was received. Payload => %s\n",
            callbackInfo->eventName, buffer.getLength() ? *buffer : "EMPTY");

if (strcmp(callbackInfo->eventName, "Command") == 0)
{
    LOG_VERBOSE("- Command name was => %s\r\n", callbackInfo->tag);
}
}

void setup()
{
Serial.begin(9600);

connect_wifi(WIFI_SSID, WIFI_PASSWORD);
connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY);

if (context != NULL)
{
    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)
{

    unsigned long ms = millis();
    if (ms - lastTick > 10000)
    { // 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);
            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
}
else
{
    iotc_free_context(context);
    context = NULL;
    connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY);
}
}

Here are the Temperature and Humidity values generated from the code enter image description here

The data generated the following graph on the Azure IoT Central. enter image description here

Please note that I have used DHT11 sensor and connected to read it from the GPI0 2 (D4) pin on my NodeMCU board. I have used the Arduino IDE version 1.8.19 and ESP8266 board version 2.7.4

enter image description here

Upvotes: 0

Related Questions