Hello friends,
I am doing my graduation project like led on/off using web button and push button. In web 'on'button is pressed then led on and the status of the led is displayed in web browser. when we press push button the led is off but led status is not displayed in web browser. please help me how to solve this problem.
#include <ESP8266WiFi.h>
#include <Bounce2.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
const byte SwitchPins[] = {4};
Bounce switches[sizeof(SwitchPins)];
int ledpin = 13;
IPAddress ip(********); //Node static IP
IPAddress gateway(*****);
IPAddress subnet(********);
WiFiServer server(***);
void setup() {
Serial.begin(115200);
delay(10);
for(byte i = 0; i < sizeof(SwitchPins); i++){
switches[i].attach(SwitchPins[i], INPUT_PULLUP); //use INPUT if you have external pull up/down resistors
}
pinMode(ledpin, OUTPUT);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop()
{
webloop();
switchloop();
}
void webloop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value1 = LOW;
if (request.indexOf("/button1=on") != -1) {
digitalWrite(ledpin, LOW);
value1 = HIGH;
}
if (request.indexOf("/button1=off") != -1) {
digitalWrite(ledpin, HIGH);
value1 = LOW;
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("<TITLE>Home Automation</TITLE>\r\n");
client.print("</HEAD>\r\n");
client.print("<BODY>\r\n");
client.print("<H1>My Smart Home System</H1>\r\n");
client.print("led is now: ");
if(value1 == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.print("<a href=\"/button1=on\"><button>ON</button></a> <a href=\"/button1=off\"><button>OFF</button></a></p>");
client.println("\n");
client.println("\n");
client.println("</html>");
client.println("
");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
void switchloop()
{
for(byte i = 0; i < sizeof(SwitchPins); i++){
switches[i].update();
}
//I here assume pull up resistors (like with INPUT_PULLUP)
if(switches[0].fell()){
digitalWrite(ledpin, HIGH);
}
if(switches[0].rose()){
digitalWrite(ledpin, LOW);
}
}