-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added example simpleServerCaptive.ino
- Loading branch information
1 parent
152029a
commit b34e1fc
Showing
9 changed files
with
779 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"configuration": "JTAGAdapter=default,PSRAM=disabled,FlashMode=qio,FlashSize=4M,LoopCore=1,EventsCore=1,USBMode=hwcdc,CDCOnBoot=default,MSCOnBoot=default,DFUOnBoot=default,UploadMode=default,PartitionScheme=default,CPUFreq=240,UploadSpeed=921600,DebugLevel=none,EraseFlash=none", | ||
"board": "esp32:esp32:esp32s3", | ||
"sketch": "simpleServerCaptive.ino" | ||
} |
594 changes: 594 additions & 0 deletions
594
examples/simpleServerCaptive/.vscode/c_cpp_properties.json
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | ||
<title>ESP simpleServer.ino</title> | ||
|
||
<!--Pico - Minimal CSS Framework for semantic info: HTML https://picocss.com/ --> | ||
<link rel="stylesheet" type="text/css" href="css/pico.fluid.classless.css"> | ||
|
||
<!-- Cyustom page styling --> | ||
<style> | ||
html { | ||
background-color: #92938fd1 | ||
} | ||
.container { | ||
display: flex; | ||
justify-content: center; | ||
min-height: calc(100vh - 6rem); | ||
padding: 1rem 0; | ||
|
||
} | ||
.container { | ||
max-width: 860px; | ||
padding: 40px; | ||
} | ||
</style> | ||
|
||
</head> | ||
<body> | ||
<main class="container"> | ||
<article class="grid"> | ||
<div> | ||
<hgroup> | ||
<h1>ESP FS WebServer - LED Switcher</h1> | ||
</hgroup> | ||
<label for="remember"> | ||
<input type="checkbox" role="switch" id="toggle-led" name="toggle-led"> | ||
Toggle built-in LED | ||
</label> | ||
<br> | ||
<p id="esp-response"></p> | ||
<img src="img/espressif.jpg" style="opacity: 0.75"/> | ||
</div> | ||
</article> | ||
</main> | ||
|
||
<script type="text/javascript"> | ||
// This function fetch the GET request to the ESP webserver | ||
function toggleLed() { | ||
const pars = new URLSearchParams({ | ||
val: document.getElementById('toggle-led').checked ? '1' : '0' | ||
}); | ||
|
||
fetch('/led?' + pars ) // Do the request | ||
.then(response => response.text()) // Parse the response | ||
.then(text => { // DO something with response | ||
console.log(text); | ||
document.getElementById('esp-response').innerHTML = text + ' <i>(Builtin LED is ON with a low signal)</i>'; | ||
}); | ||
} | ||
|
||
// Add event listener to the LED checkbox (the function will be called on every change) | ||
document.getElementById('toggle-led').addEventListener('change', toggleLed ); | ||
</script> | ||
|
||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include <FS.h> | ||
#include <LittleFS.h> | ||
#include <AsyncFsWebServer.h> | ||
|
||
AsyncFsWebServer server(80, LittleFS, "myServer"); | ||
bool captiveRun = false; | ||
|
||
#ifndef LED_BUILTIN | ||
#define LED_BUILTIN 2 | ||
#endif | ||
uint8_t ledPin = LED_BUILTIN; | ||
|
||
//////////////////////////////// Filesystem ///////////////////////////////////////// | ||
bool startFilesystem() { | ||
if (LittleFS.begin()){ | ||
server.printFileList(LittleFS, "/", 1); | ||
return true; | ||
} | ||
else { | ||
Serial.println("ERROR on mounting filesystem. It will be formmatted!"); | ||
LittleFS.format(); | ||
ESP.restart(); | ||
} | ||
return false; | ||
} | ||
|
||
|
||
/* | ||
* Getting FS info (total and free bytes) is strictly related to | ||
* filesystem library used (LittleFS, FFat, SPIFFS etc etc) and ESP framework | ||
*/ | ||
#ifdef ESP32 | ||
void getFsInfo(fsInfo_t* fsInfo) { | ||
fsInfo->totalBytes = LittleFS.totalBytes(); | ||
fsInfo->usedBytes = LittleFS.usedBytes(); | ||
strcpy(fsInfo->fsName, "LittleFS"); | ||
} | ||
#endif | ||
|
||
|
||
//--------------------------------------- | ||
void handleLed(AsyncWebServerRequest *request) { | ||
static int value = false; | ||
// http://xxx.xxx.xxx.xxx/led?val=1 | ||
if(request->hasParam("val")) { | ||
value = request->arg("val").toInt(); | ||
digitalWrite(ledPin, value); | ||
} | ||
String reply = "LED is now "; | ||
reply += value ? "ON" : "OFF"; | ||
request->send(200, "text/plain", reply); | ||
} | ||
|
||
|
||
void setup() { | ||
pinMode(ledPin, OUTPUT); | ||
Serial.begin(115200); | ||
delay(1000); | ||
|
||
// Init and start LittleFS file system | ||
startFilesystem(); | ||
|
||
// Try to connect to stored SSID, start AP with captive portal if fails after timeout | ||
IPAddress myIP = server.startWiFi(15000); | ||
if (!myIP) { | ||
Serial.println("\n\nNo WiFi connection, start AP and Captive Portal\n"); | ||
server.startCaptivePortal("ESP_AP", "123456789", "/setup"); | ||
myIP = WiFi.softAPIP(); | ||
captiveRun = true; | ||
} | ||
|
||
// Set a custom /setup page title | ||
server.setSetupPageTitle("Simple Async FS<br>Web Server"); | ||
|
||
// Enable ACE FS file web editor and add FS info callback fucntion | ||
server.enableFsCodeEditor(); | ||
#ifdef ESP32 | ||
server.setFsInfoCallback(getFsInfo); | ||
#endif | ||
|
||
// Add 0 callback function handler | ||
server.on("/led", HTTP_GET, handleLed); | ||
|
||
// Start server | ||
server.init(); | ||
Serial.print(F("ESP Web Server started on IP Address: ")); | ||
Serial.println(myIP); | ||
Serial.println(F( | ||
"This is \"simpleServer.ino\" example.\n" | ||
"Open /setup page to configure optional parameters.\n" | ||
"Open /edit page to view, edit or upload example or your custom webserver source files." | ||
)); | ||
|
||
} | ||
|
||
void loop() { | ||
if (captiveRun) | ||
server.updateDNS(); | ||
} |