ESP8266-01 + DHT sensor longer delay problem
Posted: Fri Dec 25, 2015 11:14 am
Mighty Men and Women; please help a non-programmer out here:
I want to add a DHT22 sensor node to my Openhab setup. IMHO a simple way to do this is by using a ESP8266-01; I have a few around. I use the Arduino 1.6.5 IDE since that is what I have. I am having problems with the timing of the loop: I do not need readings every few seconds, minute or so, 5-10 minutes are more then enough; I do not need that much data. I also want to run the node on batteries in the future so less transmissions is what I want to save power. However, when I use a delay of more then approx. 24000 mils the ESP stops working, lower values are no problem.
I have tried several approaches, none work. Can anyone help me out?
The sketch I use is below, you can see what I tried in the comments:
I want to add a DHT22 sensor node to my Openhab setup. IMHO a simple way to do this is by using a ESP8266-01; I have a few around. I use the Arduino 1.6.5 IDE since that is what I have. I am having problems with the timing of the loop: I do not need readings every few seconds, minute or so, 5-10 minutes are more then enough; I do not need that much data. I also want to run the node on batteries in the future so less transmissions is what I want to save power. However, when I use a delay of more then approx. 24000 mils the ESP stops working, lower values are no problem.
I have tried several approaches, none work. Can anyone help me out?
The sketch I use is below, you can see what I tried in the comments:
Code: Select all
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <DHT.h>
const char* ssid = "Yggdrasil";
const char* password = "XXXXXXXXX";
char* topic_t = "openhab/esp8266-1/temp";
char* topic_h = "openhab/esp8266-1/vocht";
char* server = "192.168.X.X";
String clientName = "esp8266-1";
//time peter, idea taken from https://www.safaribooksonline.com/library/view/arduino-cookbook-2nd/9781449321185/ch12.html
const long oneSecond = 1000; // a second is a thousand milliseconds
const long oneMinute = oneSecond * 60;
const long fiveMinutes = oneMinute * 5;
//time peter
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 11
DHT dht(DHTPIN, DHTTYPE,15);
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
void setup() {
Serial.begin(115200);
delay(10);
dht.begin();
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);
if (client.connect((char*) clientName.c_str())) {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic_t);
Serial.println(topic_h);
}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
static int counter = 0;
String payload ;
payload += t;
//payload += ":";
//payload += h;
String payloadh ;
payloadh += h;
if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payload);
if (client.publish(topic_t, (char*) payload.c_str()))
{
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}
//peter
if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payloadh);
if (client.publish(topic_h, (char*) payloadh.c_str()))
{
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}}
//peter
}
else {
if (client.connect((char*) clientName.c_str())) {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic_t);
Serial.println(topic_h);
}
}
// delay (20000); //WORKS FINE
// delay(5*60*1000); //STOPS AFTER FIRST READING AT STARTUP
delay(fiveMinutes); // STOPS AFTER FIRST READING AT STARTUP
}