-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcallbacks.ino
83 lines (66 loc) · 2.21 KB
/
callbacks.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <Arduino.h>
#include <WiFi.h>
#include <PrettyOTA.h>
const char* WIFI_SSID = "YOUR_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
AsyncWebServer server(80); // Server on port 80 (HTTP)
PrettyOTA OTAUpdates;
// Gets called when update starts
// updateMode can be FILESYSTEM or FIRMWARE
void OnOTAStart(NSPrettyOTA::UPDATE_MODE updateMode)
{
Serial.println("OTA update started");
if(updateMode == NSPrettyOTA::UPDATE_MODE::FIRMWARE)
Serial.println("Mode: Firmware");
else if(updateMode == NSPrettyOTA::UPDATE_MODE::FILESYSTEM)
Serial.println("Mode: Filesystem");
}
// Gets called while update is running
// currentSize: Number of bytes already processed
// totalSize: Total size of new firmware in bytes
void OnOTAProgress(uint32_t currentSize, uint32_t totalSize)
{
Serial.printf("OTA Progress Current: %u bytes, Total: %u bytes\n", currentSize, totalSize);
}
// Gets called when update finishes
void OnOTAEnd(bool successful)
{
if (successful)
Serial.println("OTA update finished successfully");
else
Serial.println("OTA update failed");
}
void setup()
{
Serial.begin(115200);
// Initialize WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// Wait for successful WiFi connection
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.println("[WiFi] Connection failed! Rebooting...");
delay(3000);
ESP.restart();
}
// Print IP address
Serial.println("PrettyOTA can be accessed at: http://" + WiFi.localIP().toString() + "/update");
// Initialize PrettyOTA and set username and password for authentication
OTAUpdates.Begin(&server, "admin", "123");
// Set unique Hardware-ID for your hardware/board
OTAUpdates.SetHardwareID("UniqueBoard1");
// Set firmware version to 1.0.0
OTAUpdates.OverwriteAppVersion("1.0.0");
// Set current build time and date
PRETTY_OTA_SET_CURRENT_BUILD_TIME_AND_DATE();
// Set custom callbacks
OTAUpdates.OnStart(OnOTAStart);
OTAUpdates.OnProgress(OnOTAProgress);
OTAUpdates.OnEnd(OnOTAEnd);
// Start web server
server.begin();
}
void loop()
{
// Give CPU time to other running tasks
delay(100);
}