Dht22 / cayenne

In my serial console

[21453] Connecting to mqtt.mydevices.com:1883
[21921] Connected
[22102] Publish: topic 4, channel 65534, value NodeMCU, subkey , key
[22103] Publish: topic 6, channel 65534, value Xtensa32, subkey , key
[22119] Publish: topic 7, channel 65534, value 80000000, subkey , key
[22194] Publish: topic 5, channel 65534, value 1.2.0, subkey , key
[22266] Publish: topic 8, channel 65534, value ESP8266, subkey , key
Humidity: 71.80
Temperature (c): 24.70
Heat Index (c): 20.24
[25119] Publish: topic 1, channel 0, value 71.800, subkey p, key rel_hum
[25120] Publish: topic 1, channel 1, value 24.700, subkey c, key temp
[25138] Publish: topic 1, channel 2, value 20.245, subkey c, key temp
[34236] Connection ok
[40257] Publish: topic 1, channel 0, value 71.800, subkey p, key rel_hum
[40258] Publish: topic 1, channel 1, value 24.700, subkey c, key temp
[40276] Publish: topic 1, channel 2, value 20.245, subkey c, key temp
[44362] Connection ok
[54390] Connection ok
[55394] Publish: topic 1, channel 0, value 71.800, subkey p, key rel_hum
[55395] Publish: topic 1, channel 1, value 24.700, subkey c, key temp
[55412] Publish: topic 1, channel 2, value 20.245, subkey c, key temp
[64513] Connection ok
[70531] Publish: topic 1, channel 0, value 71.800, subkey p, key rel_hum
[70532] Publish: topic 1, channel 1, value 24.700, subkey c, key temp
[70549] Publish: topic 1, channel 2, value 20.245, subkey c, key temp
[74638] Connection ok
[84673] Connection ok
[85677] Publish: topic 1, channel 0, value 71.800, subkey p, key rel_hum
[85678] Publish: topic 1, channel 1, value 24.700, subkey c, key temp
[85695] Publish: topic 1, channel 2, value 20.245, subkey c, key temp
[94801] Connection ok
[100821] Publish: topic 1, channel 0, value 71.800, subkey p, key rel_hum
[100822] Publish: topic 1, channel 1, value 24.700, subkey c, key temp
[100842] Publish: topic 1, channel 2, value 20.245, subkey c, key temp
[104928] Connection ok
[114961] Connection ok

in my cayenne

Capture

But i have a problem the result don’t move. It s always the same value and i change the temperature and the humidity.

71.8
24.7
20.2

how were you changing the humidity? the serial monitor shows what is sent to cayenne and that is shown in the dashboard. now add the sensor to the dashboard by clicking on “+”. if it value is not changing then it may be the problem with sensor.

@adam all good with MQTT :+1:

The result don’t move in the serial console and in the cayenne web console.

it may be problem with your sensor. try removing all the cayenne code and test the sensor alone.

The program is designed to send one set of values, sleep, then send another after x time. If your program has set ReadError to false (which from your output it has) it will never send anything other than those values since it’s not going to read the sensor again. After the device sleeps and wakes again all values in memory are lost and it starts over. Since you commented that part out it will never reset itself. Uncomment that part again and it should work.

@adam he has all things correct, he has not clicked on “+” to add the sensor to dashboard.

If uncomment

else {
_ //Check if we sent all values and sleep for 10 minutes if we did_
_ if (CayenneSent){_
_ Serial.println(“Sent all values - sleeping”);_
_ delay(100);_
_ ESP.deepSleep(SleepTime, WAKE_RF_DEFAULT);_
_ delay(100);_
_ }_

i have the original probleme

Capture

My sensor is ok

with this all is allright in my serial console

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

#include "DHT.h"
#define DHTPIN 4     // what digital pin the DHT22 is conected to
#define DHTTYPE DHT22   // there are multiple kinds of DHT sensors

DHT dht(DHTPIN, DHTTYPE);


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("-------------------------------------");

}

int timeSinceLastRead = 0;
void loop() {

  // Report every 2 seconds.
  if(timeSinceLastRead > 2000) {
    // 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");

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

Capture2

I’m not sure what the problem is, your serial output says it has sent all values. Have you tried clicking the green + on the widget and let it run for a while then check if everything was received? As I said, with the sleep part commented out it will never get new values from your sensor.

There are no comments with this version

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. 

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define DHTTYPE DHT22
#define DHTPIN  4

#include <CayenneMQTTESP8266.h>
#include <DHT.h>

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

//Variables for DHT11 values
float h, t, hif;
//Time to sleep - 5 minutes - delete a zero to set to 1 minute
int SleepTime = 300000000;
//Values sent/error booleans
bool CayenneSent = false;
bool ReadError = false;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("Setup");
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

  CayenneSent = false;
  ReadError = true;
}

void loop() {
  //Run Cayenne Functions
  Cayenne.loop();

  //Only check values if there is a ReadError 
  if (ReadError){
    ReadError = false;
    //Check if read failed and try until success
    int WhileLoops = 0;
    do {
      WhileLoops++;
      //We can't go too long without calling Cayenne.loop() so exit this loop after 2 seconds and set an error flag
      if (WhileLoops >= 4){
        Serial.println("Sensor Read Error");
        ReadError = true;
        break;
      }
      //Read temperature as Celsius
      t = dht.readTemperature();
      //Read humidity (percent)
      h = dht.readHumidity();
      //Read temperature as Celsius
      t = dht.readTemperature();
      //Calculate Heat Index as Celsius
      hif = dht.computeHeatIndex(t, h);
  
      delay(500);
    } while  (isnan(t) || isnan(h));

    Serial.print("Humidity: ");
    Serial.println(h);
    Serial.print("Temperature (c): ");
    Serial.println(t);
    Serial.print("Heat Index (c): ");
    Serial.println(hif);
  }
  else {
    //Check if we sent all values and sleep for 10 minutes if we did
    if (CayenneSent){
      Serial.println("Sent all values - sleeping");
      delay(100);
      ESP.deepSleep(SleepTime, WAKE_RF_DEFAULT);
      delay(100);
    }
  }
}
    
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
  //Check if there was a read error and skip sending values if there is
  if (!ReadError){
    //Send Values
    Cayenne.virtualWrite(0, h, "rel_hum", "p");
    Cayenne.virtualWrite(1, t, "temp", "c");
    Cayenne.virtualWrite(2, hif, "temp", "c");

    //Set CayenneSent to true so we know when to sleep
    CayenneSent = true;
  }
}

did you tried this?

where is the green +

image

Ah OK
But i don’t have this nows
I have uncomments the line

else {
_ //Check if we sent all values and sleep for 10 minutes if we did_
_ if (CayenneSent){_
_ Serial.println(“Sent all values - sleeping”);_
_ delay(100);_
_ ESP.deepSleep(SleepTime, WAKE_RF_DEFAULT);_
_ delay(100);_
_ }_

i have the original probleme

Capture

I don’t have the widjet in cayenne console and i don’t have the value in cayenne console

Capture

okay try adding the widget “+” first without the sleeping code and add again the sleeping code.

Every time you refresh or navigate away from that page your widgets get reset unless you click the green +. You have to be on that dashboard when you power on the device and wait for it to add the widgets.

Hello,

With this code everything is OK the values ​​are good in the serial console and in the cayenne interface. I simply removed the sleeping part. However, this part of standby interests me.
To explain the objective of this project is to install on a remote sailboat various sensors. These will be fed by the battery of the edge.
I do not understand how to put the program to sleep to save the batteries.

CODE

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define DHTTYPE DHT22
#define DHTPIN 4

#include <CayenneMQTTESP8266.h>
#include <DHT.h>

// WiFi network info.
char ssid = “XXX”;
char wifiPassword = “XXX”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “XXX”;
char password = “XXX”;
char clientID = “XXX”;

//Variables for DHT22 values
float h, t, hif;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println(“Setup”);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

}

void loop() {
//Run Cayenne Functions
Cayenne.loop();

  //Read temperature as Celsius
  t = dht.readTemperature();
  //Read humidity (percent)
  h = dht.readHumidity();
  //Read temperature as Celsius
  t = dht.readTemperature();
  //Calculate Heat Index as Celsius
  hif = dht.computeHeatIndex(t, h);

  delay(500);


Serial.print("Humidity: ");
Serial.println(h);
Serial.print("Temperature (c): ");
Serial.println(t);
Serial.print("Heat Index (c): ");
Serial.println(hif);


Cayenne.virtualWrite(0, h, "rel_hum", "p");
Cayenne.virtualWrite(1, t, "temp", "c");
Cayenne.virtualWrite(2, hif, "temp", "c");

}

SERIAL CONSOLE

OVERVIEW

overview

DATA

first add channel 1 and 2 by clicking “+” to your dashboard.
then use the code of sleeping by @adam and all should be good to go.

I followed the requested procedure but still the same problem.

  • OK value in the serial console
  • OK value in cayenne
  • Implementation of the new code (adam) with the setting to sleep

Verification

Sleep nodemcu after the set time the nodemcu wakes up with the blue LED and the console learns the message below.
In the interface CAYENNE no change always the values ​​before the modification of the code.

console

change the sleep time to 1 min and upload the code. this will give out some value on the serial monitor and upload it to cayenne and go to sleep. now place the sensor near to the air conditioner or some place which is hot. this will give you the change in the temp and will show on the the serial monitor. do this test for 3-4 times and post the serial monitor output here.