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;
}
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
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āø®
// 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?
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.