Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By DinoN
#44271 HI,

I am trying to get ESP-12 to go to deep sleep with DHT22 sensor attached.
Idea is that sensor node is accesible from browser first 5 minutes, and then after going to first deep sleep it just sends sensor updates

Code: Select all#include <PietteTech_DHT.h>
#include <Arduino.h>
#include <Time.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
#include <EEPROM.h>

// Include API-Headers
extern "C" {
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "cont.h"
}

//ADC_MODE(ADC_VCC); //vcc read-mode

// RTC-MEM Adresses
#define RTC_BASE 100

// state definietions
#define STATE_COLDSTART 0
#define STATE_SLEEP_WAKE 1
#define SLEEP_TIME 2*60*1000000                    // sleep intervalls in us
#define SLEEP_AFTER_MIN 2                          // go to sleep after minutes

// global variables
byte buf[2];
byte state;   // state variable
uint32_t sleepCount;

// User variables
String ClientIP;
float   temperature   = 0.0;
float   humidity    = 0.0;
long    lastInterval  = 0;
int     port = 80;
#define SERIAL_DEBUG


ESP8266WebServer server(80);

//DHT22 config
#define DHTPIN 5 // what pin DHT is connected to
#define DHTTYPE DHT22 // DHT 11
//declaration
void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

// globals
bool bDHTstarted;       // flag to indicate we started acquisition

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
  DHT.isrCallback();
}


void handle_root() {
}




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void send_data() {
 
  String yourdata;

  int acquireresult;
  acquireresult = DHT.acquireAndWait(2000);
  if ( acquireresult == 0 ) {

   
    // get data
    float t = DHT.getCelsius();
    float h = DHT.getHumidity();
    delay(10);
    Serial.println("Read from DHT sensor!");
    temperature = t;
    humidity    = h;

    HTTPClient http;
   
      h = h * 100;
      t = t * 100;
     
      char STRBUff[4];
      String StrHum;
      String StrTemp;

      dtostrf(h,4,0,STRBUff);
      StrHum = STRBUff;
      dtostrf(t,4,0,STRBUff);
      StrTemp = STRBUff;
     
      Serial.println(StrTemp);
      Serial.println(StrHum);
     
      // configure target server and url
      String url = ...
   
      String HTTPReq = "http://";
      HTTPReq += host;
      HTTPReq += url;
   
      Serial.println(HTTPReq);
      http.begin(HTTPReq);
   
      // start connection and send HTTP header
      int httpCode = http.GET();
      if(httpCode) {
          if(httpCode == 200 && httpCode < 300) {
            String payload = http.getString();
            Serial.println("Sent OK");
          }
      } else {
          Serial.println("Send failed!");
      }
 
  } else {
    Serial.println("Failed to read from DHT sensor!");
 
  }
 
}


void setup() {
    // put your setup code here, to run once:

#ifdef SERIAL_DEBUG
    Serial.begin(74880);
    delay(100);
    Serial.println();
    Serial.println();
    Serial.println("Started from reset");
#endif     
   
    delay(500);
    system_rtc_mem_read(RTC_BASE,buf,2); // read 2 bytes from RTC-MEMORY
    Serial.println(buf[0]);
    Serial.println(buf[1]);
   
     if ((buf[0] == 0x55) && (buf[1] == 0xaa))  // cold start, magic number is nor present
     {
        // WAKE UP START
        state = STATE_SLEEP_WAKE;           
        Serial.println("START - Wake up");
     }
     else                                       
     { 
        // COLD START
        state = STATE_COLDSTART;
        buf[0] = 0x55; buf[1]=0xaa;
        system_rtc_mem_write(RTC_BASE,buf,2);
        Serial.println("START - Cold start");
     }


    //WiFiManager
     //Local intialization. Once its business is done, there is no need to keep it around
     WiFiManager wifiManager;

           
     switch (state)
     { 
        case STATE_COLDSTART:   // first run after power on - initializes
        {
           
            //buf[0] = 0x55; buf[1]=0xaa;
            //system_rtc_mem_write(RTC_BASE,buf,2);
           
            //WiFiManager
            //Local intialization. Once its business is done, there is no need to keep it around
            //WiFiManager wifiManager;
           
            //fetches ssid and pass from eeprom and tries to connect/
            //if it does not connect it starts an access point with the specified name
            //here  "AutoConnectAP"
            //and goes into a blocking loop awaiting configuration
            wifiManager.autoConnect("IoT Autoconnect");
            //or use this for auto generated name ESP + ChipID
            //wifiManager.autoConnect();
           
            //if you get here you have connected to the WiFi
            Serial.println("connected to AP... success !");
       
            // set up HTTP SERVER
            server.on("/", handle_root);
            server.begin();
            Serial.println("HTTP server started");
        break;
        }
       
        case STATE_SLEEP_WAKE:
        { 
            //WiFiManager
            //Local intialization. Once its business is done, there is no need to keep it around
            //WiFiManager wifiManager;
           
            //fetches ssid and pass from eeprom and tries to connect/
            //if it does not connect it starts an access point with the specified name
            //here  "AutoConnectAP"
            //and goes into a blocking loop awaiting configuration
            wifiManager.autoConnect("IoT Autoconnect");
            //or use this for auto generated name ESP + ChipID
            //wifiManager.autoConnect();
       
            //if you get here you have connected to the WiFi
            Serial.println("connected to AP... success !");
         
        break;
        }
     } 
}

void loop() {
    if (state == STATE_COLDSTART)
    {
        // put your main code here, to run repeatedly:
       
        if (millis() - lastInterval > sendInterval) {
          send_data();
          lastInterval = millis();
        }
   
        server.handleClient();

        if (minute() > SLEEP_AFTER_MIN)
        {
           
            wifi_station_disconnect();
            wifi_set_opmode(NULL_MODE);      //set wifi mode to null mode.
            wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);      //set force sleep type, clsoe rf&cpu
            wifi_fpm_open();            //enable force sleep fucntion


            Serial.println("GOING FOR DEEP SLEEP");
            ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT);
            delay(1000);                                            // pass control back to background processes to prepare sleep 
        }
    }

    if (state == STATE_SLEEP_WAKE)
    {
        // put your main code here, to run repeatedly:
        delay(2000);
        send_data();

        Serial.println("GOING FOR DEEP SLEEP");
        ESP.deepSleep(10000000, WAKE_RF_DEFAULT);
        delay(10000);                                               // pass control back to background processes to prepare sleep
    }

}



Writting to RTC memory simply does not work. Every time it wakes up as if full reset was done. Can it be because of server() and WifiManager class.
Do I need to turn something off before entering deep sleep. I am using

wifi_station_disconnect();
wifi_set_opmode(NULL_MODE); //set wifi mode to null mode.
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); //set force sleep type, clsoe rf&cpu
wifi_fpm_open(); //enable force sleep function

but still no luck.

This is what I get on serial interface.

Code: Select all ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1264, room 16
tail 0
chksum 0x42
csum 0x42
~ld


Started from reset
0
0
START - Cold start
*WM:
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Using last saved values, should be faster
*WM: Connection result:
*WM: 3
*WM: IP Address:
*WM: 192.168.xxx.xxx
connected to AP... success !
HTTP server started
pm open,type:2 0
User avatar
By DinoN
#44273 HI,

something is linked to WifiManager class and server running.

It should be able to write to memory location (I am using 100 here)

but if I leave it running for some time it crashes

Code: Select allSent OK
force slp enable,type: 1
pm close 7
fpm open,type:1 0
GOING FOR DEEP SLEEP
state: 5 -> 0 (0)
rm 0
del if0
usl
Fatal exception 2(InstructionRetchErrorCause):
epc1=0x3ffea4f9, epc2=0x00000000, epc3=0x00000000, excvaddr=0x3ffea4f8, depc=0x00000000

Exception (2):
epc1=0x3ffea4f9 epc2=0x00000000 epc3=0x00000000 excvaddr=0x3ffea4f8 depc=0x00000000

ctx: sys
sp: 3ffffc80 end: 3fffffb0 offset: 01a0

>>>stack>>>
3ffffe20:  3ffefd20 4010486e 3ffefd20 00000018 
3ffffe30:  4021a23d 3ffffed0 00000000 4000050c 
3ffffe40:  40104e19 3ffefd20 3ffef478 00000000 
3ffffe50:  4010432c 00040000 7fffffff 00000000 
3ffffe60:  00000022 40104329 00040000 ffffffff 
3ffffe70:  3ffefd20 00000004 3ffffed0 3ffffee1 
3ffffe80:  401073f8 00000000 3fff4268 3fff15a0 
3ffffe90:  60000600 00000000 000000a5 40105ec5 
3ffffea0:  4000050c 00000000 00000000 3ffffe30 
3ffffeb0:  40000f68 00000030 0000001e ffffffff 
3ffffec0:  3ffffee0 3ffffed0 00000004 00000000 
3ffffed0:  3ffeff50 3ffea5f5 3fff4268 00000009 
3ffffee0:  000000a5 00000000 0000001e 3fffdab0 
3ffffef0:  00000000 3fffdc00 3ffeff38 4010031d 
3fffff00:  00000000 00000000 3fff3f9c 40100426 
3fffff10:  3ffeddfa 3ffede16 00000000 40100466 
3fffff20:  3ffeb9d4 40105f97 3fff3b7c 401008bc 
3fffff30:  000005e0 00000000 3fff3b7c 401075f0 
3fffff40:  4022c37a 3fff3b7c 00000000 4022c38b 
3fffff50:  4022e4b6 3fff3b7c 3fff3b7c 3ffefd20 
3fffff60:  4022c6e7 3fff1480 000002c8 4010020c 
3fffff70:  4022c4a5 3fff1474 3fff1430 0ababc69 
3fffff80:  4022c4ea 3fffdab0 00000000 3fffdcb0 
3fffff90:  3fff1450 00000000 3fff508c 4022f7f6 
3fffffa0:  40000f49 40000f49 3fffdab0 40000f49 
<<<stack<<<
"@.rjrA(!.S.u.....X.Q.~..Q*!.UK....I*E.1...1.)D.......A.!..n.*E"HhinA,T[M..A,T[..Q.

Started from reset
0
0
START - Cold start
*WM:
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Using last saved values, should be faster
*WM: Connection result:
*WM: 3
*WM: IP Address: