Can someone please help a noob with a Deep Sleep code?
Posted: Wed Jan 22, 2020 12:17 pm
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
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