-->
Page 1 of 1

ESP8266 stuck

PostPosted: Sat Apr 08, 2017 4:07 am
by drekthral
Hello community,

I made code that connects to my WiFi, reads temperature from DHT22, reads battery voltage, sends to my server that save all data to database and then in my website draws temperature graf of all my ESPs sensors, when it connect to server, server return number- time how long should ESP remain in deepsleep (everything can be set from my website), then it go to deep sleep and the process is repeated.

My problem is that sometimes- for example after one day of measuring the ESPs get stuck and I have to go where I placed them and reset them. Then they are sending data for eample another two days and again get stuck.

Also sometimes reading from DHT sensor return "nan" I dont know why so I put in code condition if so then restart and try again.

Thanks for any advice!

HW:
-Wemos D1 mini
-Wemos DHT Shield
-Wemos Battery Shield

-Pin D0 is connected to RST (wake from deep sleep)
-100k Ω resistor connected from + on battery shield connector to A0 (measuring approximately
battery voltage)

Code:
Code: Select all#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include "DHT.h"
#define USE_SERIAL Serial
#define DHTPIN 2
#define DHTTYPE DHT22


ESP8266WiFiMulti WiFiMulti;

DHT dht(DHTPIN, DHTTYPE);

//ESP sensor ID - every ESP have different ID (1-6)
int ID = 3;

unsigned int raw=0;
float volt=0.00;
float sleepTime;

 String host = "http://www.xxxxxxxxxxx.xx/esp8266.php?temp=";

void setup() {
    pinMode(BUILTIN_LED, OUTPUT);
    pinMode(A0, INPUT);
    USE_SERIAL.begin(115200);

    WiFiMulti.addAP("XXXXX", "xxxxxx"); // connect to wifi

    dht.begin();
}

void loop() {
    USE_SERIAL.println("starting loop");
    digitalWrite(BUILTIN_LED, HIGH);
 
    // wait for WiFi connection
    USE_SERIAL.print("connecting:");
   
    while((WiFiMulti.run() != WL_CONNECTED)) {
        digitalWrite(BUILTIN_LED, LOW);  // control LED if connecting
        USE_SERIAL.print(".");
        delay(100);
      }
    if((WiFiMulti.run() == WL_CONNECTED)) {
        digitalWrite(BUILTIN_LED, HIGH);  // control led if connected
        HTTPClient http;
       
        USE_SERIAL.println();
        USE_SERIAL.print("[HTTP] begin...\n");

        String temp = String(dht.readTemperature());
        delay(1500);
        //sometimes reading from sensor fail and return "nan" ... if so, then go to short deepsleep and repeat process until sensor return number
        if(temp == "nan"){
            ESP.deepSleep(5000000,WAKE_RF_DEFAULT); //5s
          }
         
        raw = analogRead(A0);
        volt=raw/1023.0;
        volt=volt*4.2;

        host += temp;
        host += "&id=";
        host += ID;
        host += "&bat=";
        host += volt;
       
        http.begin(host);

        USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

          //server return how long will ESP remain in deep sleep
            if(httpCode == 200) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
                int openBracket = payload.indexOf('?');
                int closeBracket = payload.indexOf('!');
                String timeToSleep = payload.substring(openBracket + 1, closeBracket);
                USE_SERIAL.println(timeToSleep);
                sleepTime = timeToSleep.toFloat();
                sleepTime *= 60000000;
            }

       
        } else {
     //       USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }
    USE_SERIAL.println("going to sleep in 0.5sec");
    delay(500);
    USE_SERIAL.print("now sleeping for:");
    USE_SERIAL.print(sleepTime / 1000000);
    USE_SERIAL.print("seconds");
    ESP.deepSleep(sleepTime,WAKE_RF_DEFAULT);
    delay(100);
}


Re: ESP8266 stuck

PostPosted: Mon Apr 10, 2017 5:45 am
by drekthral
Looks like sometimes ESP can't get response from server so sleepTime is not defined and ESP remain in deep sleep forever (?)

Repaired with this code:

Code: Select allif(httpCode == 200) {}
else{
sleepTime = 1 * 60000000; // one minute
}