Skip to content

Instantly share code, notes, and snippets.

@nsfilho
Created July 23, 2020 18:12
Show Gist options
  • Save nsfilho/6a2c9d8d1b0827869077f301751044cb to your computer and use it in GitHub Desktop.
Save nsfilho/6a2c9d8d1b0827869077f301751044cb to your computer and use it in GitHub Desktop.
Esp8266 Arduino Framework Simple OTA
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
const char* ssid = "NOME DA SUA REDE WIFI";
const char* password = "SENHA DA SUA REDE WIFI";
void setup() {
Serial.begin(115200);
Serial.println("Iniciando...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Conexao falhou! Reiniciando...");
delay(5000);
ESP.restart();
}
// Porta padrao do ESP8266 para OTA eh 8266 - Voce pode mudar ser quiser, mas deixe indicado!
// ArduinoOTA.setPort(8266);
// O Hostname padrao eh esp8266-[ChipID], mas voce pode mudar com essa funcao
// ArduinoOTA.setHostname("nome_do_meu_esp8266");
// Nenhuma senha eh pedida, mas voce pode dar mais seguranca pedindo uma senha pra gravar
// ArduinoOTA.setPassword((const char *)"123");
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");
Serial.print("Endereco IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
ArduinoOTA.handle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment