- Mon Aug 01, 2016 2:55 pm
#51900
Okay, tried to max out delay:
Sending Data every 20 Seconds = 6h lasting
Sending Date every 5 Min. = 72h lasting!
That´s already great, but not enough.
As far as i understand modem sleep, it automatically turns on whenever a delay happens. As long, as the ESP is in Station mode.
So:
Code: Select alldht.begin();
WiFi.mode(WIFI_STA); // added!
WiFi.begin(ssid, password);
Serial.println();
Result: Look above, 72h with 5 Min. delay.
After that, i´ve tried:
Code: Select all// Required for LIGHT_SLEEP_T delay mode
extern "C" {
#include "user_interface.h"
}
...
...
...
Serial.println("Waiting…");
// thingspeak needs minimum 15 sec delay between updates
wifi_set_sleep_type(LIGHT_SLEEP_T); // added
delay(900000); // 15 Min.
}
Battery was empty in less than one day!
I think, i´m still missing something. Maybe anyone can help me to implement Light Sleep the right way?
Thanks!
PS: Actual Sketch
Code: Select all#include <DHT.h>
#include <ESP8266WiFi.h>
// replace with your channel’s thingspeak API key,
String apiKey = "adfadfgadfgadfgadfg";
const char* ssid = "adfgadfg";
const char* password = "12121212121212";
const char* server = "api.thingspeak.com";
#define DHTPIN 2 // what pin we’re connected to
DHT dht(DHTPIN, DHT11,15);
WiFiClient client;
// Required for LIGHT_SLEEP_T delay mode
extern "C" {
#include "user_interface.h"
}
void setup() {
Serial.begin(115200);
delay(10);
dht.begin();
WiFi.mode(WIFI_STA); // added 230716
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
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);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
Serial.println("% send to Thingspeak");
}
client.stop();
Serial.println("Waiting…");
// thingspeak needs minimum 15 sec delay between updates
wifi_set_sleep_type(LIGHT_SLEEP_T);
delay(900000);
}