You mean SWITCH type button widget? Just change the LED widget to a switch type… it will reflect the state of current… but not the state of the RELAY… so if the relay is ACTIVE… the current might not flow because it depends on the state of the phyiscal switch.
This is why your idea becomes very complicated. Better to read state of current, and light up an LED, then use a PUSH button to change the state of RELAY.
See diagram. There is a physical lightswitch in there.
EDIT: actually… just thought of another way to hook up the button to a SWITCH type widget. brb writing the code
UPDATE: here you go… now the BUTTON (SWITCH type) will light up when Current is detected. If you switch the BUTTON widget off, then current will stop, and the relay will invert its current option.
//#define BLYNK_DEBUG
//#define BLYNK_PRINT Serial
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;
char auth[] = "xxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxx";
bool USE_LOCAL_SERVER = 1;
int RELAY_PIN = 14; // GPIO-14 or D5 on ESP8266
int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module
int ACSoffset = 2500;
double Voltage, Amps;
void readACS712() {
Voltage = (analogRead(A0) / 1024.0) * 5000;
if (Voltage <= 2500) {
// detected nothing
Blynk.virtualWrite(V0, 0); // update BUTTON (SWITCH) widget state when detecting current
Voltage = 2500;
Amps = 0;
} else {
// detected current
Blynk.virtualWrite(V0, 1); // update BUTTON (SWITCH) widget state when detecting current
Amps = ((Voltage - ACSoffset) / mVperAmp);
Blynk.virtualWrite(V2, Amps); // report Amps, because why not monitor power usage too?
}
}
// button control for relay...
// needs to be SWITCH type button, detect CHANGE OF STATE only, and invert relay state
BLYNK_WRITE(V0) {
digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN));
}
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // most relays are active LOW, so on boot, set HIGH (OFF)
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (USE_LOCAL_SERVER) {
Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 1, 2));
} else {
Blynk.begin(auth, ssid, pass);
}
while (Blynk.connect() == false) {}
ArduinoOTA.setHostname("AC-Detecting-Switch");
ArduinoOTA.begin();
timer.setInterval(1000L, readACS712);
}
void loop() {
Blynk.run();
ArduinoOTA.handle();
timer.run();
}