BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

ESP8266 Timed Button (Mimic a button press for a duration)

esp8266

#1

Hi, I am new to Blynk. I would like to use the Blynk App on the iPhone to send a pulse (Mimic a button press) to one of the ESP8266 GPIO to control a device. Do you know if Blynk supports pulse instead of just ON or OFF? Can you give some instructions on how it can be done? Thanks a lot.


#2

Blynk Switch widgets can be set to Push (momentary) or Switch (on/off) mode.
In Push mode the digital GPIO Pin will be active as long as the button is held pressed. In Switch mode, one press activates it, then another press deactivates it.

If you need a pulse of a specific duration then you could attach the switch widget to a virtual pin and then use code to control your GPIO pin.
Personally, I always use virtual rather than digital pins in Blynk as they give much more control, but they do require some coding knowledge.

Pete.


#3

Here is the working code with GPIO pulse for 3secs.

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on NodeMCU.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right NodeMCU module
  in the Tools -> Board menu!

  For advanced settings please follow ESP examples :
   - ESP8266_Standalone_Manual_IP.ino
   - ESP8266_Standalone_SmartConfig.ino
   - ESP8266_Standalone_SSL.ino

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxx
char pass[] = "xxxxxxxx";

// Select your pin with physical button
const int away = 5;
const int indoor = 0;
const int disarm = 14;
const int alarmstatus = 12;    // gpio 12 --- D6
int previoustate = 0;

BlynkTimer timer;

WidgetLCD lcd(V10);

void PrintStatus_LCD(){
  pinMode(alarmstatus,INPUT);
  if (digitalRead(alarmstatus) == 1)
  {
    if (previoustate == 0)
    {
      lcd.clear();
      lcd.print(0,1, " A L A R M   O N");
      previoustate = 1;
    }
  }  
  
  else
  {
    if (previoustate == 1)
    {
      lcd.clear();
      lcd.print(0,1, "A L A R M  O F F");
      previoustate = 0;
    }
  }

   
//  Blynk.virtualWrite(V10,digitalRead(alarmstatus));
}

BLYNK_WRITE(V0){
  if(param.asInt()){
    digitalWrite(away, HIGH);
    delay(3000);
    digitalWrite(away, LOW);
  }
  else{
    digitalWrite(away, LOW);
  }
}

BLYNK_WRITE(V1){
  if(param.asInt()){
    digitalWrite(indoor, HIGH);
    delay(3000);
    digitalWrite(indoor, LOW);
  }
  else{
    digitalWrite(indoor, LOW);
  }
}

BLYNK_WRITE(V2){
  if(param.asInt()){
    digitalWrite(disarm, HIGH);
    delay(3000);
    digitalWrite(disarm, LOW);
  }
  else{
    digitalWrite(disarm, LOW);
  }
}
void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(away, OUTPUT);
  pinMode(indoor, OUTPUT );
  pinMode(disarm, OUTPUT);
  digitalWrite(away, LOW);
  digitalWrite(indoor, LOW);
  digitalWrite(disarm, LOW);

  // Setup a function to be called every second
  timer.setInterval(1000L, PrintStatus_LCD);
}

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

#4

Firstly, you need to edit your post (using the pencil icon at the bottom of the post) and format the code correctly, so that it displays correctly:
Blynk%20-%20FTFC

Second,my using delay(3000); is very bad practice in Blynk. Whilst you might get away with it sometimes, you’ll almost certainly suffer disconnections if you use it regularly.
What you should do is to trigger a countdown timer of duration 3000 instead.

Take a look at this thread:

Pete.


#5

Thanks for the tip Pete!


#6

I think your term “pulse” (in MCU terms typically a rapid, possibly repeating, ON/OFF signal) in your title and topic is misleading… you seem to be simply requiring a press for a time, no pulsing required. Perhaps some editing so as to attract the proper readership and suggestions.

I have also recently added in a code routine to make any Button Widget (or perhaps any action widget, or function call) into a timed action. Should be a simple drop in replacement for those awful delay() routines :stuck_out_tongue_winking_eye:


#7

Cool, that’s what I am looking for, thanks!