simplification

This commit is contained in:
Julien Cabillot 2017-05-26 17:12:24 +02:00 committed by Cabillot Julien
parent 0a4a92ac8b
commit 1814ebece2

View File

@ -12,119 +12,172 @@
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
// RemoteDebug
#include <RemoteDebug.h>
#include "sonoffliving.h" #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() { void setup() {
Serial.begin(SERIAL_SPEED);
Serial.println("\nresetting");
pinMode(LED, OUTPUT); pinMode(LED, OUTPUT);
pinMode(RELAY, OUTPUT); pinMode(RELAY, OUTPUT);
pinMode(BUTTON, INPUT); pinMode(BUTTON, INPUT);
digitalWrite(LED, HIGH); digitalWrite(LED, LOW);
digitalWrite(RELAY, LOW); digitalWrite(RELAY, HIGH);
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); btn_timer.attach(0.05, button);
mqttClient.set_callback(callback);
WiFi.mode(WIFI_STA); // WIFI
WiFi.begin(WIFI_SSID, WIFI_PASS); 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([]() { ArduinoOTA.onStart([]() {
Debug.println("OTA Starting");
Serial.println("OTA Starting");
OTAupdate = true; OTAupdate = true;
blinkLED(LED, 400, 2); blinkLED(LED, 400, 2);
digitalWrite(LED, HIGH); digitalWrite(LED, HIGH);
Serial.println("OTA Update Initiated . . .");
}); });
ArduinoOTA.onEnd([]() { ArduinoOTA.onEnd([]() {
Serial.println("\nOTA Update Ended . . .s"); Debug.println("\nOTA End");
ESP.restart(); Serial.println("\nOTA End");
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { 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, LOW);
delay(5); delay(5);
digitalWrite(LED, HIGH); digitalWrite(LED, HIGH);
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
}); });
ArduinoOTA.onError([](ota_error_t error) { ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("OTA Error[%u]: ", error);
Debug.printf("OTA Error[%u]: ", error);
blinkLED(LED, 40, 2); blinkLED(LED, 40, 2);
OTAupdate = false; OTAupdate = false;
Serial.printf("OTA Error [%u] ", error); if (error == OTA_AUTH_ERROR) {
if (error == OTA_AUTH_ERROR) Serial.println(". . . . . . . . . . . . . . . Auth Failed"); Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println(". . . . . . . . . . . . . . . Begin Failed"); Debug.println("Auth Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println(". . . . . . . . . . . . . . . Connect Failed"); } else if (error == OTA_BEGIN_ERROR) {
else if (error == OTA_RECEIVE_ERROR) Serial.println(". . . . . . . . . . . . . . . Receive Failed"); Serial.println("Begin Failed");
else if (error == OTA_END_ERROR) Serial.println(". . . . . . . . . . . . . . . End 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(); ArduinoOTA.begin();
Serial.println(VERSION); }
Serial.print("\nUnit ID: ");
Serial.print("esp8266-"); // WIFI
Serial.print(ESP.getChipId(), HEX); void setupWifi()
Serial.print("\nConnecting to "); Serial.print(WIFI_SSID); Serial.print(" Wifi"); {
while ((WiFi.status() != WL_CONNECTED) && kRetries --) { 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); delay(500);
Serial.print(" ."); Serial.print(".");
} }
if (WiFi.status() == WL_CONNECTED) { Serial.println(" OK");
Serial.println(" DONE"); Serial.print("IP : ");
Serial.print("IP Address is: "); Serial.println(WiFi.localIP()); 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 --) { // MQTT
Serial.print(" ."); void testConnectMQTT()
delay(1000); {
} while (!client.connected()) {
if(mqttClient.connected()) { Debug.print("Connexion au serveur MQTT... ");
Serial.println(" DONE"); if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
Serial.println("\n---------------------------- Logs ----------------------------"); Debug.print("OK\nSend Current State");
Serial.println(); /*
mqttClient.subscribe(MQTT_TOPIC); mqttSendState();
mqttSendBrightnessState();
mqttSendColorState();
*/
Debug.print("OK\nSubscribe");
client.subscribe(MQTT_TOPIC);
blinkLED(LED, 40, 8); blinkLED(LED, 40, 8);
if(digitalRead(RELAY) == HIGH) { if(digitalRead(RELAY) == HIGH) {
digitalWrite(LED, LOW); digitalWrite(LED, LOW);
} else { } else {
digitalWrite(LED, HIGH); digitalWrite(LED, HIGH);
} }
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(" FAILED!");
Serial.println("\n----------------------------------------------------------------");
Serial.println();
}
}
else {
Serial.println(" WiFi FAILED!");
Serial.println("\n----------------------------------------------------------------");
Serial.println();
} }
} }
void loop() { // Déclenche les actions à la réception d'un message
ArduinoOTA.handle(); void callbackMQTT(char* topic, byte* payload, unsigned int length)
if (OTAupdate == false) { {
mqttClient.loop(); String stopic = String(topic);
timedTasks();
checkStatus(); 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();
// RemoteDebug
Debug.handle();
// MQTT
testConnectMQTT();
client.loop();
} }
void blinkLED(int pin, int duration, int n) { void blinkLED(int pin, int duration, int n) {
@ -153,52 +206,3 @@ void button() {
count=0; 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();
}
}