From cc8707b9c4b12a4a82f56ef83fd537d7a174b44c Mon Sep 17 00:00:00 2001 From: Lomanic Date: Tue, 26 Nov 2019 23:46:42 +0100 Subject: [PATCH] Fix ArduinoOTA and put OTA setup in dedicated function --- arduino.cpp | 70 ++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/arduino.cpp b/arduino.cpp index 336a227..6aaea45 100644 --- a/arduino.cpp +++ b/arduino.cpp @@ -57,6 +57,41 @@ void setupWifi() { Serial.println(WiFi.localIP()); } + +void setupOTA() { + 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(); + Serial.println(F("End of OTA setup")); +} /// MQTT #include "Adafruit_MQTT.h" @@ -148,47 +183,16 @@ void setup() { randomSeed(analogRead(0)); setupWifi(); + setupOTA(); // 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(); + MDNS.update(); ArduinoOTA.handle(); Adafruit_MQTT_Subscribe *subscription; -- 2.20.1