|
| 1 | +#include <WiFi.h> |
| 2 | +#include <ESPmDNS.h> |
| 3 | +#include <WiFiUdp.h> |
| 4 | +#include <ArduinoOTA.h> |
| 5 | + |
| 6 | +const char* ssid = ".........."; |
| 7 | +const char* password = ".........."; |
| 8 | + |
| 9 | +void setup() { |
| 10 | + Serial.begin(115200); |
| 11 | + Serial.println("Booting"); |
| 12 | + WiFi.mode(WIFI_STA); |
| 13 | + WiFi.begin(ssid, password); |
| 14 | + while (WiFi.waitForConnectResult() != WL_CONNECTED) { |
| 15 | + Serial.println("Connection Failed! Rebooting..."); |
| 16 | + delay(5000); |
| 17 | + ESP.restart(); |
| 18 | + } |
| 19 | + |
| 20 | + // Port defaults to 3232 |
| 21 | + // ArduinoOTA.setPort(3232); |
| 22 | + |
| 23 | + // Hostname defaults to esp3232-[MAC] |
| 24 | + // ArduinoOTA.setHostname("myesp32"); |
| 25 | + |
| 26 | + // No authentication by default |
| 27 | + // ArduinoOTA.setPassword("admin"); |
| 28 | + |
| 29 | + // Password can be set with it's md5 value as well |
| 30 | + // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 |
| 31 | + // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); |
| 32 | + |
| 33 | + ArduinoOTA.onStart([]() { |
| 34 | + String type; |
| 35 | + if (ArduinoOTA.getCommand() == U_FLASH) |
| 36 | + type = "sketch"; |
| 37 | + else // U_SPIFFS |
| 38 | + type = "filesystem"; |
| 39 | + |
| 40 | + // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() |
| 41 | + Serial.println("Start updating " + type); |
| 42 | + }); |
| 43 | + ArduinoOTA.onEnd([]() { |
| 44 | + Serial.println("\nEnd"); |
| 45 | + }); |
| 46 | + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { |
| 47 | + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); |
| 48 | + }); |
| 49 | + ArduinoOTA.onError([](ota_error_t error) { |
| 50 | + Serial.printf("Error[%u]: ", error); |
| 51 | + if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); |
| 52 | + else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); |
| 53 | + else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); |
| 54 | + else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); |
| 55 | + else if (error == OTA_END_ERROR) Serial.println("End Failed"); |
| 56 | + }); |
| 57 | + ArduinoOTA.begin(); |
| 58 | + Serial.println("Ready"); |
| 59 | + Serial.print("IP address: "); |
| 60 | + Serial.println(WiFi.localIP()); |
| 61 | +} |
| 62 | + |
| 63 | +void loop() { |
| 64 | + ArduinoOTA.handle(); |
| 65 | +} |
0 commit comments