From 0a4a92ac8b925c9e859e0fbb819bc36e31caf22e Mon Sep 17 00:00:00 2001 From: Julien Cabillot Date: Fri, 26 May 2017 16:40:52 +0200 Subject: [PATCH] =?UTF-8?q?on=20remets=20=C3=A7a=20comme=20j'ai=20l'habitu?= =?UTF-8?q?de=20[WIP]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arduino/sonoffliving/sonoffliving.cpp | 204 ++++++++++++++++++++++++++ arduino/sonoffliving/sonoffliving.ino | 0 2 files changed, 204 insertions(+) create mode 100644 arduino/sonoffliving/sonoffliving.cpp create mode 100644 arduino/sonoffliving/sonoffliving.ino diff --git a/arduino/sonoffliving/sonoffliving.cpp b/arduino/sonoffliving/sonoffliving.cpp new file mode 100644 index 0000000..75d65cc --- /dev/null +++ b/arduino/sonoffliving/sonoffliving.cpp @@ -0,0 +1,204 @@ +#include +#include + +// Wifi +#include + +// MQTT +#include + +// OTA +#include +#include +#include + +#include "sonoffliving.h" + +void callback(const MQTT::Publish& pub) { + if (pub.payload_string() == "stat") { + } + else if (pub.payload_string() == "on") { + digitalWrite(LED, LOW); + digitalWrite(RELAY, HIGH); + } + else if (pub.payload_string() == "off") { + digitalWrite(LED, HIGH); + digitalWrite(RELAY, LOW); + } + else if (pub.payload_string() == "reset") { + requestRestart = true; + } + sendStatus = true; +} + +void setup() { + pinMode(LED, OUTPUT); + pinMode(RELAY, OUTPUT); + pinMode(BUTTON, INPUT); + digitalWrite(LED, HIGH); + digitalWrite(RELAY, LOW); + Serial.begin(115200); + EEPROM.begin(8); + lastRelayState = EEPROM.read(0); + if (rememberRelayState && lastRelayState == 1) { + digitalWrite(LED, LOW); + digitalWrite(RELAY, HIGH); + } + btn_timer.attach(0.05, button); + mqttClient.set_callback(callback); + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + ArduinoOTA.onStart([]() { + OTAupdate = true; + blinkLED(LED, 400, 2); + digitalWrite(LED, HIGH); + Serial.println("OTA Update Initiated . . ."); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nOTA Update Ended . . .s"); + ESP.restart(); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + digitalWrite(LED, LOW); + delay(5); + digitalWrite(LED, HIGH); + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + blinkLED(LED, 40, 2); + OTAupdate = false; + Serial.printf("OTA Error [%u] ", error); + if (error == OTA_AUTH_ERROR) Serial.println(". . . . . . . . . . . . . . . Auth Failed"); + else if (error == OTA_BEGIN_ERROR) Serial.println(". . . . . . . . . . . . . . . Begin Failed"); + else if (error == OTA_CONNECT_ERROR) Serial.println(". . . . . . . . . . . . . . . Connect Failed"); + else if (error == OTA_RECEIVE_ERROR) Serial.println(". . . . . . . . . . . . . . . Receive Failed"); + else if (error == OTA_END_ERROR) Serial.println(". . . . . . . . . . . . . . . End Failed"); + }); + ArduinoOTA.begin(); + Serial.println(VERSION); + Serial.print("\nUnit ID: "); + Serial.print("esp8266-"); + Serial.print(ESP.getChipId(), HEX); + Serial.print("\nConnecting to "); Serial.print(WIFI_SSID); Serial.print(" Wifi"); + while ((WiFi.status() != WL_CONNECTED) && kRetries --) { + delay(500); + Serial.print(" ."); + } + if (WiFi.status() == WL_CONNECTED) { + Serial.println(" DONE"); + Serial.print("IP Address is: "); Serial.println(WiFi.localIP()); + Serial.print("Connecting to ");Serial.print(MQTT_SERVER);Serial.print(" Broker . ."); + delay(500); + while (!mqttClient.connect(MQTT::Connect(MQTT_CLIENT).set_keepalive(90).set_auth(MQTT_USER, MQTT_PASS)) && kRetries --) { + Serial.print(" ."); + delay(1000); + } + if(mqttClient.connected()) { + Serial.println(" DONE"); + Serial.println("\n---------------------------- Logs ----------------------------"); + Serial.println(); + mqttClient.subscribe(MQTT_TOPIC); + blinkLED(LED, 40, 8); + if(digitalRead(RELAY) == HIGH) { + digitalWrite(LED, LOW); + } else { + digitalWrite(LED, HIGH); + } + } + else { + Serial.println(" FAILED!"); + Serial.println("\n----------------------------------------------------------------"); + Serial.println(); + } + } + else { + Serial.println(" WiFi FAILED!"); + Serial.println("\n----------------------------------------------------------------"); + Serial.println(); + } +} + +void loop() { + ArduinoOTA.handle(); + if (OTAupdate == false) { + mqttClient.loop(); + timedTasks(); + checkStatus(); + } +} + +void blinkLED(int pin, int duration, int n) { + for(int i=0; i 1 && count <= 40) { + digitalWrite(LED, !digitalRead(LED)); + digitalWrite(RELAY, !digitalRead(RELAY)); + sendStatus = true; + } + else if (count >40){ + Serial.println("\n\nSonoff Rebooting . . . . . . . . Please Wait"); + requestRestart = true; + } + count=0; + } +} + +void checkConnection() { + if (WiFi.status() == WL_CONNECTED) { + if (mqttClient.connected()) { + Serial.println("mqtt broker connection . . . . . . . . . . OK"); + } + else { + Serial.println("mqtt broker connection . . . . . . . . . . LOST"); + requestRestart = true; + } + } + else { + Serial.println("WiFi connection . . . . . . . . . . LOST"); + requestRestart = true; + } +} + +void checkStatus() { + if (sendStatus) { + if(digitalRead(LED) == LOW) { + if (rememberRelayState) { + EEPROM.write(0, 1); + } + mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "on").set_retain().set_qos(1)); + Serial.println("Relay . . . . . . . . . . . . . . . . . . ON"); + } else { + if (rememberRelayState) { + EEPROM.write(0, 0); + } + mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "off").set_retain().set_qos(1)); + Serial.println("Relay . . . . . . . . . . . . . . . . . . OFF"); + } + if (rememberRelayState) { + EEPROM.commit(); + } + sendStatus = false; + } + if (requestRestart) { + blinkLED(LED, 400, 4); + ESP.restart(); + } +} + +void timedTasks() { + if ((millis() > TTasks + (kUpdFreq*60000)) || (millis() < TTasks)) { + TTasks = millis(); + checkConnection(); + } +} diff --git a/arduino/sonoffliving/sonoffliving.ino b/arduino/sonoffliving/sonoffliving.ino new file mode 100644 index 0000000..e69de29