#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
#define WIFI_SSID "Atharva's Galaxy"
#define WIFI_PASSWORD "samsungS24"
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
WebServer server(80);
// Original pins (kept for compatibility)
const int LED1 = 19; // repurposed for RELAY by default
const int LED2 = 27; // stays as a regular LED
// Relay configuration (uses LED1 pin by default)
const int RELAY_PIN = LED1; // change this if your relay is wired to other GPIO
const bool RELAY_ACTIVE_LOW = true; // set to false if your relay activates on HIGH
bool led1State = false; // used as relay state (false = off, true = on)
bool led2State = false;
void applyRelayOutput() {
// Drive the relay pin according to led1State and whether it's active-low
if (RELAY_ACTIVE_LOW) {
// ON -> LOW, OFF -> HIGH
digitalWrite(RELAY_PIN, led1State ? LOW : HIGH);
} else {
// ON -> HIGH, OFF -> LOW
digitalWrite(RELAY_PIN, led1State ? HIGH : LOW);
}
}
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; border-radius:8px; display:inline-block; }
.btn.OFF { background-color: #333; }
</style>
</head>
<body>
<h1>ESP32 Web Server</h1>
<div>
<h2>Relay (Buzzer)</h2>
<a href="/toggle/1" class="btn RELAY_TEXT">RELAY_TEXT</a>
<h2>LED 2</h2>
<a href="/toggle/2" class="btn LED2_TEXT">LED2_TEXT</a>
</div>
</body>
</html>
)";
// Replace placeholders with actual states
response.replace("RELAY_TEXT", led1State ? "ON" : "OFF");
response.replace("LED2_TEXT", led2State ? "ON" : "OFF");
server.send(200, "text/html", response);
}
void setup(void) {
Serial.begin(115200);
// Initialize pins
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED2, OUTPUT);
// Ensure relay is OFF at boot
led1State = false;
applyRelayOutput();
digitalWrite(LED2, LOW);
led2State = false;
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", sendHtml);
server.on(UriBraces("/toggle/{}"), []() {
String which = server.pathArg(0);
Serial.print("Toggle #");
Serial.println(which);
switch (which.toInt()) {
case 1:
// Toggle relay (using led1State variable)
led1State = !led1State;
applyRelayOutput();
Serial.printf("Relay -> %s\n", led1State ? "ON" : "OFF");
break;
case 2:
// Toggle LED2 (regular LED)
led2State = !led2State;
digitalWrite(LED2, led2State ? HIGH : LOW);
Serial.printf("LED2 -> %s\n", led2State ? "ON" : "OFF");
break;
default:
Serial.println("Unknown toggle");
break;
}
sendHtml();
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
delay(2);
}