integrating manual switch and web button to control same relay

I want to on/off relay using both manual switch and web button using esp8266nodemcu. I am new to this nodemcu. and the code I used is given below having some problems. Please help me on this.

#include <ESP8266WiFi.h>
 
const char* ssid = "******";
const char* password = "****************";
 
int in1 = 13; // D7
int in2 = 12; // D6
int in3 = 14; // D5
int in4 = 2; // D4
int buttonPin1 = 0; // D3-SWITCH 1
int buttonPin2 = 4; // D2-SWITCH 2
int buttonPin3 = 5; // D1-SWITCH 3
int buttonPin4 = 16; // D0-SWITCH 4
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
  pinMode(in1, OUTPUT);
  pinMode(buttonPin1, INPUT);
  digitalWrite(in1, HIGH);
  pinMode(in2, OUTPUT);
  pinMode(buttonPin2, INPUT);
  digitalWrite(in2, HIGH);
  
  pinMode(in3, OUTPUT);
  pinMode(buttonPin3, INPUT);
  digitalWrite(in3, HIGH);
  pinMode(in4, OUTPUT);
  pinMode(buttonPin4, INPUT);
  digitalWrite(in4, HIGH);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  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");
 
  // 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() {
  // 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();


  val1 = digitalRead(buttonPin1);
  val2 = digitalRead(buttonPin2);
  val3 = digitalRead(buttonPin3);
  val4 = digitalRead(buttonPin4);
 
  // Match the request
 
  int value1 = LOW;
  int value2 = LOW;
  int value3 = LOW;
  int value4 = LOW;
  if (request.indexOf("/button1=on") != -1 || val1 == HIGH )  {
    digitalWrite(in1, LOW);
    value1 = HIGH;
  }
  if (request.indexOf("/button1=off") != -1 || val1 == LOW)  {
    digitalWrite(in1, HIGH);
    value1 = LOW;
  }
 if (request.indexOf("/button2=on") != -1 || val2 == HIGH) {
    digitalWrite(in2, LOW);
    value2 = HIGH;
  }
  if (request.indexOf("/button2=off") != -1 || val2 == LOW) {
    digitalWrite(in2, HIGH);
    value2 = LOW;
  }

  if (request.indexOf("/button3=on") != -1 || val3 == HIGH) {
    digitalWrite(in3, LOW);
    value3 = HIGH;
  }
  if (request.indexOf("/button3=off") != -1 || val3 == LOW) {
    digitalWrite(in3, HIGH);
    value3 = LOW;
  }

  if (request.indexOf("/button4=on") != -1 || val4 == HIGH) {
    digitalWrite(in4, LOW);
    value4 = HIGH;
  }
  if (request.indexOf("/button4=off") != -1 || val4 == LOW) {
    digitalWrite(in4, HIGH);
    value4 = LOW;
  }
  if (request.indexOf("/buttonall=on") != -1) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    value1 = HIGH;
    value2 = HIGH;
    value3 = HIGH;
    value4 = HIGH;
  }
  if (request.indexOf("/buttonall=off") != -1) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, HIGH);
    value1 = LOW;
    value2 = LOW;
    value3 = LOW;
    value4 = 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("Relay1 is now: ");
   if(value1 == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
  client.print("<a href=\"/button1=on\"><button>ON</button></a>&nbsp;<a href=\"/button1=off\"><button>OFF</button></a></p>");
  client.println("\n");
  client.print("Relay 2 is now: ");
  if (value2 == HIGH) {
    client.println("On");
  } else {
    client.println("Off");
  }
   client.print("<a href=\"/button2=on\"><button>ON</button></a>&nbsp;<a href=\"/button2=off\"><button>OFF</button></a></p>");
   client.println("\n");
   client.print("Relay 3 is now: ");
  if (value3 == HIGH) {
    client.println("On");
  } else {
    client.println("Off");
  }
   client.print("<a href=\"/button3=on\"><button>ON</button></a>&nbsp;<a href=\"/button3=off\"><button>OFF</button></a></p>");
   client.println("\n");
   client.print("Relay 4 is now: ");
  if (value4 == HIGH) {
    client.println("On");
  } else {
    client.println("Off");
  }
  
  client.print("<a href=\"/button4=on\"><button>ON</button></a>&nbsp;<a href=\"/button4=off\"><button>OFF</button></a></p>");
  client.println("\n");
  client.println("<a href=\"/buttonall=on\"><button>ALL ON</button></a>&nbsp;<a href=\"/buttonall=off\"><button>ALL OFF</button></a></p>");
  client.println("\n");
  client.println("</html>");
  client.println("

");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
 
}

I want you to stop shouting at us.

See How to use this forum.

Raghavendra431 wrote (in part):

THE CODE I USED IS ... HAVING SOME PROBLEM

The code is expected to do something. What is it?
The code does something. What is it?

actually my project is to switch the relay from the web and also from manual switchboard.

conditions: -

  1. Relay should "ON" when switched "ON" from web even when the manual switch is in "OFF" condition.
  2. Relay should "OFF" when switched "OFF" from web even when the manual switch is in "ON" condition.
  3. Relay should "ON" when manual switch is switched on when web switch is initially "OFF" condition.
  4. Relay should "OFF" when manual switch is switched on when web switch is initially "ON" condition.
  5. web switch "ON" and manual Switch "ON" relay "ON"
  6. web switch "OFF" and manual Switch "OFF" relay "OFF"

To activate a relay, you need to set HIGH to a digital pin that connected to Relay input pin. Say this pin is 4 of Arduino.

In case of a Switch, you check whether the switch is pressed then digitalWrite(4,HIGH), if not LOW

In case of ESP, you wait for a serial data, say to switch on, you need a command say 'ON'. In loop you wait for a Serial data and if it's 'ON' then digitalWrite(4,HIGH), if 'OFF' then LOW

If you want some one to help, then write a small tracer code, may be dealing with one digital pin, may be pin 13. This way your code looks simple. To simulate ESP communication we can directly send the command via Serial monitor.

@Raghavendra431 - in reply #3 what are you quoting? Please just post text normally. Don't quote yourself in this way.