Dht22 / cayenne

Hello everyone,

My DHT22 mount works with my nodemcu however I do not know how to send my data to cayenne via the cayennewrite command.
I have in the console serie the temperature in celcius and humidity in%

Thanks for your help.

My code

#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);


// 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";

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

I have a project here that should help you out. Itā€™s for a DHT11, but where you get the temp/hum values is irrelevant.

1 Like

@adam :raised_hands: and for more about virtual.write have a look at this Data types for Cayenne MQTT API

Hello Adam and thank you
It works correctly I just changed to Celsius because I am French.
On the other hand how does the function deepsleep very interesting in my project to reduce consumption.
What is the principle?
How to reduce the duration from 10 minutes to 5 minutes?
Thank you

OK thankā€™s

in @adam code change

//Time to sleep - 10 minutes - delete a zero to set to 1 minute
int SleepTime = 600000000; 

to

// for 5 min
int SleepTime = 300000000;
1 Like

Thinkā€™s

After the test i havenā€™t receive in CAYENNE my data. In my serial console i have this text

[21936] Connecting to mqtt.mydevices.com:1883
[22035] Network connect failed
[23318] Connected
Humidity: 71.70
Temperature (c): 24.60
Heat Index (c): 20.13
Sent all values - sleeping
āø®^T^āø®āø®yāø®

can you post your code.

This is the code

// 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[] = "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 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;
  }
}

Looks right to me. If you are seeing ā€œSent all values - sleepingā€ in your serial output then the values were sent. Are you looking at the correct device on your dashboard?

uncomment this and check your serial.

I am looking the good device.
I thinks the time is very short to sent data.
My device is online and offline when the esp sleep but i have no data.

I have uncomment the line debug

what is your serial monitor output now?

The serial monitor

[23316] Connected
[23468] Publish: topic 4, channel 65534, value NodeMCU, subkey , key
[23469] Publish: topic 6, channel 65534, value Xtensa32, subkey , key
[23485] Publish: topic 7, channel 65534, value 80000000, subkey , key
[23560] Publish: topic 5, channel 65534, value 1.2.0, subkey , key
[23632] Publish: topic 8, channel 65534, value ESP8266, subkey , key
Humidity: 71.00
Temperature (c): 24.60
Heat Index (c): 20.10
[26485] Publish: topic 1, channel 0, value 71.000, subkey p, key rel_hum
[26486] Publish: topic 1, channel 1, value 24.600, subkey c, key temp
[26504] Publish: topic 1, channel 2, value 20.097, subkey c, key temp
Sent all values - sleeping

in my cayenne

cayenne

you are getting value. what more do you want?

There is no temperature and humidity.

Now i have in my serial console

Capture3

In my cayenne

Capture2

do one thing just comment out this code:

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

and add all the widget to your dashboard. once done uncomment it again and see what result you get.

According to this your device sent all the data Cayenne needs. @shramik_salgaonkar are there any MQTT bugs at the moment?

Another suggestion I have is to hit F12 in your browser and go to the console tab. Send the data and see if you are getting any browser errors that would prevent the other values from being displayed.