2021年12月4日 星期六

ESP32 Web Server using SPIFFS (SPI Flash File System)

 ESP32 Web Server using SPIFFS (SPI Flash File System)

Control buildin LED GPIO2














/*********

  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 = "REPLACE_WITH_YOUR_SSID";

//const char* password = "REPLACE_WITH_YOUR_PASSWORD";

const char* ssid     = "PTS-2F";

const char* password = "PTS6662594";


// 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(){

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

  }


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

  });

  

  // Route to load style.css file

  server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){

    request->send(SPIFFS, "/style.css", "text/css");

  });


  // Route to set GPIO to HIGH

  server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){

    digitalWrite(ledPin, HIGH);    

    request->send(SPIFFS, "/index.html", String(), false, processor);

  });

  

  // 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(){

  

}



 index.html file

<!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>

style.css file

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

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...