ESP32 – WebServer
Table of Contents
Links
Some testing for Savable Settings via a WebServer
Starter Code Still in Progress
// _ ___ __ _ _
// | || \ \ / / | | (_)
// | || |\ \ /\ / /__ | |__ _
// |__ _\ \/ \/ / _ \| '_ \| |
// | | \ /\ / (_) | |_) | |
// |_| \/ \/ \___/|_.__/|_|.com
//
// Import required libraries
#include
#include
#include
#include "EEPROM.h"
#include "html.h"
const char* PARAM_INPUT_1 = "output";
const char* PARAM_INPUT_2 = "state";
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// Replaces placeholder with button section in your web page
String processor(const String& var) {
//Serial.println(var);
if (var == "BUTTONPLACEHOLDER") {
String buttons = "";
buttons += "Output - GPIO 2
";
buttons += "Output - GPIO 0
";
buttons += "Output - GPIO 4
";
return buttons;
}
return String();
}
String outputState(int output) {
if (digitalRead(output)) {
return "checked";
}
else {
return "";
}
}
void EEPROMSetup() {
// STARTING EEPROM
if (!EEPROM.begin(1000)) {
Serial.println("Failed to initialise EEPROM");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}
int address = 0;
EEPROM.writeFloat(address, 421321.134123);
address += sizeof(float);
String sentence = "I love ESP32.";
EEPROM.writeInt(address, sentence.length());
address += sizeof(int);
EEPROM.writeString(address, sentence);
address += sentence.length() + 1;
EEPROM.writeDouble(address, -123456789.123456789);
address += sizeof(double);
EEPROM.commit();
address = 0;
Serial.println(EEPROM.readFloat(address), 4);
address += sizeof(float);
int i = EEPROM.readInt(address);
Serial.println(i);
address += sizeof(int);
Serial.println(EEPROM.readString(address));
address += i + 1;
Serial.println(EEPROM.readDouble(address), 8);
address += sizeof(double);
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
while (!Serial) continue;
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
pinMode(4 , OUTPUT);
digitalWrite(4, LOW);
EEPROMSetup();
// Connect to Wi-Fi
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
WiFiManager wm;
//wm.resetSettings();
bool res; // WifiManager
res = wm.autoConnect("AutoConnectAP", "password"); // password protected ap
if (!res) {
Serial.println("Failed to connect");
// ESP.restart();
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
// Print ESP Local IP Address
// Serial.println(WiFi.localIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/html", index_html, processor);
});
// Send a GET request to /update?output=&state=
server.on("/update", HTTP_GET, [] (AsyncWebServerRequest * request) {
String inputMessage1;
String inputMessage2;
// GET input1 value on /update?output=&state=
if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) {
inputMessage1 = request->getParam(PARAM_INPUT_1)->value();
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
digitalWrite(inputMessage1.toInt(), inputMessage2.toInt());
}
else {
inputMessage1 = "No message sent";
inputMessage2 = "No message sent";
}
Serial.print("GPIO: ");
Serial.print(inputMessage1);
Serial.print(" - Set to: ");
Serial.println(inputMessage2);
request->send(200, "text/plain", "OK");
});
// Start server
server.begin();
}
void loop() {
}
#ifndef HTML_H
#define HTML_H
const char index_html[] PROGMEM = R"rawliteral(
ESP Web Server
ESP Wobi Web Server
%BUTTONPLACEHOLDER%
)rawliteral";
#endif
–