-->
Page 1 of 3

ESP8266-01 stops with delay > 21000

PostPosted: Thu Dec 03, 2015 12:02 pm
by Petervdpol
I have hooked up a DHT11 with a ESP8266-01 The ESP sends the data to my Raspberry with MQTT. I use the Arduino IDE for programming. Initially I had the delay in the loop on 5000 (5 seconds) and all works fine. However: I do not need that much data; a reading every two minutes or so would be wonderful and more then enough to create nice charts. So I upped the Delay() in the void loop section and the ESP stops working after sending one message at boot-up when the power is reconnected. So I started with 30000 mil sec, didn't work. I have narrowed it down to 21000 mil sec (21 seconds) which works fine but still way to much data for my liking... What am I doing wrong?

My sketch:

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.XXX.XXX";
String clientName = "esp8266-1";

#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT11 // 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);
}
}
++counter;
delay(21000);
}

Re: ESP8266-01 stops with delay > 21000

PostPosted: Thu Dec 03, 2015 12:24 pm
by Mario Mikočević
Try this ->

Code: Select all#include <Ticker.h>
Ticker tickerSensorScan;
#define WAITTIME 300
boolean tickerFired;

void flagSensorScan( void ) {
  tickerFired = true;
}

void setup() {
  tickerSensorScan.attach( WAITTIME, flagSensorScan );
  tickerFired = true;
}

void loop() {
  if( tickerFired ) {
    tickerFired = false;
    doSomethingWithSensor();
  }
}

Re: ESP8266-01 stops with delay > 21000

PostPosted: Thu Dec 03, 2015 12:34 pm
by Venkatesh
Or you can try this arduino library (SimpleTimer). I used this to read DHT-11 sensor value every 3 minutes and updated TS.

Re: ESP8266-01 stops with delay > 21000

PostPosted: Fri Dec 04, 2015 3:54 am
by zoomx
delay() has limits or not?