Project Goal
I want to have 4 LED Diods in my house: Each one in the kitchen, staircase, livingroom and bedroom.
If I turn on the one in the kitchen via a manual button OR webbrowser, the others should turn on too.
My Idea
Use four ESP32 S2 mini. One acts as a webserver and three others as clients.
The LED diodes are connected to a pin with a resistor.
How it should work
Basically it is just an simple value (on or off) that changes via the web interface or the button. The clients read that.
Progress so far
I found tons of sketches for similar scripts but I cannot manage to get my project to work.
Mostly the sketches don't store the on/off value on the server, but just send an on / off command to the client or it only works on one controller but not all of them.
The sketches need to have these components:
Serverside
- Establish a webserver that stores the current state (LED on or off)
- Offers a webinterface
- Toggles the LED state by pressing a manual button
- Toggles the LED state by a virtual button in the webinterface in the browser
Clientside
- Requests the LED state every 60 seconds
- Turns LED on / off
So far, my server sketch looks like the following.
However I do not know how I can "publish" the current LED state for the clients to request and how to request it on the clientside
int LED_PIN = 4; // LED Pin
int BUTTON_PIN = 5; // Button Pin
int ledState = LOW; // the current state of LED
String ledStateNumeric = "OFF"; // Have a word for the LED status
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
#include <WiFi.h>
const char* ssid = "#";
const char* password = "#";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if (lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
ledStateNumeric = "ON";
// toggle state of LED
ledState = !ledState;
// control LED arccoding to the toggled state
digitalWrite(LED_PIN, ledState);
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>LED - State " + ledStateNumeric + "</p>");
// If the output26State is off, it displays the ON button
if (ledStateNumeric = "OFF") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
} else {
ledStateNumeric = "OFF";
}
}