From 7b29c1dab62b16ae8b7819a570e4c3bccb488c1e Mon Sep 17 00:00:00 2001 From: Julien Cabillot Date: Fri, 26 May 2017 18:02:44 +0200 Subject: [PATCH] beautification --- arduino/sonoffliving/sonoffliving.cpp | 80 +++++++++++---------- arduino/sonoffliving/sonoffliving.example.h | 3 + 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/arduino/sonoffliving/sonoffliving.cpp b/arduino/sonoffliving/sonoffliving.cpp index 7f45181..7965f2c 100644 --- a/arduino/sonoffliving/sonoffliving.cpp +++ b/arduino/sonoffliving/sonoffliving.cpp @@ -39,11 +39,10 @@ void setup() { } void setupSonOff() { - pinMode(LED, OUTPUT); - pinMode(RELAY, OUTPUT); - pinMode(BUTTON, INPUT); - digitalWrite(LED, LOW); - digitalWrite(RELAY, HIGH); + pinMode(LED_PIN, OUTPUT); + pinMode(RELAY_PIN, OUTPUT); + pinMode(BUTTON_PIN, INPUT); + relayState = false; btn_timer.attach(0.05, button); } @@ -55,8 +54,8 @@ void setupOTA() ArduinoOTA.onStart([]() { Debug.println("OTA Starting"); Serial.println("OTA Starting"); - blinkLED(LED, 400, 2); - digitalWrite(LED, HIGH); + blinkLED(LED_PIN, 400, 2); + digitalWrite(LED_PIN, HIGH); }); ArduinoOTA.onEnd([]() { Debug.println("\nOTA End"); @@ -65,14 +64,14 @@ void setupOTA() ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Debug.printf("OTA Progress: %u%%\r", (progress / (total / 100))); Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100))); - digitalWrite(LED, LOW); + digitalWrite(LED_PIN, LOW); delay(5); - digitalWrite(LED, HIGH); + digitalWrite(LED_PIN, HIGH); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("OTA Error[%u]: ", error); Debug.printf("OTA Error[%u]: ", error); - blinkLED(LED, 40, 2); + blinkLED(LED_PIN, 40, 2); if (error == OTA_AUTH_ERROR) { Serial.println("Auth Failed"); Debug.println("Auth Failed"); @@ -117,20 +116,16 @@ void testConnectMQTT() Debug.print("Connexion au serveur MQTT... "); if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) { Debug.print("OK\nSend Current State"); - /* mqttSendState(); - mqttSendBrightnessState(); - mqttSendColorState(); - */ Debug.print("OK\nSubscribe"); client.subscribe(MQTT_COMMAND); - blinkLED(LED, 40, 8); - if(digitalRead(RELAY) == HIGH) { - digitalWrite(LED, LOW); + blinkLED(LED_PIN, 40, 8); + if(digitalRead(RELAY_PIN) == HIGH) { + digitalWrite(LED_PIN, LOW); } else { - digitalWrite(LED, HIGH); + digitalWrite(LED_PIN, HIGH); } Debug.println(" OK"); @@ -158,17 +153,23 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) Debug.print("Received [" + stopic + "] : "); Debug.println(msgString); - if (msgString == "on") { - digitalWrite(LED, LOW); - digitalWrite(RELAY, HIGH); - } else if (msgString == "off") { - digitalWrite(LED, HIGH); - digitalWrite(RELAY, LOW); - } else if (msgString == "reset"){ - // TODO : esp restart + if (stopic == MQTT_COMMAND) { + if (msgString == "on") { + relayState = true; + } else if (msgString == "off") { + relayState = false; + } else if (msgString == "reset"){ + ESP.restart(); + } + mqttSendState(); } } +void mqttSendState() +{ + client.publish(MQTT_STATE, (relayState) ? "ON": "OFF", true); +} + void loop() { // OTA ArduinoOTA.handle(); @@ -179,30 +180,37 @@ void loop() { // MQTT testConnectMQTT(); client.loop(); + + if (relayState) { + digitalWrite(LED_PIN, LOW); + digitalWrite(RELAY_PIN, HIGH); + } else { + digitalWrite(LED_PIN, HIGH); + digitalWrite(RELAY_PIN, LOW); + } } void blinkLED(int pin, int duration, int n) { - for(int i=0; i 1 && count <= 40) { - digitalWrite(LED, !digitalRead(LED)); - digitalWrite(RELAY, !digitalRead(RELAY)); + digitalWrite(LED_PIN, !digitalRead(LED_PIN)); + digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN)); } - else if (count >40){ - Serial.println("\n\nSonoff Rebooting . . . . . . . . Please Wait"); - // TODO : esp restart + else if (count > 40) { + ESP.restart(); } - count=0; + count = 0; } } diff --git a/arduino/sonoffliving/sonoffliving.example.h b/arduino/sonoffliving/sonoffliving.example.h index 177bcc9..bb3c5df 100644 --- a/arduino/sonoffliving/sonoffliving.example.h +++ b/arduino/sonoffliving/sonoffliving.example.h @@ -35,6 +35,8 @@ PubSubClient client(espClient); Ticker btn_timer; unsigned long count = 0; +bool relayState; + void setup(); void loop(); void blinkLED(int pin, int duration, int n); @@ -44,3 +46,4 @@ void setupWifi(); void testConnectMQTT(); void callbackMQTT(char* topic, byte* payload, unsigned int length); void setupSonOff(); +void mqttSendState();