Created
November 10, 2020 09:10
-
-
Save lennonjesus/2f0dfa74cd796cac4acb3aeb3c69b1ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ArduinoOTA.h> | |
const char *ssid = ""; | |
const char *pass = ""; | |
void setup() { | |
Serial.begin(115200); | |
delay(1000); | |
connectWiFi(); | |
configureOta(); | |
delay(10); | |
} | |
void loop() { | |
delay(500); | |
checkWifi(); | |
ArduinoOTA.handle(); | |
delay (1000); | |
} | |
void checkWifi() { | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.println(" Conexão não estabelecida. Reiniciando..."); | |
ESP.restart(); | |
} | |
} | |
void connectWiFi() { | |
delay(100); | |
WiFi.begin(ssid, pass); | |
Serial.print("Conectando em "); | |
Serial.print(ssid); | |
int timeout = 0; | |
while (WiFi.status() != WL_CONNECTED && ++timeout <= 10) { | |
digitalWrite(BUILTIN_LED, HIGH); | |
delay(500); | |
digitalWrite(BUILTIN_LED, LOW); | |
delay(500); | |
Serial.print("."); | |
} | |
checkWifi(); | |
Serial.println(""); | |
Serial.print("Conectado em "); | |
Serial.print(ssid); | |
Serial.print(" com o IP "); | |
Serial.println(WiFi.localIP()); | |
} | |
void configureOta() { | |
// ArduinoOTA.setPort(8266); | |
ArduinoOTA.setHostname("Smart Garden - uv"); | |
ArduinoOTA.setPassword((const char *) "143252112"); | |
ArduinoOTA.onStart([]() { | |
Serial.println("Inicio..."); | |
}); | |
ArduinoOTA.onEnd([]() { | |
Serial.println("nFim!"); | |
}); | |
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { | |
Serial.printf("Progresso: %u%%r", (progress / (total / 100))); | |
}); | |
ArduinoOTA.onError([](ota_error_t error) { | |
Serial.printf("Erro [%u]: ", error); | |
if (error == OTA_AUTH_ERROR) Serial.println("Autenticacao Falhou"); | |
else if (error == OTA_BEGIN_ERROR) Serial.println("Falha no Inicio"); | |
else if (error == OTA_CONNECT_ERROR) Serial.println("Falha na Conexao"); | |
else if (error == OTA_RECEIVE_ERROR) Serial.println("Falha na Recepcao"); | |
else if (error == OTA_END_ERROR) Serial.println("Falha no Fim"); | |
}); | |
ArduinoOTA.begin(); | |
Serial.println("Pronto"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment