After implementing some clumsy code to detect a press-and-hold on a Blynk button, a fellow Blynker suggested that I try OneButton. @vshymanskyy agreed that it should work. Well, something’s not right. Hopefully, someone with experience using OneButton can help. I’m thinking that maybe it just doesn’t know how to handle Blynk’s virtual pins. Anyway, here’s my test code:
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include "OneButton.h"
//WiFi and Blynk connection variables
char auth[] = "AUTH TOKEN"; // Blynk token "YourAuthToken"
// Setup a new OneButton on pin V1.
OneButton button(V4, true);
void setup() {
//Creates an AP if no wifi credentials are stored
WiFiManager wifi;
wifi.autoConnect("ESP8266");
Blynk.config(auth);
Serial.begin(9600);
// link the button 1 functions.
button.attachClick(Click);
button.attachLongPressStart(longPressStart);
}
// main code here, to run repeatedly:
void loop() {
Blynk.run();
button.tick();
}
// This function will be called when the button is pressed 1 time.
void Click() {
Serial.println("Click.");
}
// This function will be called once, when the button is pressed for a long time.
void longPressStart() {
Serial.println("LongPress");
}
// Update V4
BLYNK_WRITE(V4) {
}
Just add Blynk.virtualWrite(V1, "some value");
in your Click or longPressStart handlers…
And add a value display widget or something similar (set it to pin V1).
Tell me if it works!
@vshymanskyy I added the Blynk.virtualWrite statements and a gauge widget on V1, but OneButton is still not calling the Click and longPressStart functions. I also added a Serial.Println statement in the BLYNK_WRITE(V4) function, just to make sure that there was a solid connection to Blynk. No problem there. I got a “V4 from Blynk” in my serial window every time I pressed the V4 button. Maybe I didn’t understand you correctly. Here’s the altered code.
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include "OneButton.h"
//WiFi and Blynk connection variables
char auth[] = "BLYNK AUTH"; // Blynk token "YourAuthToken"
// Setup a new OneButton on pin V4.
OneButton button(V4, true);
void setup() {
//Creates an AP if no wifi credentials are stored
WiFiManager wifi;
wifi.autoConnect("ESP8266");
Blynk.config(auth);
Serial.begin(9600);
// link the button 1 functions.
button.attachClick(Click);
button.attachLongPressStart(longPressStart);
}
// main code here, to run repeatedly:
void loop() {
Blynk.run();
button.tick();
}
// This function will be called when the button is pressed 1 time.
void Click() {
Serial.println("Click.");
Blynk.virtualWrite(V1,50);
}
// This function will be called once, when the button is pressed for a long time.
void longPressStart() {
Serial.println("LongPress");
Blynk.virtualWrite(V1,50);
}
// Update V4
BLYNK_WRITE(V4) {
if (param.asInt()){
Serial.println("V4 from Blynk");
}
}
I agree with @vshymanskyy here, it looks like it should work (emphasis on should of course). The only thing that bugs me a little is the fact that the library declares the pin as being an input pin. In case of V1, does that work ok? I’m not sure. You can try commenting that out in OneButton.cpp and see if that works.
Also, I think this library was made to debounce and reduce jitter from intermittent electronic signals, but since you are using a vPin that should not be an issue I think.
I think this could well be an idea to implement in the Blynk library. Multiple state buttons (click, user-defined hold period or on/off?). Could be an idea
Still having no luck with OneButton and the virtual pins. I just rewrote my own code to better handle the press-and-hold condition.
what about the limits of request every 10 ms , maybe long press not playing nice with that as it keeps sending (0,1) in higher frequent.
which is ignored ether by the server or the hardware.
@vshymanskyy will have a better idea of that