This repository has been archived on 2020-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
lightscontrol-client/lightscontrol-client.ino

138 lines
2.9 KiB
Arduino
Raw Normal View History

2017-07-24 10:21:02 +00:00
#include <ESP8266WiFi.h>
const char* host = "ursaminor.local";
String serviceUri = "/service.php";
const int httpPort = 8088;
const int relaisPin = D2;
2017-07-24 10:21:02 +00:00
bool lightState = false;
unsigned int lightTime = 0;
bool updated = false;
void wifiConnect(void) {
2017-07-24 10:21:02 +00:00
Serial.println("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin("WLAN-Bey-IoT", "IoT-Fra80");
2017-07-24 10:21:02 +00:00
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print(" ok, IP: ");
2017-07-24 10:21:02 +00:00
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
delay(100);
wifiConnect();
2017-07-24 10:21:02 +00:00
pinMode(0, OUTPUT);
pinMode(relaisPin, OUTPUT);
}
void loop() {
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("ERROR connection failed");
return;
}
2017-07-24 10:21:02 +00:00
// This will send the request to the server
String httpRequest = "GET " + serviceUri + " HTTP/1.1\r\n" +
2017-07-24 10:21:02 +00:00
"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()) {
2017-07-24 10:21:02 +00:00
String line = client.readStringUntil('\n');
if (line.indexOf("HTTP") != -1) {
if (line.indexOf("200") == -1) {
Serial.println("ERROR: invalid http status code");
}
break;
} else {
;
}
}
2017-07-24 10:21:02 +00:00
// then read until the payload is found
while (client.available()) {
2017-07-24 10:21:02 +00:00
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);
2017-07-24 10:21:02 +00:00
if (buf.equals("0")) {
lightState = 0;
} else {
lightState = 1;
}
start = line.indexOf("t=");
buf = line.substring(start + 2);
2017-07-24 10:21:02 +00:00
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.println("sleeping with WiFi off");
2017-07-24 10:21:02 +00:00
Serial.println("");
Serial.flush();
WiFi.mode(WIFI_OFF);
delay(lightTime * 60 * 1000);
wifiConnect();
2017-07-24 10:21:02 +00:00
} else {
Serial.println("taking a normal sleep");
if (lightTime <= 1) {
2017-07-24 10:21:02 +00:00
delay(10 * 1000);
} else {
delay(lightTime * 60 * 1000);
}
}
2017-07-24 10:21:02 +00:00
Serial.println("");
updated = false;
}