-->
Page 1 of 4

Can someone please help a noob with a Deep Sleep code?

PostPosted: Wed Jan 22, 2020 12:17 pm
by hackerjoe
Hi all,

I have found this code online and can't find the author.
It works great for up to 30 minutes of sleep but I would like go an hour. Its in seconds
I did try and increase it from 1800 to 3600 seconds ( in code marked in Red ) but it never reboots and come out of sleep I guess.
Am I missing something? I know the timing is supposed to be in microseconds so if I changed it from 1800 ( in red ) seconds to 3.6e+6 and delete *1000000 from the deepsleep line ( in blue ) would that do anything?
There is a line in the code that has something with deepsleep but I have no idea what it references. ( Ok I think the Const in is the time designation )
Do I need to add a resistor to the reset circuit as well? I have been reading posts where people said it may be needed.
I appreciate any help or suggestions.
Thank you,

Here is the code:

/* DHT22 temperature and humidity data are uploaded to thingspeak using
* WeMOS ESP8266 MCU.
*
* Details: http://embedded-lab.com/blog/iot-temper ... ty-logger/
*/
#include <ESP8266WiFi.h>
#include <DHT.h>
#include "Math.h"
#define DHTTYPE DHT11
#define DHTPIN D2 // DHT22 data pin connects to D4

// Time to sleep (in seconds):
const int sleepTimeS = 1800;

DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266


float prevTemp = 0;
const char* server = "api.thingspeak.com";
String apiKey ="****************";
const char* MY_SSID = "*************";
const char* MY_PWD = "***************";
int sent = 0;
void setup() {
Serial.begin(115200);
dht.begin();
connectWifi();

}

void loop() {
float t1,humidity;
//char buffer[10];
humidity = dht.readHumidity(); // Read humidity (percent)
t1 = dht.readTemperature(true); // Read temperature as Fahrenheit
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(t1)) {
Serial.println("Failed to read from DHT sensor!");
return;
}


Serial.print(String(sent)+" Temperature: ");
Serial.println(t1);
Serial.print(String(sent)+" Humidity: ");
Serial.println(humidity);
sendTeperatureTS(t1, humidity);
ESP.deepSleep(sleepTimeS*1000000, WAKE_RF_DEFAULT); // Sleep for 60 seconds
}

void connectWifi()
{
Serial.print("Connecting to "+*MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}

Serial.println("");
Serial.println("Connected");
Serial.println("");
}//end connect

void sendTeperatureTS(float temp1, float temp2)
{
WiFiClient client;

if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
Serial.println("WiFi Client connected ");

String postStr = apiKey;
postStr += "&field7=";
postStr += String(temp1);
postStr += "&field8=";
postStr += String(temp2);
postStr += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
delay(1000);

}//end if
sent++;
client.stop();
}//end send

Re: Can someone please help a noob with a Deep Sleep code?

PostPosted: Wed Jan 22, 2020 7:04 pm
by hackerjoe
Ok I have tried a few different things and nothing helps getting it to sleep for 1 hour.
The current one get 30 minutes but that's it.
I'd like to extend that in order to save further on batteries.

Re: Can someone please help a noob with a Deep Sleep code?

PostPosted: Wed Jan 22, 2020 8:17 pm
by davydnorris
That code should work ok. You will need to do two things:

- increase the value to 3600 for an hour of sleep
- connect the RST to GPIO16 using a Schotty diode. A wire works but will prevent auto flashing from working, and resistors will have mixed results depending on the state of your battery. A diode gets the most consistent results while still allowing you to auto flash your chip.

The the following topic: https://www.esp8266.com/viewtopic.php?p=73516

Re: Can someone please help a noob with a Deep Sleep code?

PostPosted: Wed Jan 22, 2020 10:09 pm
by hackerjoe
davydnorris wrote:That code should work ok. You will need to do two things:

- increase the value to 3600 for an hour of sleep
- connect the RST to GPIO16 using a Schotty diode. A wire works but will prevent auto flashing from working, and resistors will have mixed results depending on the state of your battery. A diode gets the most consistent results while still allowing you to auto flash your chip.

The the following topic: https://www.esp8266.com/viewtopic.php?p=73516

Thank you for the suggestions !
I have increased the value in the past without any luck. A half hour is all it would cycle at.
I jumped D0 and RST without a resistor or diode by the way and thought that had something to do with it.
I have a bunch of diodes left over from some recent projects and may have the type you mentioned.
That's a great idea by the way, what direction should the diode go?
I did change the script just before you posted and I may have gotten it to work.
I might have gotten the ESP to sleep for an hour by of course increasing the value to 3600 which I knew about but with 2 other changes and it was adding ul to the line below and spacing the asterisk:
ESP.deepSleep(sleepTimeS * 1000000ul, WAKE_RF_DEFAULT); // Sleep for 60 seconds

Thank you again - I'm going to let this code run overnight and if it works I'm going to reflash my 4 temp sensors over the weekend and hopefully the batteries will last a lot longer seeing I will have deep sleep working for an hour.
I'll add a diode to the sensor I'm testing with and to my 4 working sensors.