[FIX] only switch wifi modem off instead of deep sleep

This commit is contained in:
Jannik Beyerstedt 2017-08-02 18:25:48 +02:00
parent 026ca60835
commit a6a1a5fc38

View file

@ -1,38 +1,32 @@
//#include <ESP8266WiFiGeneric.h>
//#include <WiFiClient.h>
//#include <WiFiClientSecure.h>
//#include <WiFiUdp.h>
//#include <ESP8266WiFiMulti.h>
//#include <ESP8266WiFiAP.h>
//#include <ESP8266WiFiSTA.h>
//#include <WiFiServer.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
const char* host = "ursaminor.local"; const char* host = "ursaminor.local";
String serviceUri = "/service.php"; String serviceUri = "/service.php";
const int httpPort = 8088; const int httpPort = 8088;
const int relaisPin = D3; const int relaisPin = D2;
bool lightState = false; bool lightState = false;
unsigned int lightTime = 0; unsigned int lightTime = 0;
bool updated = false; bool updated = false;
void setup() { void wifiConnect(void) {
Serial.begin(115200);
delay(100);
Serial.println("");
Serial.println("Connecting to WiFi"); Serial.println("Connecting to WiFi");
WiFi.begin("WLAN-Bey-IoT","IoT-Fra80"); WiFi.mode(WIFI_STA);
WiFi.begin("WLAN-Bey-IoT", "IoT-Fra80");
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
} }
Serial.println(" connected"); Serial.print(" ok, IP: ");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
Serial.println(""); }
void setup() {
Serial.begin(115200);
delay(100);
wifiConnect();
pinMode(0, OUTPUT); pinMode(0, OUTPUT);
pinMode(relaisPin, OUTPUT); pinMode(relaisPin, OUTPUT);
@ -49,9 +43,9 @@ void loop() {
Serial.println("ERROR connection failed"); Serial.println("ERROR connection failed");
return; return;
} }
// This will send the request to the server // This will send the request to the server
String httpRequest = "GET " + serviceUri + " HTTP/1.1\r\n" + String httpRequest = "GET " + serviceUri + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" + "Host: " + host + "\r\n" +
"Connection: close\r\n\r\n"; "Connection: close\r\n\r\n";
client.print(httpRequest); client.print(httpRequest);
@ -61,7 +55,7 @@ void loop() {
// Read the lines of the reply from server // Read the lines of the reply from server
// first expect a HTTP 200 status code // first expect a HTTP 200 status code
while(client.available()) { while (client.available()) {
String line = client.readStringUntil('\n'); String line = client.readStringUntil('\n');
if (line.indexOf("HTTP") != -1) { if (line.indexOf("HTTP") != -1) {
if (line.indexOf("200") == -1) { if (line.indexOf("200") == -1) {
@ -72,17 +66,17 @@ void loop() {
; ;
} }
} }
// then read until the payload is found // then read until the payload is found
while(client.available()) { while (client.available()) {
String line = client.readStringUntil('\n'); String line = client.readStringUntil('\n');
int start = line.indexOf("s="); int start = line.indexOf("s=");
if (start != -1) { if (start != -1) {
// parse state information // parse state information
String buf; String buf;
buf = line.substring(start+2, start+3); buf = line.substring(start + 2, start + 3);
if (buf.equals("0")) { if (buf.equals("0")) {
lightState = 0; lightState = 0;
} else { } else {
@ -90,7 +84,7 @@ void loop() {
} }
start = line.indexOf("t="); start = line.indexOf("t=");
buf = line.substring(start+2); buf = line.substring(start + 2);
lightTime = buf.toInt(); lightTime = buf.toInt();
updated = true; updated = true;
@ -112,7 +106,6 @@ void loop() {
Serial.print("\tTime: "); Serial.print("\tTime: ");
Serial.println(lightTime); Serial.println(lightTime);
// TODO: SWITCH SOME PIN
digitalWrite(relaisPin, lightState); digitalWrite(relaisPin, lightState);
// sleep some time according to the recommendation in the answer // sleep some time according to the recommendation in the answer
@ -122,21 +115,23 @@ void loop() {
// only deep sleep, if the time is more than 5 minutes // only deep sleep, if the time is more than 5 minutes
if (lightTime >= 5 && lightState == false) { if (lightTime >= 5 && lightState == false) {
Serial.println("going for a deep sleep"); Serial.println("sleeping with WiFi off");
Serial.println(""); Serial.println("");
Serial.flush(); Serial.flush();
Serial.end();
ESP.deepSleep(lightTime * 60 * 1000000); WiFi.mode(WIFI_OFF);
// connect GPIO16 (D0) to RST!!! delay(lightTime * 60 * 1000);
wifiConnect();
} else { } else {
Serial.println("taking a normal sleep"); Serial.println("taking a normal sleep");
if (lightTime < 1) { if (lightTime <= 1) {
delay(10 * 1000); delay(10 * 1000);
} else { } else {
delay(lightTime * 60 * 1000); delay(lightTime * 60 * 1000);
} }
} }
Serial.println(""); Serial.println("");
updated = false; updated = false;
} }