No data/payload with connected device


#1

Followed instructions in ESP8266 and DHT22 getting started guide. The device is connected but no data/payload is being received by Losant? Correct data is being written to serial port.


#2

Hi Rudi!

Would you be able to post the code? ( Make sure to take out device ID, key, and secret )


#3
//This is straight from the online example: https://www.losant.com/blog/getting-started-with-the-esp8266-and-dht22-sensor

/**
 * Example for sending temperature and humidity
 * to the cloud using the DHT22 and ESP8266
 *
 * Copyright (c) 2016 Losant IoT. All rights reserved.
 * https://www.losant.com
 */


#include "DHT.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Losant.h>

#define DHTPIN 4     // what digital pin the DHT22 is conected to
#define DHTTYPE DHT11   // There are multiple kinds of DHT sensors

DHT dht(DHTPIN, DHTTYPE);

// WiFi credentials.
const char* WIFI_SSID = "WiFi";
const char* WIFI_PASS = "xxxx";

// Losant credentials.
const char* LOSANT_DEVICE_ID = ""; //removed for security
const char* LOSANT_ACCESS_KEY = ""; //removed for security
const char* LOSANT_ACCESS_SECRET = ""; //removed for security


WiFiClientSecure wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

void connect() {

  // Connect to Wifi.
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  // WiFi fix: https://github.com/esp8266/Arduino/issues/2186
  WiFi.persistent(false);
  WiFi.mode(WIFI_OFF);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  unsigned long wifiConnectStart = millis();

  while (WiFi.status() != WL_CONNECTED) {
    // Check to see if
    if (WiFi.status() == WL_CONNECT_FAILED) {
      Serial.println("Failed to connect to WIFI. Please verify credentials: ");
      Serial.println();
      Serial.print("SSID: ");
      Serial.println(WIFI_SSID);
      Serial.print("Password: ");
      Serial.println(WIFI_PASS);
      Serial.println();
    }

    delay(500);
    Serial.println("...");
    // Only try for 5 seconds.
    if(millis() - wifiConnectStart > 5000) {
      Serial.println("Failed to connect to WiFi");
      Serial.println("Please attempt to send updated configuration parameters.");
      return;
    }
  }

  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  Serial.print("Authenticating Device...");
  HTTPClient http;
  http.begin("http://api.losant.com/auth/device");
  http.addHeader("Content-Type", "application/json");
  http.addHeader("Accept", "application/json");

  /* Create JSON payload to sent to Losant
   *
   *   {
   *     "deviceId": "575ecf887ae143cd83dc4aa2",
   *     "key": "this_would_be_the_key",
   *     "secret": "this_would_be_the_secret"
   *   }
   *
   */

  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["deviceId"] = LOSANT_DEVICE_ID;
  root["key"] = LOSANT_ACCESS_KEY;
  root["secret"] = LOSANT_ACCESS_SECRET;
  String buffer;
  root.printTo(buffer);

  int httpCode = http.POST(buffer);

  if(httpCode > 0) {
      if(httpCode == HTTP_CODE_OK) {
          Serial.println("This device is authorized!");
      } else {
        Serial.println("Failed to authorize device to Losant.");
        if(httpCode == 400) {
          Serial.println("Validation error: The device ID, access key, or access secret is not in the proper format.");
        } else if(httpCode == 401) {
          Serial.println("Invalid credentials to Losant: Please double-check the device ID, access key, and access secret.");
        } else {
           Serial.println("Unknown response from API");
        }
      }
    } else {
        Serial.println("Failed to connect to Losant API.");

   }

  http.end();

  // Connect to Losant.
  Serial.println();
  Serial.print("Connecting to Losant...");

  device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  while(!device.connected()) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("Connected!");
  Serial.println();
  Serial.println("This device is now ready for use!");
}

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

  // Wait for serial to initialize.
  while(!Serial) { }

  Serial.println("Device Started");
  Serial.println("-------------------------------------");
  Serial.println("Running DHT!");
  Serial.println("-------------------------------------");

  connect();
}

void report(double humidity, double tempC, double tempF, double heatIndexC, double heatIndexF) {
  StaticJsonBuffer<500> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["humidity"] = humidity;
  root["tempC"] = tempC;
  root["tempF"] = tempF;
  root["heatIndexC"] = heatIndexC;
  root["heatIndexF"] = heatIndexF;
  device.sendState(root);
  Serial.println("Reported!");
}

int timeSinceLastRead = 0;
void loop() {
   bool toReconnect = false;

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Disconnected from WiFi");
    toReconnect = true;
  }

  if (!device.connected()) {
    Serial.println("Disconnected from MQTT");
    Serial.println(device.mqttClient.state());
    toReconnect = true;
  }

  if (toReconnect) {
    connect();
  }

  device.loop();

  // Report every 10 seconds.
  if(timeSinceLastRead > 10000) {
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      timeSinceLastRead = 0;
      return;
    }

    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);

    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");
    report(h, t, f, hic, hif);

    timeSinceLastRead = 0;
  }
  delay(100);
  timeSinceLastRead += 100;
}

#4

This may be an obvious question. But, did you happen to set up the device attributes?


#5

Yes I followed the guide very closely I also deleted and started afresh to make sure I didnt miss anything:


#6

Sorry for the late reply on this! I’m going to run this myself now to double check the firmware.

Also, are you seeing data come up in the communication logs? You can view this on your application main page.


#7

No data in communication logs but showing device as connected. Serial monitor shows the data being sent through on serial port.

I have also been checking using request data export but no data there either.


#8

Huzzah! I think I know what it is. Check this out:

The underlying MQTT library doesn’t have configurable memory limits yet. The payload has tons of data. I’m going to update the article with this information too.


#9

Taron, I am using a NodeMCU so it is the same controller as in your getting started example. Everything is setup as per your blog. Has the MQTT library or something changed since you wrote the blog, if so, can we use the same version as you used when writing your blog?

There is one thing different is that I am using a DHT11 not DHT22 - could that be the cause of the different packet size. I do not have a DHT22 to check against, on the serial monitor the data looks like:
“Humidity: 64.00 % Temperature: 25.00 *C 77.00 *F Heat index: 25.23 *C 77.41 *F
Reported!”


#10

The two sensors look the same ( in terms of configuration ). However, this is a problem with a library the firmware is using. So, it’s not a problem with the code you have, just the library.

Here is the problem: MQTT has a payload memory limit. You are sending humidity, tempC, tempF, heatIndexC, and heatIndexF. The default memory limit is not enough to support all of these values. The error is also not apparent, which makes it hard to track.

You can fix this in two ways:

  1. So, you can only send the data you need. You can edit the void report function to seen one or two values.

  2. Or you can increase the memory limit:


#11

@Taron_Foxworth In deed that resolved the issue. Thank you, but please put a note for this in your article https://www.losant.com/blog/getting-started-with-the-esp8266-and-dht22-sensor it took me so much time debugging, i am luck to find this article. I almost gave up on this great losant.com!


#12

Hi Everyone!

I edited the PubSubClient.h and increased from 128 to 256 the parameter:
#define MQTT_MAX_PACKET_SIZE 256

Now I am getting events in the “Device Log” but it has a orange triangle exclamation mark saying:
One or more of your payload properties was rejected.
A provided attribute is not defined on the device, or the data type is incorrect.
Received Payload
“humidity”:39.9
“tempC”:23.9
“tempF”:75.02
“heatIndexC”:23.38715
“heatIndexF”:74.0973

When I setup the device attributes for the DHT22 I set them all to “Number” which was the default. Should I change it to “String” or ??

But the data IS getting through so not sure what is wrong…

Thanks!
Zac


#13

Can you please paste a screenshot of the actual device log?


#14

I’m thinking I probably setup my account wrong… Is there a way I can show you how I setup my account?
Also here are the particulars of the IDE: Windows 7 laptop, ArdIDE 1.8.5,

LosantEror


#15

That error indicates the attributes are not currently configured on the device. Can you open your device page and confirm that attributes are configured for each of those entries?

For example, the screenshot below has attributes for temperature and humidity. You’ll need to add attributes that exactly match the fields you’re reporting (humidity, tempC, tempF, heatIndexC, and heatIndexF). These are also case-sensitive, so make sure the capitalization also matches.


#16

OK hang on… Going to grab a snapshot…


#17

OOOHHH! I think I got it. I just typed in the names but did not pay attention to exact spelling and capitalization. lemme try that…

So these should be the actual variable names in the source code, right?


#18

IT WORKS!!!

Timestamp ISO Date humidity tempC tempF heatIndexC heatIndexF
1522080434826 2018-03-26T16:07:14.826Z 55.7 23.8 74.84 23.68971 74.6419
1522080437218 2018-03-26T16:07:17.218Z 55 24 75.2 23.89143 75.005
1522080439581 2018-03-26T16:07:19.581Z 54 24.1 75.38 23.97532 75.156
1522080441964 2018-03-26T16:07:21.964Z 53.3 24.2 75.56 24.06704 75.3211
1522080444350 2018-03-26T16:07:24.350Z 52.7 24.2 75.56 24.05137 75.2929
1522080446729 2018-03-26T16:07:26.729Z 52.2 24.3 75.74 24.14831 75.4674
1522080449118 2018-03-26T16:07:29.118Z 51.8 24.4 75.92 24.24787 75.6466
1522080451499 2018-03-26T16:07:31.499Z 51.4 24.4 75.92 24.23742 75.6278
1522080453883 2018-03-26T16:07:33.883Z 51.1 24.5 76.1 24.33959 75.8117
1522080456268 2018-03-26T16:07:36.268Z 50.8 24.5 76.1 24.33176 75.7976
1522080458653 2018-03-26T16:07:38.653Z 50.6 24.5 76.1 24.32653 75.7882
1522080466867 2018-03-26T16:07:46.867Z 50.9 24.6 76.28 24.44436 76.0003
1522080469243 2018-03-26T16:07:49.243Z 53.9 24.7 76.46 24.6327 76.3393
1522080471648 2018-03-26T16:07:51.648Z 53.2 24.7 76.46 24.61442 76.3064
1522080474001 2018-03-26T16:07:54.001Z 52.5 24.8 76.64 24.70614 76.4715

#19

Awesome! Typos and capitalization is a common issue. We might be able to detect some of these and be more helpful in the device log. We’ll brainstorm on that. Anyway, glad you got it working!


#20

Yes! I suggest saying something to the effect that these device attribute names should be the actual names found in the JsonObject used for the reporting of the data.