Hi guys, maybe it’s a small project for this community, but for sure there are some other noobs (like me) looking for this solution. I’m not sure if it’s perfect, but for me works well.
This simulation was needed especially for GearBlynk app. I have to control a gate using a push button only. The button should be active for ~1 second. GearBlynk doesn’t have this push/ switch button option, so i’ve done this. Also, even for the phone app, it’s more easy to press the button once and let the code do the job for you (keep the pin active for ~1.5 seconds then switch it off).
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxx";
char ssid[] = "xxxx";
char pass[] = "xxxxx";
BLYNK_CONNECTED() {
Blynk.syncVirtual(V1);
}
int Relay_Pin = 0; // Connect the relay to GPIO0
BLYNK_WRITE(V1) // This code is triggered when the App button widget on V1 changes state
{
int pinValue = param.asInt(); // Assign incoming value from pin V1 to a variable
if (pinValue==1) // Do this if it was an On command from the Button widget in the App
{
Blynk.virtualWrite(V1, HIGH); // Change the app button state and keep it ON while you remove the finger - for visual feedback
digitalWrite(Relay_Pin, HIGH); // Turn the relay On
delay (1350); // Wait for 1.35 sec
digitalWrite(Relay_Pin, LOW); // Turn the relay Off
Blynk.virtualWrite(V1, LOW); //Change the app button state to OFF
}
else // Else do this if it was an Off command from the Button widget in the App
{
digitalWrite(Relay_Pin, LOW); // Turn/ Keep the relay Off
}
}
void setup()
{
pinMode(Relay_Pin, OUTPUT); // Define the relay pin as an output
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
While it may work in shorter durations, delay() is just plain bad programming form with regards to Blynk and any other timing sensitive processes. As has been referenced many times in this forum and documentation, for all no0Bs to read BlynkTimer (based on SimpleTimer) is the prefered method.
For example, non-blocking timer controlled button action is the prefered way to make a “Timed Button”
Speaking of gate bells for the fun of it I set mine to do the SOS routine when I push the button. WITH NO DELAYS. Please see the last 5-6 functions. There is probably better ways to do this but this works. I just started with OTA the other day so I would like to find lighter code for that.