Hi
I am trying to get my ESP to sleep for 30mins and then upload data to my server.
I have followed both of these Tutorials
ESP32 Publish Sensor Readings to Google Sheets (ESP8266 Compatible)
Visualize Your Sensor Readings from Anywhere in the World (ESP32/ESP8266 + MySQL + PHP)
And adjusted the code to suit my needs.
When I put the code in for sleep I’m not getting any readings on my database now
[code]
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include “DHT.h”
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
// Replace with your network credentials
const char* ssid = “”;
const char* password = “”;
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = “”;
// Keep this API Key value to be compatible with the PHP code provided in the project page.
// If you change the apiKeyValue value, the PHP file /post-data.php also needs to have the same key
String apiKeyValue = “”;
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define ONE_WIRE_BUS 15 // Data wire is connected to GPIO15
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = { 0x28, 0x6A, 0x48, 0x7C, 0x0C, 0x00, 0x00, 0xC4 };
DeviceAddress sensor2 = { 0x28, 0xFA, 0x14, 0x7B, 0x0C, 0x00, 0x00, 0x47 };
DHT dht(DHTPIN, DHTTYPE);
// Time to sleep
uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds
// sleep for 30 minutes = 1800 seconds
uint64_t TIME_TO_SLEEP = 10;
// Get Sensor Readings
String getSensorReadings(){
sensors.requestTemperatures();
}
void setup() {
Serial.begin(9600);
sensors.begin();
dht.begin();
Serial.println(“Starting…”);
WiFi.begin(ssid, password);
Serial.println(“Connecting”);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.print(“Connected to WiFi network with IP Address: “);
Serial.println(WiFi.localIP());
#ifdef ESP32
// enable timer deep sleep
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println(“Going to sleep now”);
// start deep sleep for 3600 seconds (60 minutes)
esp_deep_sleep_start();
#else
// Deep sleep mode for 3600 seconds (60 minutes)
Serial.println(“Going to sleep now”);
ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
#endif
}
void loop(){
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);
// Prepare your HTTP POST request data
String httpRequestData = “api_key=” + apiKeyValue + “&value1=” + String(dht.readTemperature())
+ “&value2=” + String(dht.readHumidity()) + “&value3=” + String(sensors.getTempC(sensor1)) + “&value4=” + String(sensors.getTempC(sensor2))+””;
Serial.print(“httpRequestData: “);
Serial.println(httpRequestData);
// You can comment the httpRequestData variable above
// then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
//String httpRequestData = “api_key=XXXXX&value1=24.75&value2=49.54&value3=1005.14”;
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: text/plain
//http.addHeader(“Content-Type”, “text/plain”);
//int httpResponseCode = http.POST(“Hello, World!”);
// If you need an HTTP request with a content type: application/json, use the following:
//http.addHeader(“Content-Type”, “application/json”);
//int httpResponseCode = http.POST(“{\”value1\”:\”19\”,\”value2\”:\”67\”,\”value3\”:\”78\”}”);
if (httpResponseCode>0) {
Serial.print(“HTTP Response code: “);
Serial.println(httpResponseCode);
}
else {
Serial.print(“Error code: “);
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println(“WiFi Disconnected”);
}
//Send an HTTP POST request every 30 seconds
delay(30000);
// Wait a few seconds between measurements.
delay(20000);
//Serial.print(“Requesting temperatures…”);
sensors.requestTemperatures(); // Send the command to get temperatures
// Serial.println(“DONE”);
Serial.print(“8 Frame Hive: “);
Serial.print(sensors.getTempC(sensor1));
Serial.print(“°C: “);
Serial.print(“10 Frame Hive: “);
Serial.print(sensors.getTempC(sensor2));
Serial.print(“°C: “);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F(“Failed to read from DHT sensor!”));
return;
}
Serial.print(F(“8 Frame Roof Temp: “));
Serial.print(t);
Serial.print(F(“°C “));
Serial.print(F(“8 Frame Roof Humidity: “));
Serial.print(h);
Serial.print(F(“% “));
Serial. print(‘\n’);
}
[/code]
Thanks in advance
Hi.
What happens is that you’re putting the ESP in deep sleep before posting data to the server.
With your current code, it wakes up, goes through the setup(), in the setup() there are the instructions for deep sleep, so it goes to sleep and never reaches the loop().
Call the following line, after sending the data to the server, and cut it from the setup().:
ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Let me know if this is clear.
Regards,
Sara
Hi Sara
Can you give me a little more detail. As in which lines to delete and add and where etc.
Thank you so much
Hi again.
In your setup(), delete the following line:
esp_deep_sleep_start();
Then, add that same line at the end of your loop():
..... Serial.print(F(“8 Frame Roof Temp: “)); Serial.print(t); Serial.print(F(“°C “)); Serial.print(F(“8 Frame Roof Humidity: “)); Serial.print(h); Serial.print(F(“% “)); Serial. print(‘\n’); esp_deep_sleep_start(); //<---- this line here }
To learn more about deep sleep with the ESP32 with timer wake up:
I hope this helps.
Regards,
Sara
Hi Sara
That works now
Thank you so much for you help. The tutorials are awesome.
My head is spinning though with so much info…
That’s great!
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara
I have seen it before, and again on this persons code. They leave their network credentials in their code when they publish it to the forum. I hope this is not his real network information.
Is there a way you could politely remind people about the security issues. Or maybe “X” them out before posting them.
Hi.
Thanks for noticing that. I deleted the credentials.
When I notice that people share their network credentials, I edit the responses to delete them.
I overlooked this one.
Regards,
Sara