Created
January 21, 2025 14:13
-
-
Save davepermen/78de68cc1704605ad32a837d32071290 to your computer and use it in GitHub Desktop.
basic web-device setup routine
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
const char *ssid; | |
const char *ssidpass; | |
const char *webrequest; | |
bool loadsettings() { | |
if(FS.exists("/ssid") == false && FS.exists("/ssidpass") == false && FS.exists("/webrequest") == false) { | |
return false; | |
} | |
ssid = FS.read("/ssid"); | |
ssidpass = FS.read("/ssidpass"); | |
return true; | |
} | |
void handlesetupasneeded() { | |
auto ssids = WiFi.getvisiblessids(); | |
if(!ResetButton.doublereset() && loadsettings() && ssids.contains(ssid)) { | |
return; | |
} | |
WiFi.starthotspotmode(); | |
Server server(80); | |
server.get("/", [](auto request) { | |
request.send('<html>...<form action=post>...'); | |
request.send('<select name=ssid>'); | |
for(var i = 0; i < ssids.length; i++) { | |
var ssid = ssids[i]; | |
request.send('<option value=').send(ssid).send('>').send(ssid).send('</option>'); | |
} | |
request.send('<input type=password name=ssidpass>'); | |
request.send('<input type=text name=webrequest>'); | |
request.send('...</form>...</html>'); | |
}) | |
server.post("/", [](auto request) { | |
FS.write("/ssid", request.form.ssid); | |
FS.write("/ssidpass", request.form.ssidpass); | |
FS.write("/webrequest", request.form.webrequest); | |
request.send('device is set up, should show up online in the dashboard'); | |
ESP.restart(); | |
}) | |
server.listen(); | |
} |
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
extern const char *ssid; | |
extern const char *ssidpass; | |
extern const char *webrequest; | |
void handlesetupasneeded(); |
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 'devicesetup.h' | |
void setup() { | |
handlesetupasneeded() | |
/* insert actual code */ | |
} | |
void loop() { | |
/* insert actual code */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment