Fix ArduinoOTA and put OTA setup in dedicated function #2

Open
Lomanic wants to merge 1 commits from Lomanic/PiedThon:arduinoota-fix into master
Showing only changes of commit cc8707b9c4 - Show all commits

View File

@ -57,6 +57,41 @@ void setupWifi() {
Serial.println(WiFi.localIP()); 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 /// MQTT
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT.h"
@ -148,47 +183,16 @@ void setup() {
randomSeed(analogRead(0)); randomSeed(analogRead(0));
setupWifi(); setupWifi();
setupOTA();
// Setup MQTT subscription for onoff feed. // Setup MQTT subscription for onoff feed.
mqtt.subscribe(&sub_piedthon); 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() { void loop() {
MQTT_connect(); MQTT_connect();
MDNS.update();
ArduinoOTA.handle(); ArduinoOTA.handle();
Adafruit_MQTT_Subscribe *subscription; Adafruit_MQTT_Subscribe *subscription;