Dear all,
I have an issue on setting digitalwrite on several pin using Button on Virtual PIN, when I start and run the code, I can get the LED status following the Button Status, but somehow the digitalwrite is not executed on my hardware (by the way I’m using NodeMCU ESP8266).
Strangely if I change the Button PIN from virtual to actual Dx PIN (on Blynk Apps), and back again the setting to virtual PIN everything is back to normal (for that particular PIN, so I have to repeat this procedure for every virtual PIN that I have).
unfortunately the issue will be back when I’m reseting my hardware …
here are my code:
// ------------------- some main code are not shown --------
#define D1 5
#define D3 0
#define D5 14
WidgetLED led1(V41);
WidgetLED led3(V43);
WidgetLED led5(V45);
int NetworkDisconectedTimes = 0;
BLYNK_CONNECTED() {
Blynk.syncAll();
NetworkDisconectedTimes++;
Blynk.virtualWrite(V21, NetworkDisconectedTimes); // -- Disconected time counter on V21
}
BLYNK_WRITE(V31)
{
int i=param.asInt();
if (i==1)
{
digitalWrite(D1, HIGH);
led1.on();
}
else
{
digitalWrite(D1, LOW);
led1.off();
}
}
`BLYNK_WRITE(V33)
{
int i=param.asInt();
if (i==1)
{
digitalWrite(D3, HIGH);
led3.on();
}
else
{
digitalWrite(D3, LOW);
led3.off();
}
}
BLYNK_WRITE(V35)
{
int i=param.asInt();
if (i==1)
{
digitalWrite(D5, HIGH);
led5.on();
}
else
{
digitalWrite(D5, LOW);
led5.off();
}
//---------------------------------------------- `
@noersaleh I have some idea about your virtual pins and button, here some code may be can help to solve your issue.
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <TimeLib.h>
// include your ESP library here
// and other blynk widget
const int D1 = 7; // pin for your relay outputs or any trigger output. etc.
const int btnPin = 8; // pin for your physical push btn sw or toggle sw. just for example
//declaire your other pins here
SimpleTimer timer; // add simple timer for the time interval scanning period of physical btn input sw.
WidgetLED led1(V41);
void checkPhysicalButton();
int D1State = LOW;
int D1State = HIGH; //HIGH
//*******Sets Relays to Off Position*****************
#define TURN_ON 0 // TURN_ON and TURN_OFF are defined to account for Active High relays
#define TURN_OFF 1 // Used to switch relay states
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
// You can also specify server.
// For more options, see BoardsAndShields/Arduino_Ethernet_Manual example
{
pinMode(D1, OUTPUT); // initialize ur pin as output
pinMode(btnPin, INPUT_PULLUP); // init ur pin as input with internal pull-up resistor "input_pullup"
digitalWrite(D1, D1State);
digitalWrite(D1, TURN_OFF); // remain off till command is receive
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
}
while (Blynk.connect() == false) {
// Wait until connected
}
void checkPin()
{
// Invert state, since button is "Active LOW"
if (digitalRead(7)) {
led1.on();
} else {
led1.off();
}
}
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(V31);
// Alternatively, you could override server state using:
//Blynk.virtualWrite(V31, D1State);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V31) {
D1State = param.asInt();
digitalWrite(D1, D1State);
{
//BLYNK_LOG("Got a value: %s", param.asStr());
// You can also use:
int i = param.asInt();
int state;
// Switch mode inverse//
if (i == 0) {
digitalWrite(D1, HIGH);
Serial.println("Relay 1 = OFF"); // turn OFF your relay outputs
}
if (i == 1) {
digitalWrite(D1, LOW); // turn ON your relay outputs
Serial.println("Relay 1 = ON");
delay (300);
}
}
}
void checkPhysicalButton()
{
if (digitalRead(btnPin) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState != LOW) {
// Toggle LED state
D1State = !D1State;
digitalWrite(D1, D1State);
// Update Button Widget
Blynk.virtualWrite(V31, D1State);
}
btnState = LOW;
} else {
btnState = HIGH;
}
}
void loop()
{
Blynk.run(); // Run Blynk
timer.run(); // Run SimpleTimer
}
Dear Castle, really appreciated with your input, let me clarify some info on my issue above:
The Button that I mentioned earlier actualy is a Virtual Button (from the Blynk Andriod apps) that tight to virtual PIN, thats why I’m using BLYNK_WRITE command, and the digital.write command is to set up the harware relay/LED.
The reason I’m doing this is to make sure that the realy is set ON/OFF when I change the virtual button, and I want to see how fast the respons from I press the button to the time that the relay really react (due to my unstable network).
The condition is that the virtual LED is alway be executed any time when the hardware startup, unfortunately the relay isn’t, until I change the app button to harware PIn first, executed a switch on/off, then setup back the button to virtual Pin…
Hi, @noersaleh the sketch which i send is two option are there, either you can give command on/off from external input btnPin to toggle on/off your output D1 or through ios or andriod mobile apps switch widget. for the response it is depends on your network at least you have less than 140ms. for the apps switch widget you can reduce the delay (300); which i put in the sketch above, but 300 is enough i think. you can try playaround and its up to you how fast do you want.
that code is 100% working and tested. i read many articles and gather some important lines for each and every forums, and compile it by support of our technical teams @Dmitriy his blynk team. so you can do it too. i just join recently and i can help you guys if i have free time and sory for late reply. i ll be keep in line till you solve your small issue.
// AS YOU SAID "The condition is that the virtual LED is alway be executed any time when the hardware startup"
digitalWrite(D1, TURN_OFF); // remain off till command is receive (this is to avoid your digital I/O activation during startup or bootup.
digitalWrite(D1, HIGH); // or either you can use this. if you use this, remove the declaration #define TURN_OFF 1 & #define TURN_ON 0 to avois error while compiling.