From e99219cc16943c57946517d5ccdf80c4981b349f Mon Sep 17 00:00:00 2001 From: Lomanic Date: Fri, 22 Nov 2019 21:19:39 +0100 Subject: [PATCH 1/3] Autoformat Arduino code wit Arduino IDE --- arduino.cpp | 157 ++++++++++++++++++++++++++-------------------------- 1 file changed, 78 insertions(+), 79 deletions(-) diff --git a/arduino.cpp b/arduino.cpp index 18f5a49..9926db3 100644 --- a/arduino.cpp +++ b/arduino.cpp @@ -1,10 +1,10 @@ /*** - * Fuz Piedthon - * Pedestrian traffic lights, green and red, controlled over MQTT - * Board: ESP8266 - * - */ + Fuz Piedthon + Pedestrian traffic lights, green and red, controlled over MQTT + Board: ESP8266 + +*/ #include @@ -22,10 +22,10 @@ #include "WifiCredFuz.cpp" // Wifi and MQTT Credentials /* with -#define WLAN_SSID "" -#define WLAN_PASS "" -#define MQTT_USERNAME "" //omg no user -#define MQTT_KEY "" //omg no key + #define WLAN_SSID "" + #define WLAN_PASS "" + #define MQTT_USERNAME "" //omg no user + #define MQTT_KEY "" //omg no key */ /// End of user config @@ -38,20 +38,20 @@ WiFiClient client; void setupWifi() { - // Connect to WiFi access point. - Serial.print("Connecting to "); - Serial.println(WLAN_SSID); + // Connect to WiFi access point. + Serial.print("Connecting to "); + Serial.println(WLAN_SSID); - WiFi.begin(WLAN_SSID, WLAN_PASS); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(); + WiFi.begin(WLAN_SSID, WLAN_PASS); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); - Serial.print("WiFi connected"); - Serial.print(" IP address: "); - Serial.println(WiFi.localIP()); + Serial.print("WiFi connected"); + Serial.print(" IP address: "); + Serial.println(WiFi.localIP()); } /// MQTT @@ -78,55 +78,55 @@ char *mqtt_message; // Function to connect and reconnect as necessary to the MQTT server. void MQTT_connect() { - int8_t ret; + int8_t ret; - // Stop if already connected. - if (mqtt.connected()) { - return; + // Stop if already connected. + if (mqtt.connected()) { + return; + } + + Serial.print("Connecting to MQTT... "); + + uint8_t retries = 3; + while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected + Serial.println(mqtt.connectErrorString(ret)); + Serial.println("Retrying MQTT connection in 5 seconds..."); + mqtt.disconnect(); + delay(5000); // wait 5 seconds + retries--; + if (retries == 0) { + // basically die and wait for WDT to reset me + Serial.println("Fail to connect to MQTT after 3 tries ... "); + return; // will recall the function } - - Serial.print("Connecting to MQTT... "); - - uint8_t retries = 3; - while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected - Serial.println(mqtt.connectErrorString(ret)); - Serial.println("Retrying MQTT connection in 5 seconds..."); - mqtt.disconnect(); - delay(5000); // wait 5 seconds - retries--; - if (retries == 0) { - // basically die and wait for WDT to reset me - Serial.println("Fail to connect to MQTT after 3 tries ... "); - return; // will recall the function - } - } - Serial.println("MQTT Connected!"); + } + Serial.println("MQTT Connected!"); } /// PiedThon void messageToPiedthon(char *msg) { - // Clignotement ? deux en même temps ? - switch (msg[0]) { - // LOW active le relay - case '0' : // Rouge et Vert - digitalWrite(GPIO_ROUGE, LOW); - digitalWrite(GPIO_VERT, LOW); - break; - case '1'​ : // ROUGE - digitalWrite(GPIO_VERT, HIGH); - digitalWrite(GPIO_ROUGE, LOW); - break; - case '2' : // VERT - digitalWrite(GPIO_VERT, LOW); - digitalWrite(GPIO_ROUGE, HIGH); - break; - case '3'​ : // Rien - digitalWrite(GPIO_ROUGE, HIGH); - digitalWrite(GPIO_VERT, HIGH); - break; - } + // Clignotement ? deux en même temps ? + switch (msg[0]) { + // LOW active le relay + case '0' : // Rouge et Vert + digitalWrite(GPIO_ROUGE, LOW); + digitalWrite(GPIO_VERT, LOW); + break; + case '1' : // ROUGE + digitalWrite(GPIO_VERT, HIGH); + digitalWrite(GPIO_ROUGE, LOW); + break; + case '2' : // VERT + digitalWrite(GPIO_VERT, LOW); + digitalWrite(GPIO_ROUGE, HIGH); + break; + case '3' : // Rien + digitalWrite(GPIO_ROUGE, HIGH); + digitalWrite(GPIO_VERT, HIGH); + break; + } } //// Bug workaround for Arduino 1.6.6, it seems to need a function declaration @@ -136,31 +136,30 @@ void messageToPiedthon(char *msg) { void setup() { - Serial.begin(115200); - delay(10); - Serial.println(F("Adafruit MQTT PiedThon remix")); + Serial.begin(115200); + delay(10); + Serial.println(F("Adafruit MQTT PiedThon remix")); - pinMode(GPIO_VERT, OUTPUT); - pinMode(GPIO_ROUGE, OUTPUT); + pinMode(GPIO_VERT, OUTPUT); + pinMode(GPIO_ROUGE, OUTPUT); - setupWifi(); + setupWifi(); - // Setup MQTT subscription for onoff feed. - mqtt.subscribe(&sub_piedthon); + // Setup MQTT subscription for onoff feed. + mqtt.subscribe(&sub_piedthon); } void loop() { - MQTT_connect(); + MQTT_connect(); - Adafruit_MQTT_Subscribe *subscription; - while ((subscription = mqtt.readSubscription(5000))) { - if (subscription == &sub_piedthon) { - mqtt_message = (char *) sub_piedthon.lastread; - Serial.println("Got: "+String(mqtt_message)); - messageToPiedthon(mqtt_message); - } + Adafruit_MQTT_Subscribe *subscription; + while ((subscription = mqtt.readSubscription(5000))) { + if (subscription == &sub_piedthon) { + mqtt_message = (char *) sub_piedthon.lastread; + Serial.println("Got: " + String(mqtt_message)); + messageToPiedthon(mqtt_message); } + } } - From 87dc889a7b794cd56ba2aac5cef8688edd11b7dc Mon Sep 17 00:00:00 2001 From: Lomanic Date: Fri, 22 Nov 2019 21:21:54 +0100 Subject: [PATCH 2/3] Implement OTA over local WiFi --- arduino.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/arduino.cpp b/arduino.cpp index 9926db3..336a227 100644 --- a/arduino.cpp +++ b/arduino.cpp @@ -7,6 +7,9 @@ */ #include +#include +#include +#include /// Pins configuration @@ -18,7 +21,7 @@ #define MQTT_SERVER "sonic.fuz.re" #define MQTT_SERVERPORT 1883 - +#define ESP_HOSTNAME "piedthon" #include "WifiCredFuz.cpp" // Wifi and MQTT Credentials /* with @@ -67,8 +70,8 @@ Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, /****************************** Feeds ***************************************/ -// Setup a feed called 'pub_photocell' for publishing. -Adafruit_MQTT_Publish pub_photocell = Adafruit_MQTT_Publish(&mqtt, "piedthon/debug"); +// Setup a feed called 'pub_piedthon' for publishing. +Adafruit_MQTT_Publish pub_piedthon = Adafruit_MQTT_Publish(&mqtt, "piedthon/debug"); // Setup a feed called 'sub_piedthon' for subscribing to changes. Adafruit_MQTT_Subscribe sub_piedthon = Adafruit_MQTT_Subscribe(&mqtt, "piedthon/input"); @@ -122,7 +125,7 @@ void messageToPiedthon(char *msg) { digitalWrite(GPIO_VERT, LOW); digitalWrite(GPIO_ROUGE, HIGH); break; - case '3' : // Rien + case '3' : // Rien digitalWrite(GPIO_ROUGE, HIGH); digitalWrite(GPIO_VERT, HIGH); break; @@ -135,7 +138,6 @@ void messageToPiedthon(char *msg) { void setup() { - Serial.begin(115200); delay(10); Serial.println(F("Adafruit MQTT PiedThon remix")); @@ -143,16 +145,51 @@ void setup() { pinMode(GPIO_VERT, OUTPUT); pinMode(GPIO_ROUGE, OUTPUT); + randomSeed(analogRead(0)); + setupWifi(); // Setup MQTT subscription for onoff feed. mqtt.subscribe(&sub_piedthon); + + + ArduinoOTA.setHostname(ESP_HOSTNAME); + ArduinoOTA.onStart([]() { // switch off all the GPIOs during upgrade + digitalWrite(GPIO_ROUGE, HIGH); + digitalWrite(GPIO_VERT, HIGH); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + /*if (progress % (total / 4) == 0) { + digitalWrite(GPIO_ROUGE, random(2) ? LOW : HIGH); + digitalWrite(GPIO_VERT, random(2) ? LOW : HIGH); + }*/ + + }); + + ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end + for (int i = 0; i < 10; i++) { + int RANDOM_GPIO = GPIO_ROUGE; + if (random(2)) { // random(2) returns 0 or 1 randomly + RANDOM_GPIO = GPIO_VERT; + } + digitalWrite(RANDOM_GPIO, !digitalRead(RANDOM_GPIO)); + delay(400); + } + digitalWrite(GPIO_ROUGE, LOW); + digitalWrite(GPIO_VERT, LOW); + }); + + ArduinoOTA.onError([](ota_error_t error) { + (void)error; + ESP.restart(); + }); + ArduinoOTA.begin(); } void loop() { - MQTT_connect(); + ArduinoOTA.handle(); Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(5000))) { From 64549943c7d199dcd49c6b94969927fea5d6ef7c Mon Sep 17 00:00:00 2001 From: Lomanic Date: Fri, 22 Nov 2019 21:24:23 +0100 Subject: [PATCH 3/3] Link Fuz wiki in README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4fb6fe7..06be105 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # PiedThon -Feux PiedThon ESP8266 \ No newline at end of file +Feux PiedThon ESP8266 + +https://wiki.fuz.re/doku.php?id=projets:fuz:piedthon \ No newline at end of file