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
#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.
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