Skip to content

Commit b1dc4c5

Browse files
committed
esp01-ota: Add example application for using ArduinoOTA
1 parent 2dbb232 commit b1dc4c5

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/esp01-ota/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include ../esp01-passwords.mk
2+
include ../esp01.mk

src/esp01-ota/esp01-ota.ino

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)