|
| 1 | +#include <ESP8266WiFi.h> |
| 2 | +#include <ESP8266mDNS.h> |
| 3 | +#include <WiFiUdp.h> |
| 4 | +#include <ArduinoOTA.h> |
| 5 | + |
| 6 | +// Replace with your network details |
| 7 | +//#define WIFI_SSID "" |
| 8 | +//#define WIFI_PASSWORD "" |
| 9 | +//#define OTA_HOSTNAME "" |
| 10 | +//#define OTA_PORT 8266 |
| 11 | +//#define OTA_PASSWORD "" |
| 12 | + |
| 13 | +void setup() { |
| 14 | + Serial.begin(115200); |
| 15 | + Serial.println("Booting"); |
| 16 | + |
| 17 | + WiFi.mode(WIFI_STA); |
| 18 | + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
| 19 | + while (WiFi.waitForConnectResult() != WL_CONNECTED) { |
| 20 | + Serial.println("Connection Failed! Rebooting..."); |
| 21 | + delay(5000); |
| 22 | + ESP.restart(); |
| 23 | + } |
| 24 | + |
| 25 | + Serial.println("WiFi connected!"); |
| 26 | + |
| 27 | +#ifdef OTA_HOSTNAME |
| 28 | + // Hostname defaults to esp8266-[ChipID] |
| 29 | + ArduinoOTA.setHostname(OTA_HOSTNAME); |
| 30 | +#endif |
| 31 | +#ifdef OTA_PORT |
| 32 | + // Port defaults to 8266 |
| 33 | + ArduinoOTA.setPort(OTA_PORT); |
| 34 | +#endif |
| 35 | +#ifdef OTA_PASSWORD |
| 36 | + // No authentication by default |
| 37 | + ArduinoOTA.setPassword(OTA_PASSWORD); |
| 38 | +#endif |
| 39 | + |
| 40 | + ArduinoOTA.onStart([]() { |
| 41 | + String type; |
| 42 | + if (ArduinoOTA.getCommand() == U_FLASH) { |
| 43 | + type = "sketch"; |
| 44 | + } else { // U_FS |
| 45 | + type = "filesystem"; |
| 46 | + } |
| 47 | + |
| 48 | + // NOTE: if updating FS this would be the place to unmount FS using FS.end() |
| 49 | + Serial.println("Start updating " + type); |
| 50 | + }); |
| 51 | + ArduinoOTA.onEnd([]() { |
| 52 | + Serial.println("\nEnd"); |
| 53 | + }); |
| 54 | + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { |
| 55 | + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); |
| 56 | + }); |
| 57 | + ArduinoOTA.onError([](ota_error_t error) { |
| 58 | + Serial.printf("Error[%u]: ", error); |
| 59 | + if (error == OTA_AUTH_ERROR) { |
| 60 | + Serial.println("Auth Failed"); |
| 61 | + } else if (error == OTA_BEGIN_ERROR) { |
| 62 | + Serial.println("Begin Failed"); |
| 63 | + } else if (error == OTA_CONNECT_ERROR) { |
| 64 | + Serial.println("Connect Failed"); |
| 65 | + } else if (error == OTA_RECEIVE_ERROR) { |
| 66 | + Serial.println("Receive Failed"); |
| 67 | + } else if (error == OTA_END_ERROR) { |
| 68 | + Serial.println("End Failed"); |
| 69 | + } |
| 70 | + }); |
| 71 | + ArduinoOTA.begin(); |
| 72 | + Serial.println("Ready"); |
| 73 | + Serial.print("IP address: "); |
| 74 | + Serial.println(WiFi.localIP()); |
| 75 | +} |
| 76 | + |
| 77 | +void loop() { |
| 78 | + ArduinoOTA.handle(); |
| 79 | +} |
0 commit comments