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 ![]()
#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
}