|
|
|
@ -12,119 +12,172 @@ |
|
|
|
#include <WiFiUdp.h>
|
|
|
|
#include <ArduinoOTA.h>
|
|
|
|
|
|
|
|
#include "sonoffliving.h"
|
|
|
|
// RemoteDebug
|
|
|
|
#include <RemoteDebug.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; |
|
|
|
} |
|
|
|
#include "sonoffliving.h"
|
|
|
|
|
|
|
|
void setup() { |
|
|
|
Serial.begin(SERIAL_SPEED); |
|
|
|
Serial.println("\nresetting"); |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
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); |
|
|
|
|
|
|
|
// WIFI
|
|
|
|
setupWifi(); |
|
|
|
|
|
|
|
// OTA
|
|
|
|
setupOTA(); |
|
|
|
|
|
|
|
// RemoteDebug
|
|
|
|
Debug.begin(REMDEB_CLIENT); |
|
|
|
|
|
|
|
// MQTT
|
|
|
|
client.setServer(MQTT_SERVER, MQTT_PORT); |
|
|
|
client.setCallback(callbackMQTT); |
|
|
|
testConnectMQTT(); |
|
|
|
} |
|
|
|
|
|
|
|
// OTA
|
|
|
|
void setupOTA() |
|
|
|
{ |
|
|
|
ArduinoOTA.setHostname(OTA_CLIENT); // on donne une petit nom a notre module
|
|
|
|
ArduinoOTA.setPassword(OTA_PASSWORD); |
|
|
|
ArduinoOTA.onStart([]() { |
|
|
|
Debug.println("OTA Starting"); |
|
|
|
Serial.println("OTA Starting"); |
|
|
|
OTAupdate = true; |
|
|
|
blinkLED(LED, 400, 2); |
|
|
|
digitalWrite(LED, HIGH); |
|
|
|
Serial.println("OTA Update Initiated . . ."); |
|
|
|
}); |
|
|
|
ArduinoOTA.onEnd([]() { |
|
|
|
Serial.println("\nOTA Update Ended . . .s"); |
|
|
|
ESP.restart(); |
|
|
|
Debug.println("\nOTA End"); |
|
|
|
Serial.println("\nOTA End"); |
|
|
|
}); |
|
|
|
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); |
|
|
|
delay(5); |
|
|
|
digitalWrite(LED, HIGH); |
|
|
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); |
|
|
|
}); |
|
|
|
ArduinoOTA.onError([](ota_error_t error) { |
|
|
|
Serial.printf("OTA Error[%u]: ", error); |
|
|
|
Debug.printf("OTA Error[%u]: ", 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"); |
|
|
|
if (error == OTA_AUTH_ERROR) { |
|
|
|
Serial.println("Auth Failed"); |
|
|
|
Debug.println("Auth Failed"); |
|
|
|
} else if (error == OTA_BEGIN_ERROR) { |
|
|
|
Serial.println("Begin Failed"); |
|
|
|
Debug.println("Begin Failed"); |
|
|
|
} else if (error == OTA_CONNECT_ERROR) { |
|
|
|
Serial.println("Connect Failed"); |
|
|
|
Debug.println("Connect Failed"); |
|
|
|
} else if (error == OTA_RECEIVE_ERROR) { |
|
|
|
Serial.println("Receive Failed"); |
|
|
|
Debug.println("Receive Failed"); |
|
|
|
} else if (error == OTA_END_ERROR) { |
|
|
|
Serial.println("End Failed"); |
|
|
|
Debug.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 --) { |
|
|
|
} |
|
|
|
|
|
|
|
// WIFI
|
|
|
|
void setupWifi() |
|
|
|
{ |
|
|
|
Serial.print("Connexion a "); |
|
|
|
Serial.print(WIFI_SSID); |
|
|
|
WiFi.mode(WIFI_STA); |
|
|
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED) { |
|
|
|
delay(500); |
|
|
|
Serial.print(" ."); |
|
|
|
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); |
|
|
|
Serial.println(" OK"); |
|
|
|
Serial.print("IP : "); |
|
|
|
Serial.println(WiFi.localIP()); |
|
|
|
} |
|
|
|
|
|
|
|
// MQTT
|
|
|
|
void testConnectMQTT() |
|
|
|
{ |
|
|
|
while (!client.connected()) { |
|
|
|
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_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(); |
|
|
|
|
|
|
|
Debug.println(" OK"); |
|
|
|
} else { |
|
|
|
Debug.print("KO, erreur : "); |
|
|
|
Debug.print(client.state()); |
|
|
|
Debug.println(", on attend 5 secondes avant de recommencer"); |
|
|
|
delay(5000); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
Serial.println(" WiFi FAILED!"); |
|
|
|
Serial.println("\n----------------------------------------------------------------"); |
|
|
|
Serial.println(); |
|
|
|
} |
|
|
|
|
|
|
|
// Déclenche les actions à la réception d'un message
|
|
|
|
void callbackMQTT(char* topic, byte* payload, unsigned int length) |
|
|
|
{ |
|
|
|
String stopic = String(topic); |
|
|
|
|
|
|
|
unsigned int i = 0; |
|
|
|
for(i = 0; i < length; i++) { |
|
|
|
message_buff[i] = payload[i]; |
|
|
|
} |
|
|
|
message_buff[i] = '\0'; |
|
|
|
String msgString = String(message_buff); |
|
|
|
|
|
|
|
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"){ |
|
|
|
requestRestart = true; |
|
|
|
} |
|
|
|
sendStatus = true; |
|
|
|
} |
|
|
|
|
|
|
|
void loop() { |
|
|
|
// OTA
|
|
|
|
ArduinoOTA.handle(); |
|
|
|
if (OTAupdate == false) { |
|
|
|
mqttClient.loop(); |
|
|
|
timedTasks(); |
|
|
|
checkStatus(); |
|
|
|
} |
|
|
|
|
|
|
|
// RemoteDebug
|
|
|
|
Debug.handle(); |
|
|
|
|
|
|
|
// MQTT
|
|
|
|
testConnectMQTT(); |
|
|
|
client.loop(); |
|
|
|
} |
|
|
|
|
|
|
|
void blinkLED(int pin, int duration, int n) { |
|
|
|
@ -153,52 +206,3 @@ void button() { |
|
|
|
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(); |
|
|
|
} |
|
|
|
} |