[CODE] more failsafe actions, new hostname

This commit is contained in:
Jannik Beyerstedt 2017-08-23 12:35:49 +02:00
parent a6a1a5fc38
commit 7f2d3f1ae8

View file

@ -1,6 +1,13 @@
/*
* 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> #include <ESP8266WiFi.h>
const char* host = "ursaminor.local"; const char* host = "ursaminor.fra80";
String serviceUri = "/service.php"; String serviceUri = "/service.php";
const int httpPort = 8088; const int httpPort = 8088;
@ -10,39 +17,61 @@ bool lightState = false;
unsigned int lightTime = 0; unsigned int lightTime = 0;
bool updated = false; bool updated = false;
void wifiConnect(void) { 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"); Serial.println("Connecting to WiFi");
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin("WLAN-Bey-IoT", "IoT-Fra80"); 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(".");
cycles++;
if (cycles > wifiConnectTimeout) {
return false;
}
} }
Serial.print(" ok, IP: "); Serial.print(" ok, IP: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
return true;
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(100); delay(100);
wifiConnect();
pinMode(0, OUTPUT); pinMode(0, OUTPUT);
pinMode(relaisPin, OUTPUT); pinMode(relaisPin, OUTPUT);
digitalWrite(relaisPin, HIGH); /* failsafe */
while (!wifiConnect()) {
/* wait 5s and try again */
delay(5000);
}
srvConnCnt = 0;
} }
void loop() { void loop() {
if (srvConnCnt > serverConnectAttempts) {
digitalWrite(relaisPin, HIGH); /* failsafe */
}
Serial.print("connecting to "); Serial.print("connecting to ");
Serial.println(host); Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client; WiFiClient client;
if (!client.connect(host, httpPort)) { if (!client.connect(host, httpPort)) {
Serial.println("ERROR connection failed"); Serial.println("ERROR connection failed");
srvConnCnt++;
return; return;
} }
srvConnCnt = 0;
// 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" +
@ -115,18 +144,20 @@ 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("sleeping with WiFi off"); Serial.print("sleeping with WiFi off: Minutes ");
Serial.println(""); Serial.println(lightTime);
Serial.flush(); Serial.flush();
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
delay(lightTime * 60 * 1000); delay(lightTime * 60 * 1000);
wifiConnect(); wifiConnect();
} else { } else {
Serial.println("taking a normal sleep"); Serial.print("taking a normal sleep: Minutes ");
if (lightTime <= 1) { if (lightTime < 1) {
Serial.println("only 10 seconds");
delay(10 * 1000); delay(10 * 1000);
} else { } else {
Serial.println(lightTime);
delay(lightTime * 60 * 1000); delay(lightTime * 60 * 1000);
} }
} }