Hi,
I am not a coder and hence i need your help for adding two widgets to my code.
I am using a NodeMCU based system to run a relay. Basic setup. Hope i get some help.
I have a little logic going on to listen to both, the physical switch (Pin that grounds when on) and App switch at the same time and then provide feedback for both on the app.
I want the Timer Widget and GPS trigger widget to work with my logic, so that it can manage the state of the physical button as well as the App button.
To give an example of the logic:
If i want the timer to turn on the D0 pin at a 12 am, then,
if physical Switch is high and App button is off, turn pin D0 to high (at the 12 am),
if physical Switch is high and App button is on, do nothing (since the system would be on),
if physical Switch is low and App button is off, do nothing,
if physical Switch is Low and App button is on, turn pin D0 to high (because the system would be off).
This logic needs to be reversed for Off timer too.
Hope i make sense here. Maybe the code helps you understand the logic better. Thanks for your help!
#define USE_NODE_MCU_BOARD // Comment out the boards you are not using
#define APP_DEBUG // Comment this out to disable debug prints
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include "BlynkProvisioning.h"
void setup() {
delay(500);
Serial.begin(115200);
BlynkProvisioning.begin();
example_init();
}
void loop() {
BlynkProvisioning.run();
example_run();
}
BlynkTimer timer;
WidgetLED led3(V3);
WidgetLCD lcd(V2);
WidgetLED Led8(V7);
const int btnPin = 2;
int AppButtonState = LOW;
boolean btnState = false;
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V1);
}
// V3 LED Widget represents the physical button state
void buttonLedWidget()
{
Blynk.virtualWrite(V1,AppButtonState); // update virtual pin1 with AppButtonState
boolean isPressed = (digitalRead(btnPin) == LOW);
if( (isPressed==0)&& (AppButtonState==0) )
{
//lcd.clear();
lcd.print(1, 0, "System ON");
Led8.on();
}
else if ( (isPressed==0)&& (AppButtonState==1) )
{
//lcd.clear();
lcd.print(1, 0, "System OFF");
Led8.off();
}
else if ( (isPressed==1)&& (AppButtonState==0) )
{
//lcd.clear();
lcd.print(1, 0, "System OFF");
Led8.off();
}
else if ( (isPressed==1)&& (AppButtonState==1) )
{
//lcd.clear();
lcd.print(1, 0, "System ON");
Led8.on();
}
// If state has changed...
if (isPressed != btnState)
{
// Serial.print("Btn:"); // comment this before delivering
// Serial.println(isPressed); // comment this before delivering
if (isPressed)
{
led3.on();
} else
{
led3.off();
}
btnState = isPressed;
}
}
// When App button is pushed - switch the state
BLYNK_WRITE(V1) {
AppButtonState = param.asInt();
Serial.print("App:"); // comment this before delivering
Serial.println(AppButtonState); // comment this before delivering
digitalWrite(D0, AppButtonState);
}
void example_init() {
Serial.begin(115200);
// Setup notification button on pin 2 as D4 is set as input button pin, active low
pinMode(btnPin, INPUT_PULLUP);
pinMode(16,OUTPUT);//pin D0 is output
// Setup a function to be called every x seconds
timer.setInterval(500L, buttonLedWidget);
}
void example_run() {
timer.run();
}