Last active
January 23, 2021 10:19
-
-
Save dennisheitmann/20ad64624e4a55fb59ec0cd2f6a218b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <ESPmDNS.h> | |
#include <WiFiUdp.h> | |
#include <ArduinoOTA.h> | |
int ota_once = 0; | |
#define HOSTNAME "ESP32TemplateSketch" | |
#include <IotWebConf.h> | |
// Initial name of the Thing. Used e.g. as SSID of the own Access Point. | |
const char thingName[] = "ESP32config"; | |
// Initial password to connect to the Thing, when it creates an own Access Point. | |
const char wifiInitialApPassword[] = "ESP32config"; | |
DNSServer dnsServer; | |
WebServer server(80); | |
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword); | |
void handleRoot() | |
{ | |
// Let IotWebConf test and handle captive portal requests. | |
if (iotWebConf.handleCaptivePortal()) | |
{ | |
// Captive portal request were already served. | |
return; | |
} | |
String s = "<!DOCTYPE html><html lang=\"en\"><head>"; | |
s += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>"; | |
s += "<title>IotWebConf</title></head><body>"; | |
s += "Go to <a href='config'>configure page</a> to change settings."; | |
s += "</body></html>\n"; | |
server.send(200, "text/html", s); | |
} | |
void setupOTA() { | |
// Port defaults to 3232 | |
// ArduinoOTA.setPort(3232); | |
// No authentication by default | |
// ArduinoOTA.setPassword("admin"); | |
// Password can be set with it's md5 value as well | |
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 | |
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); | |
ArduinoOTA | |
.onStart([]() { | |
String type; | |
if (ArduinoOTA.getCommand() == U_FLASH) | |
type = "sketch"; | |
else // U_SPIFFS | |
type = "filesystem"; | |
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() | |
Serial.println("Start updating " + type); | |
}) | |
.onEnd([]() { | |
Serial.println("\nEnd"); | |
}) | |
.onProgress([](unsigned int progress, unsigned int total) { | |
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); | |
}) | |
.onError([](ota_error_t error) { | |
Serial.printf("Error[%u]: ", error); | |
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); | |
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); | |
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); | |
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); | |
else if (error == OTA_END_ERROR) Serial.println("End Failed"); | |
}); | |
ArduinoOTA.begin(); | |
Serial.println("OTA Initialized"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Booting"); | |
// Initializing the configuration. | |
iotWebConf.init(); | |
// Set up required URL handlers on the web server. | |
server.on("/", handleRoot); | |
server.on("/config", [] { iotWebConf.handleConfig(); }); | |
server.onNotFound([]() { | |
iotWebConf.handleNotFound(); | |
}); | |
delay(100); | |
// Your setup code here | |
} | |
void loop() { | |
// Check if Wifi is connected | |
if (WiFi.status() == WL_CONNECTED) { | |
// On the first time Wifi is connected, setup OTA | |
if (ota_once == 0) { | |
ArduinoOTA.setHostname(HOSTNAME); | |
setupOTA(); | |
ota_once = 1; | |
} else { | |
ArduinoOTA.handle(); | |
} | |
} | |
iotWebConf.doLoop(); | |
// Your loop code here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment