*************************************************************
This example shows how to monitor a button state
using polling mechanism.
App project setup:
LED widget on V1
*************************************************************/
/* 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[] = "YourAuthToken";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
int prevState = -1;
int currState = -1;
long lastChangeTime = 0;
void checkPin()
{
// Invert state, since button is "Active LOW"
int state = !digitalRead(2);
// Debounce mechanism
long t = millis();
if (state != prevState) {
lastChangeTime = t;
}
if (t - lastChangeTime > 50) {
if (state != currState) {
currState = state;
Blynk.virtualWrite(V1, state);
}
}
prevState = state;
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
// Make pin 2 default HIGH, and attach INT to our handler
pinMode(2, INPUT_PULLUP);
}
void loop()
{
Blynk.run();
checkPin();
}
I can directly let the button control D1 on the LED connected to the light and off. But I use the program to control the LED in the LED light and off, I do not know where the reason.
Are there enthusiastic friends who can answer this question for me?
If the question I have provided is not enough, you can leave a message and I will add it. Thank you for taking the time to read and answer my questions!
@mtrucc I’m guessing you misunderstood what the original sketch was doing.
It relates to a physical button on digital pin 2 not a Blynk virtual button on V2.
From your screenshots that is what I think your error is.
If you have actually connected a physical button to digital pin 2 let us know and we will debug further.
I once again seriously look at the code, I think you are right, I connected the APP on the virtual button, rather than the physical button. Then pin 2 in Nodemcu which is D2?
pinMode function I did not find in the document ~ so i did not quite understand this function.
It’s a basic Arduino function to define input / output state of a physical pin.
Are you wanting to use virtual pins / real pins i.e. a physical button or a Blynk virtual button?
*************************************************************
This example shows how to monitor a button state
using polling mechanism.
App project setup:
LED widget on V1
*************************************************************/
/* 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[] = "YourAuthToken";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
}
void loop()
{
Blynk.run();
}
BLYNK_WRITE(V2){ // Blynk virtual button
if(param.asInt() == 1){
Blynk.virtualWrite(V1, 255); // turn ON virtual LED
}
else{
Blynk.virtualWrite(V1, 0); // turn OFF virtual LED
}
}
This is a simple work
Use the virtual buttons on the APP to control the on and off of the LEDs on the APP