Skip to content

Commit

Permalink
added example simpleServerCaptive.ino
Browse files Browse the repository at this point in the history
  • Loading branch information
cotestatnt committed Jan 4, 2024
1 parent 152029a commit b34e1fc
Show file tree
Hide file tree
Showing 9 changed files with 779 additions and 4 deletions.
16 changes: 13 additions & 3 deletions examples/customHTML/customHTML.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const char* hostname = "myserver";
#define FILESYSTEM LittleFS
AsyncFsWebServer server(80, FILESYSTEM, hostname);
bool captiveRun = false;

#ifndef LED_BUILTIN
#define LED_BUILTIN 2
Expand Down Expand Up @@ -151,8 +152,6 @@ void setup() {
if (server.clearOptions())
ESP.restart();
#endif
// Try to connect to stored SSID, start AP if fails after timeout
server.startWiFi(15000, "ESP8266_AP", "123456789" );

// FILESYSTEM INIT
if (startFilesystem()){
Expand All @@ -162,6 +161,16 @@ void setup() {
else
Serial.println(F("Application options NOT loaded!"));
}

// 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;
}

// Add custom page handlers to webserver
server.on("/reload", HTTP_GET, handleLoadOptions);

Expand Down Expand Up @@ -227,5 +236,6 @@ void setup() {


void loop() {

if (captiveRun)
server.updateDNS();
}
2 changes: 1 addition & 1 deletion examples/customOptions/customOptions.ino
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void setup() {
Serial.println(F("Application options NOT loaded!"));
}

// Try to connect to stored SSID, start AP if fails after timeout
// 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");
Expand Down
5 changes: 5 additions & 0 deletions examples/simpleServerCaptive/.vscode/arduino.json
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 examples/simpleServerCaptive/.vscode/c_cpp_properties.json

Large diffs are not rendered by default.

Binary file not shown.
Binary file added examples/simpleServerCaptive/data/favicon.ico
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.
67 changes: 67 additions & 0 deletions examples/simpleServerCaptive/data/index.htm
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>
99 changes: 99 additions & 0 deletions examples/simpleServerCaptive/simpleServerCaptive.ino
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();
}

0 comments on commit b34e1fc

Please sign in to comment.