169 lines
3.7 KiB
C++
169 lines
3.7 KiB
C++
/*
|
|
* Outside Lighting Controll - ESP8266 Client
|
|
*
|
|
* copyright: Jannik Beyerstedt | http://jannikbeyerstedt.de | code@jannikbeyerstedt.de
|
|
* license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
|
|
*/
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
const char* host = "ursaminor.fra80";
|
|
String serviceUri = "/lightscontrol/service.php";
|
|
const int httpPort = 80;
|
|
|
|
const int relaisPin = D2;
|
|
|
|
bool lightState = false;
|
|
unsigned int lightTime = 0;
|
|
bool updated = false;
|
|
|
|
const unsigned int wifiConnectTimeout = 20; /* num of 500ms intervals */
|
|
const unsigned int serverConnectAttempts = 10; /* max num of attempts */
|
|
unsigned int srvConnCnt;
|
|
|
|
bool wifiConnect(void) {
|
|
unsigned int cycles = 0;
|
|
|
|
Serial.println("Connecting to WiFi");
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin("WLAN-Bey-IoT", "IoT-Fra80");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
|
|
cycles++;
|
|
if (cycles > wifiConnectTimeout) {
|
|
return false;
|
|
}
|
|
}
|
|
Serial.print(" ok, IP: ");
|
|
Serial.println(WiFi.localIP());
|
|
return true;
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
delay(100);
|
|
|
|
pinMode(0, OUTPUT);
|
|
pinMode(relaisPin, OUTPUT);
|
|
digitalWrite(relaisPin, HIGH); /* failsafe */
|
|
|
|
while (!wifiConnect()) {
|
|
/* wait 5s and try again */
|
|
delay(5000);
|
|
}
|
|
|
|
srvConnCnt = 0;
|
|
}
|
|
|
|
void loop() {
|
|
if (srvConnCnt > serverConnectAttempts) {
|
|
digitalWrite(relaisPin, HIGH); /* failsafe */
|
|
}
|
|
|
|
Serial.print("connecting to ");
|
|
Serial.println(host);
|
|
|
|
WiFiClient client;
|
|
if (!client.connect(host, httpPort)) {
|
|
Serial.println("ERROR connection failed");
|
|
srvConnCnt++;
|
|
return;
|
|
}
|
|
srvConnCnt = 0;
|
|
|
|
// This will send the request to the server
|
|
String httpRequest = "GET " + serviceUri + " HTTP/1.1\r\n" +
|
|
"Host: " + host + "\r\n" +
|
|
"Connection: close\r\n\r\n";
|
|
client.print(httpRequest);
|
|
delay(100);
|
|
|
|
|
|
// Read the lines of the reply from server
|
|
|
|
// first expect a HTTP 200 status code
|
|
while (client.available()) {
|
|
String line = client.readStringUntil('\n');
|
|
if (line.indexOf("HTTP") != -1) {
|
|
if (line.indexOf("200") == -1) {
|
|
Serial.println("ERROR: invalid http status code");
|
|
}
|
|
break;
|
|
} else {
|
|
;
|
|
}
|
|
}
|
|
|
|
// then read until the payload is found
|
|
while (client.available()) {
|
|
String line = client.readStringUntil('\n');
|
|
|
|
int start = line.indexOf("s=");
|
|
if (start != -1) {
|
|
// parse state information
|
|
String buf;
|
|
|
|
buf = line.substring(start + 2, start + 3);
|
|
if (buf.equals("0")) {
|
|
lightState = 0;
|
|
} else {
|
|
lightState = 1;
|
|
}
|
|
|
|
start = line.indexOf("t=");
|
|
buf = line.substring(start + 2);
|
|
lightTime = buf.toInt();
|
|
|
|
updated = true;
|
|
Serial.println("response parsed");
|
|
} else {
|
|
;
|
|
}
|
|
}
|
|
|
|
// failsafe
|
|
if (!updated) {
|
|
lightState = true;
|
|
lightTime = 1;
|
|
Serial.println("WARN failsafe");
|
|
}
|
|
|
|
Serial.print("Lights: ");
|
|
Serial.print(lightState);
|
|
Serial.print("\tTime: ");
|
|
Serial.println(lightTime);
|
|
|
|
digitalWrite(relaisPin, lightState);
|
|
|
|
// sleep some time according to the recommendation in the answer
|
|
if (lightTime > 15) { // not more than 15 minutes
|
|
lightTime = 15;
|
|
}
|
|
|
|
// only deep sleep, if the time is more than 5 minutes
|
|
if (lightTime >= 5 && lightState == false) {
|
|
Serial.print("sleeping with WiFi off: Minutes ");
|
|
Serial.println(lightTime);
|
|
Serial.flush();
|
|
|
|
WiFi.mode(WIFI_OFF);
|
|
delay(lightTime * 60 * 1000);
|
|
wifiConnect();
|
|
} else {
|
|
Serial.print("taking a normal sleep: Minutes ");
|
|
if (lightTime < 1) {
|
|
Serial.println("only 10 seconds");
|
|
delay(10 * 1000);
|
|
} else {
|
|
Serial.println(lightTime);
|
|
delay(lightTime * 60 * 1000);
|
|
}
|
|
}
|
|
|
|
Serial.println("");
|
|
updated = false;
|
|
}
|
|
|