How to use 1 way switch instead of push switches in Home Automation project

Yep i am aware of it… If we go as per the safe pins we get only 2 pins (and few which spikes during boot.) :ok_man:
But the pins what i am using as of now works just fine while using push buttons. May be keeping few pins high during boot will destroy the module.

Look out the code:

#define BLYNK_PRINT Serial            
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>  // For OTA with ESP8266
#include <WiFiUdp.h>  // For OTA
#include <ArduinoOTA.h>  // For OTA

BlynkTimer timer;

void checkPhysicalButton();

int relay1State = LOW;
int Button1State = HIGH;

int relay2State = LOW;
int Button2State = HIGH;

int relay3State = LOW;
int Button3State = HIGH;

int relay4State = LOW;
int Button4State = HIGH;


#define AUTH "AUTH"  // You should get Auth Token in the Blynk App.  
#define WIFI_SSID "SSID"                   //Enter Wifi Name
#define WIFI_PASS "PASS"                   //Enter wifi Password

#define SERVER "blynk-cloud.com "             // Comment-out if use Blynk hosted cloud service
#define PORT 8442

#define RELAY_PIN_1      5   //D1
#define RELAY_PIN_2      4   //D2
#define RELAY_PIN_3      12   //D6
#define RELAY_PIN_4      13   //D7

#define BUTTON_1    0    //D3
#define BUTTON_2    14   //D5
#define BUTTON_3    2   //D4
#define BUTTON_4    15   //D8

#define VPIN_BUTTON_1    V12 
#define VPIN_BUTTON_2    V13
#define VPIN_BUTTON_3    V14
#define VPIN_BUTTON_4    V15  

int oldState1 = HIGH;
int oldState2 = HIGH;
int oldState3 = HIGH;
int oldState4 = HIGH;

#define OTA_HOSTNAME "Home_Automation"

BLYNK_CONNECTED() {

  // Request the latest state from the server

  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
   Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
}

// When App button is pushed - switch the state

BLYNK_WRITE(VPIN_BUTTON_1) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  relay2State = param.asInt();
  digitalWrite(RELAY_PIN_2, relay2State);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
  relay3State = param.asInt();
  digitalWrite(RELAY_PIN_3, relay3State);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
  relay4State = param.asInt();
  digitalWrite(RELAY_PIN_4, relay4State);
}

 void checkPhysicalButton()
{
int State1 = digitalRead(BUTTON_1); //"State" containes the logical level of the switch
if (State1 != oldState1) { //Change of the input state? Yes if I change the switch position
    oldState1 = State1; //in order to be ready for a new change of state
    relay1State = !relay1State;
    digitalWrite(RELAY_PIN_1, relay1State);
    Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
}
   
    

int State2 = digitalRead(BUTTON_2); //"State" containes the logical level of the switch
if (State2 != oldState2) { //Change of the input state? Yes if I change the switch position
    oldState2 = State2; //in order to be ready for a new change of state
    relay2State = !relay2State;
    digitalWrite(RELAY_PIN_2, relay2State);
    Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
}

int State3 = digitalRead(BUTTON_3); //"State" containes the logical level of the switch
if (State3 != oldState3) { //Change of the input state? Yes if I change the switch position
    oldState3 = State3; //in order to be ready for a new change of state
    relay3State = !relay3State;
    digitalWrite(RELAY_PIN_3, relay3State);
    Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
}

 int State4 = digitalRead(BUTTON_4); //"State" containes the logical level of the switch
if (State4 != oldState4) { //Change of the input state? Yes if I change the switch position
    oldState4 = State4; //in order to be ready for a new change of state
    relay4State = !relay4State;
    digitalWrite(RELAY_PIN_4, relay4State);
    Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
}
}

void setup()
{

  Serial.begin(115200);
  Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS,"blynk-cloud.com", 8442);
  ArduinoOTA.setHostname(OTA_HOSTNAME);  // For OTA - Use your own device identifying name
  ArduinoOTA.begin();  // For OTA

  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);


  pinMode(RELAY_PIN_2, OUTPUT);
  pinMode(BUTTON_2, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_2, relay2State);


  pinMode(RELAY_PIN_3, OUTPUT);
  pinMode(BUTTON_3, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_3, relay3State);


  pinMode(RELAY_PIN_4, OUTPUT);
  pinMode(BUTTON_4, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_4, relay4State);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);
}

void loop()
{
  Blynk.run();
  ArduinoOTA.handle();  // For OTA
  timer.run();
}

I guess this will be problem if the switch is connected to GND during boot. I will crash the ESP. Have you checked all the switches for all the position combination (UP / DOWN)… Because this will definitely crash during startup if pins are pulled low. And even D8 is ment to be pulled high during boot. I can see a button connected to D8, if the switch is on and if it is connected to GND then it will crash… Test this out…