Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Árni Björnsson committed Oct 19, 2023
1 parent 17b8e99 commit 35921b9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 27 deletions.
15 changes: 15 additions & 0 deletions Lampi.io/data/index.html
@@ -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>
28 changes: 28 additions & 0 deletions Lampi.io/data/style.css
@@ -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;
}
6 changes: 6 additions & 0 deletions Lampi.io/platformio.ini
Expand Up @@ -12,3 +12,9 @@
platform = espressif32
board = seeed_xiao_esp32c3
framework = arduino

# or using GIT Url (the latest development version)
lib_deps =
https://github.com/me-no-dev/ESPAsyncWebServer.git
;ESP Async WebServer
me-no-dev/AsyncTCP@^1.1.1
102 changes: 75 additions & 27 deletions Lampi.io/src/main.cpp
@@ -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(){

}

0 comments on commit 35921b9

Please sign in to comment.