Virtual and Physical Switch Code

Hello all! I am looking for some help writing some code that is controlling some12v lights.

I rewired an existing 12v switch that was powering lights to use logic level (3.3v) from my board (a WemosD1Mini) to control the on-off in parallel with the Wemos using the Blynk-app over wifi. The problem is, I can control the lights fully remotely when the switch is off (on, off, PWM dimming, etc.), but, when the switch is turned on it sorta acts as an override and turns the lights on ignoring the switch state in the Blynk app.

For instance, when the switch is off and the lights are dimmed to 50% via the app; when the physical switch is turned on (closed) it turns the lights on at 100%; when the physical switch is turned off (open) the lights return to the 50% brightness according to the Blynk app.

I would prefer that the physical switch overrides the app and vice versa. So I could, for instance, turn the lights off even if the switch is on (closed)--kinda like how a 3-way switch in a house works--so two switches that both do the same thing regardless/independent of each other.

I believe I need to use some configuration of a change interrupt, or pin_ISR in my code, but I have no idea how. I am still very green to coding and have amalgamated the code below together as I have been learning. Can someone please help me code the desired functionalities as described above? Thanks :slight_smile:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ArduinoOTA.h>

//WIFI-CRED
char auth[] = "XXXX";
char ssid[] = "XXXX";
char pass[] = "XXXX";
//END WIFI-CRED

//OTA-PASS
char hostOTA[] = "Lts";
char passOTA[] = "XXXX";
//END OTA-PASS

//LIGHTSW-SETUP
int switchPin = D8;     //from single pole, logic-level-voltage switch
int ledPin =  D7;      // the number of the load pin used to drive relay 
int switchState = 0; 
//END LIGHTSW-SETUP

//RESET Function
BLYNK_WRITE(V10)
{
  if (param.asInt()) // If the button widget sent a "1" (on)
  {
      ESP.reset(); // Reset
  }
}
//END RESET Function

//-----------------------------
void setup()
{
//SWITCH
  pinMode(ledPin, OUTPUT);      
  pinMode(switchPin, INPUT);    
//END SWITCH

//BLYNK-SERVER
 Blynk.begin(auth, ssid, pass, IPAddress(192,168,XX,XX),8080);
//END BLYNK-SERVER

//OTA
  ArduinoOTA.setHostname(hostOTA);
  ArduinoOTA.setPassword(passOTA);
  ArduinoOTA.onStart([]() {
    Serial.println("OTA: Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nOTA: End");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("OTA: Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("OTA: Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("OTA: Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("OTA: Ready");
//END OTA 
}
 
//-----------------------------
void loop()
{

//BLYNK-START
Blynk.run();
//END BLYNK-START

//OTA-START
ArduinoOTA.handle();
//END OTA-START

//SWITCH-START
  switchState = digitalRead(switchPin);
  if (switchState == HIGH) {    
       digitalWrite(ledPin, HIGH);  
      
    }  else{
      digitalWrite(ledPin, LOW);
    }
//END SWITCH-START

}

The code you posted does not process any Blynk virtual pins so I'm not sure how you are controlling the lights via PWM. It only contains code to read the switch.

Your switch handling in loop() is equivalent to this:

digitalWrite(LedPin, digitalRead(switchPin));

If the LED does anything but respond to the switch then the problem resides in the code not shown.
If you want different behaviour then specify what should happen, not what should not happen.

Thanks, I plan to move the switch state code out of the loop... :slight_smile:

Sorry if I was not clear, I want the physical switch to turn the light on and off regardless of the Blynk switch state and vice versa--the Blynk switch to turn the lights on and off regardless of the physical switch state.

Currently, the Blynk switch works perfectly if the physical switch is in the off position.

I think I need to add some code to monitor the state of the physical and Blynk switches using the change interrupt, or pin_ISR functions... but I don't know how to, that's what I need help with.

Wiring diagram?

You may need state change detection?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.