Fix ArduinoOTA and put OTA setup in dedicated function

This commit is contained in:
Lomanic 2019-11-26 23:46:42 +01:00
parent a9174a59b8
commit cc8707b9c4

View File

@ -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;