Simulating a PUSH button for GearBlynk

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();
}
1 Like

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 :wink: 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”

1 Like

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.

#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN D2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

#ifndef STASSID
#define STASSID "yours"
#define STAPSK  "yours"
#define LED D4
//long S = 200;
//long O = 600;
//long off = 500;
//unsigned long previousMillis = 0;
int flagS=0;
int flagO=0;
int loopFlagS=0;
int loopFlagO=0;
#endif

const char* ssid = STASSID;
const char* password = STAPSK;
WidgetLCD lcd (V0);

char auth[] = "yours";
BlynkTimer timer;

void sendSensor(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    return;
  }
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup() {
  pinMode(D1,OUTPUT);
  digitalWrite(D1,HIGH);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    delay(5000);
    ESP.restart();
  }
  Blynk.begin(auth,STASSID,STAPSK);
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else {
      type = "filesystem";
    }
  });
  ArduinoOTA.onEnd([]() {
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  });
  ArduinoOTA.onError([](ota_error_t error) {
    if (error == OTA_AUTH_ERROR) {
    } else if (error == OTA_BEGIN_ERROR) {
    } else if (error == OTA_CONNECT_ERROR) {
    } else if (error == OTA_RECEIVE_ERROR) {
    } else if (error == OTA_END_ERROR) {
    }
  });
  ArduinoOTA.begin();
  pinMode(LED,OUTPUT);
  digitalWrite(LED, HIGH); 
  lcd.clear();
  }

void loop() {
  ArduinoOTA.handle();
  Blynk.run();
  timer.run();
}

BLYNK_WRITE(V7)
    {
    int buttonStatus = param.asInt();
    if ((buttonStatus == 1))
    {
    lcd.clear();
    S1();
    lcd.print(0,0, loopFlagO);
    lcd.print(1,0, loopFlagS);
    }
    }

void S1()
{
  if(loopFlagS <= 2){
    digitalWrite(D1,LOW);
    timer.setTimeout(S,Off2);
    lcd.print(0,0, loopFlagO);
    lcd.print(1,0, loopFlagS);
    }
  else if((loopFlagS >= 2)&&(loopFlagO <= 2))
    {
    digitalWrite(D1,LOW);
    timer.setTimeout(O,Off3);
    lcd.print(0,0, loopFlagO);
    lcd.print(1,0, loopFlagS);
    }
  else if((loopFlagS <= 5)&&(loopFlagO == 3))
  {
    digitalWrite(D1,LOW);
    timer.setTimeout(S,Off2);
    lcd.print(0,0, loopFlagO);
    lcd.print(1,0, loopFlagS);
  }
  else{
    loopFlagS=0;
    loopFlagO=0;
    S3();
    }
}

void Off2()
{
  digitalWrite(D1,HIGH);
  loopFlagS++;
  timer.setTimeout(off,S1);  
}

void Off3()
{
  digitalWrite(D1,HIGH);
  timer.setTimeout(off,S1);
  loopFlagO++;
}

void S3()
{
  lcd.print(0,0, "bucka");
}