can someone help me. If I turn off/on lights thru switch i want to sync the switch and get update to the apps if it is turn on/off. here is the code . I use Mitappinventor esp32 and a 5V relay.
int relay1 = 23;
int relay2 = 4;
int relay3 = 2;
int relay4 = 19;
int relay5 = 18;
int relay6 = 5;
int relay7 = 25;
int relay8 = 26;
int pbuttonPin = 13;
int pbuttonPin2 = 12;
int val = 0;
int lightON = 0;
int pushed = 0;
int val2 = 0;
int lightON2 = 0;
int pushed2 = 0;
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
void setup()
{
init();
lcd.begin();
// turn on LCD backlight
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Home Automation");
lcd.setCursor(0,1);
lcd.print("The System is On");
delay(2000);
lcd.clear();
Serial.begin(115200);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
pinMode(relay7, OUTPUT);
pinMode(relay8, OUTPUT);
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(relay5,HIGH);
digitalWrite(relay6,HIGH);
digitalWrite(relay7,HIGH);
digitalWrite(relay8,HIGH);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(pbuttonPin2, INPUT_PULLUP);
connectWiFi();
server.begin();
}
void loop() {
val = digitalRead(pbuttonPin);
val2 = digitalRead(pbuttonPin2);
//Relay 1
if(val == HIGH && lightON == LOW)
{
pushed = 1-pushed;
lightON = val;
if(pushed == 1)
{
digitalWrite(relay1, LOW);
}
else
{
digitalWrite(relay1, HIGH );
Serial.println("ON");
}
}
lightON = val;
//Relay 2
if(val2 == HIGH && lightON2 == LOW){
pushed2 = 1-pushed2;
lightON2 = val2;
if(pushed2 == HIGH){
command = "r2on" ;
digitalWrite(relay2, LOW);
}else{
command = "r2off";
digitalWrite(relay2, HIGH);
}
}
lightON2 = val2;
client = server.available();
if (!client) return;
command = checkClient ();
if (command == "r1on") { digitalWrite(relay1,0);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 1 ON");}
else if (command == "r1off" ) { digitalWrite(relay1,1);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 1 OFF");}
else if (command == "r2on" ) {digitalWrite(relay2,0);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 2 ON"); }
else if (command == "r2off") { digitalWrite(relay2,1);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 2 OFF");}
else if (command == "r3on") { digitalWrite(relay3,0);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 3 ON");}
else if (command == "r3off") { digitalWrite(relay3,1);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 3 OFF");}
else if (command == "r4on") { digitalWrite(relay4,0);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 4 ON");}
else if (command == "r4off"){ digitalWrite(relay4,1);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 4 OFF");}
else if (command == "r5on") { digitalWrite(relay5,0);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 5 ON");}
else if (command == "r5off"){ digitalWrite(relay5,1);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 5 OFF");}
else if (command == "r6on") { digitalWrite(relay6,0);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 6 ON");}
else if (command == "r6off"){ digitalWrite(relay6,1);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" LIGHT 6 OFF");}
else if (command == "r7on") { digitalWrite(relay7,0);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" OUTLET 7 ON");}
else if (command == "r7off"){ digitalWrite(relay7,1);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" OUTLET 7 OFF");}
else if (command == "r8on") { digitalWrite(relay8,0);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" OUTLET 8 ON");}
else if (command == "r8off"){ digitalWrite(relay8,1);
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" OUTLET 8 OFF");}
else if (command == "allon" ) {
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,LOW);
digitalWrite(relay5,LOW);
digitalWrite(relay6,LOW);
digitalWrite(relay7,LOW);
digitalWrite(relay8,LOW);
lcd.setCursor(0,0);
lcd.clear();
lcd.print("ALL DEVICES ON");
}
else if(command == "alloff" ) {
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(relay5,HIGH);
digitalWrite(relay6,HIGH);
digitalWrite(relay7,HIGH);
digitalWrite(relay8,HIGH);
lcd.setCursor(0,0);
lcd.clear();
lcd.print("ALL DEVICES OFF");
}
}
/* connecting WiFi */
void connectWiFi()
{
Serial.println("Connecting to WIFI");
WiFi.begin(ssid, password);
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
Serial.print("..");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("NodeMCU Local IP is : ");
Serial.print((WiFi.localIP()));
}
/* check command received from Android Device */
String checkClient (void)
{
while(!client.available()) delay(1);
String request = client.readStringUntil('\r');
request.remove(0, 5);
request.remove(request.length()-9,9);
return request;
}