-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esp32c3 + me-no-dev/ESPAsyncWebServer#1142 (review) : working
- Loading branch information
Árni Björnsson
committed
Oct 19, 2023
1 parent
17b8e99
commit 35921b9
Showing
4 changed files
with
124 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>ESP32 Web Server</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="icon" href="data:,"> | ||
<link rel="stylesheet" type="text/css" href="style.css"> | ||
</head> | ||
<body> | ||
<h1>ESP32 Web Server</h1> | ||
<p>GPIO state: <strong> %STATE%</strong></p> | ||
<p><a href="/on"><button class="button">ON</button></a></p> | ||
<p><a href="/off"><button class="button button2">OFF</button></a></p> | ||
</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,28 @@ | ||
html { | ||
font-family: Helvetica; | ||
display: inline-block; | ||
margin: 0px auto; | ||
text-align: center; | ||
} | ||
h1{ | ||
color: #0F3376; | ||
padding: 2vh; | ||
} | ||
p{ | ||
font-size: 1.5rem; | ||
} | ||
.button { | ||
display: inline-block; | ||
background-color: #008CBA; | ||
border: none; | ||
border-radius: 4px; | ||
color: white; | ||
padding: 16px 40px; | ||
text-decoration: none; | ||
font-size: 30px; | ||
margin: 2px; | ||
cursor: pointer; | ||
} | ||
.button2 { | ||
background-color: #f44336; | ||
} |
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 |
---|---|---|
@@ -1,40 +1,88 @@ | ||
/********* | ||
Rui Santos | ||
Complete project details at https://randomnerdtutorials.com | ||
*********/ | ||
|
||
// Import required libraries | ||
#include "WiFi.h" | ||
#include "ESPAsyncWebServer.h" | ||
#include "SPIFFS.h" | ||
|
||
// Replace with your network credentials | ||
const char* ssid = "Hotspot"; | ||
const char* password = "Password"; | ||
|
||
// Set LED GPIO | ||
const int ledPin = 2; | ||
// Stores LED state | ||
String ledState; | ||
|
||
// Create AsyncWebServer object on port 80 | ||
AsyncWebServer server(80); | ||
|
||
// Replaces placeholder with LED state value | ||
String processor(const String& var){ | ||
Serial.println(var); | ||
if(var == "STATE"){ | ||
if(digitalRead(ledPin)){ | ||
ledState = "ON"; | ||
} | ||
else{ | ||
ledState = "OFF"; | ||
} | ||
Serial.print(ledState); | ||
return ledState; | ||
} | ||
return String(); | ||
} | ||
|
||
void setup() { | ||
void setup(){ | ||
// Serial port for debugging purposes | ||
Serial.begin(115200); | ||
|
||
pinMode(ledPin, OUTPUT); | ||
|
||
// Initialize SPIFFS | ||
if(!SPIFFS.begin(true)){ | ||
Serial.println("An Error has occurred while mounting SPIFFS"); | ||
return; | ||
} | ||
|
||
File file = SPIFFS.open("/test_example.txt"); | ||
if(!file){ | ||
Serial.println("Failed to open file for reading"); | ||
return; | ||
|
||
// Connect to Wi-Fi | ||
WiFi.begin(ssid, password); | ||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(1000); | ||
Serial.println("Connecting to WiFi.."); | ||
} | ||
|
||
// Print ESP32 Local IP Address | ||
Serial.println(WiFi.localIP()); | ||
|
||
// Route for root / web page | ||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ | ||
request->send(SPIFFS, "/index.html", String(), false, processor); | ||
}); | ||
|
||
Serial.println("File Content:"); | ||
while(file.available()){ | ||
Serial.write(file.read()); | ||
} | ||
file.close(); | ||
} | ||
|
||
void loop() { | ||
delay(2000); | ||
// Route to load style.css file | ||
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){ | ||
request->send(SPIFFS, "/style.css", "text/css"); | ||
}); | ||
|
||
File file = SPIFFS.open("/test_example.txt"); | ||
if(!file){ | ||
Serial.println("Failed to open file for reading"); | ||
return; | ||
} | ||
// Route to set GPIO to HIGH | ||
server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){ | ||
digitalWrite(ledPin, HIGH); | ||
request->send(SPIFFS, "/index.html", String(), false, processor); | ||
}); | ||
|
||
Serial.println("File Content:"); | ||
while(file.available()){ | ||
Serial.write(file.read()); | ||
} | ||
Serial.println(""); | ||
file.close(); | ||
// Route to set GPIO to LOW | ||
server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request){ | ||
digitalWrite(ledPin, LOW); | ||
request->send(SPIFFS, "/index.html", String(), false, processor); | ||
}); | ||
|
||
// Start server | ||
server.begin(); | ||
} | ||
|
||
void loop(){ | ||
|
||
} |