forked from Juerd/ESP-WiFiSettings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoOTA.ino
59 lines (46 loc) · 1.89 KB
/
ArduinoOTA.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
/*
WifiSettings Arduino OTA example
Demonstrates how to run Arduino OTA in tandem with WifiSettings,
using the WifiSettings credentials.
Source and further documentation available at
https://github.com/Juerd/ESP-WiFiSettings
Note: this example is written for ESP32.
For ESP8266, use LittleFS.begin() instead of SPIFFS.begin(true).
*/
#include <SPIFFS.h>
#include <WiFiSettings.h>
#include <ArduinoOTA.h>
// Start ArduinoOTA via WiFiSettings with the same hostname and password
void setup_ota() {
ArduinoOTA.setHostname(WiFiSettings.hostname.c_str());
ArduinoOTA.setPassword(WiFiSettings.password.c_str());
ArduinoOTA.begin();
}
void setup() {
Serial.begin(115200);
SPIFFS.begin(true); // Will format on the first run after failing to mount
// Force WPA secured WiFi for the software access point.
// Because OTA is remote code execution (RCE) by definition, the password
// should be kept secret. By default, WiFiSettings will become an insecure
// WiFi access point and happily tell anyone the password. The password
// will instead be provided on the Serial connection, which is a bit safer.
WiFiSettings.secure = true;
// Set callbacks to start OTA when the portal is active
WiFiSettings.onPortal = []() {
setup_ota();
};
WiFiSettings.onPortalWaitLoop = []() {
ArduinoOTA.handle();
};
// Use stored credentials to connect to your WiFi access point.
// If no credentials are stored or if the access point is out of reach,
// an access point will be started with a captive portal to configure WiFi.
WiFiSettings.connect();
Serial.print("Password: ");
Serial.println(WiFiSettings.password);
setup_ota(); // If you also want the OTA during regular execution
}
void loop() {
ArduinoOTA.handle(); // If you also want the OTA during regular execution
// Your loop code here
}