// -------------------------------------------------------------
// Libraries
// -------------------------------------------------------------
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// -------------------------------------------------------------
// Variables
// -------------------------------------------------------------
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
WebServer server(80);
// -------------------------------------------------------------
// Structs
// -------------------------------------------------------------
struct TelemetryPoint {
char label;
float value;
char unit;
char message[256];
};
int sensorsPins[] = {32, 33, 34, 35};
int sensorsPinsCount = 4;
TelemetryPoint telemetry[4];
// -------------------------------------------------------------
// Functions
// -------------------------------------------------------------
void sendHtml() {
String response = R"(
<!DOCTYPE html><html>
<head>
<title>ESP32 Web Server Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html { font-family: sans-serif; text-align: center; }
body { display: inline-flex; flex-direction: column; }
h1 { margin-bottom: 1.2em; }
h2 { margin: 0; }
div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }
.btn { background-color: #5B5; border: none; color: #fff; padding: 0.5em 1em;
font-size: 2em; text-decoration: none }
.btn.OFF { background-color: #333; }
</style>
</head>
<body>
<h1>ESP32 Web Server</h1>
<div>
<h2>LED 1</h2>
<a href="/toggle/1" class="btn LED1_TEXT">LED1_TEXT</a>
<h2>LED 2</h2>
<a href="/toggle/2" class="btn LED2_TEXT">LED2_TEXT</a>
</div>
</body>
</html>
)";
// response.replace("LED1_TEXT", led1State ? "ON" : "OFF");
// response.replace("LED2_TEXT", led2State ? "ON" : "OFF");
server.send(200, "text/html", response);
}
void doReadings () {
// Get each pin reading
for (int thisPin = 0; thisPin < sensorsPinsCount; thisPin++) {
float result = analogRead(sensorsPins[thisPin]);
float voltage1 = map(result, 0, 4095, 0, 120);
char message[128];
sprintf(message, "Pin : %d, Raw Signal : %d, Mapped Signal : %.2f, 12V Signal : %.2f%s", sensorsPins[thisPin], result, voltage1, (voltage1 / 10.0), telemetry[thisPin].unit);
Serial.println(message);
telemetry[thisPin].label = 'Voltage';
telemetry[thisPin].value = voltage1 / 10;
telemetry[thisPin].unit = 'V';
strcpy(telemetry[thisPin].message, message);
}
}
void printReadings() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.println("IP address: " + WiFi.localIP());
for (int thisPin = 0; thisPin < sensorsPinsCount; thisPin++) {
tft.println(telemetry[thisPin].message);
}
}
// -------------------------------------------------------------
// Setup
// -------------------------------------------------------------
void setup(void) {
Serial.begin(115200);
// SETUP OUR INPUT PINS
for (int thisPin = 0; thisPin < sensorsPinsCount; thisPin++) {
pinMode(sensorsPins[thisPin], INPUT);
}
tft.begin();
tft.setRotation(1);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Connecting to WiFi");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
tft.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
tft.print("\nOK! IP=");
tft.println(WiFi.localIP());
server.on("/", sendHtml);
server.on(UriBraces("/toggle/{}"), []() {
String led = server.pathArg(0);
Serial.print("Toggle LED #");
Serial.println(led);
// switch (led.toInt()) {
// case 1:
// led1State = !led1State;
// digitalWrite(LED1, led1State);
// break;
// case 2:
// led2State = !led2State;
// digitalWrite(LED2, led2State);
// break;
// }
sendHtml();
});
server.begin();
Serial.println("HTTP server started");
}
// -------------------------------------------------------------
// Main Loop
// -------------------------------------------------------------
void loop(void) {
server.handleClient();
doReadings();
printReadings();
delay(2000);
}