Can't Update Status Widget Button

Can anyone help me, I want to make the wall switch with the button widget. I use the sample code on Github, but it doesn’t work

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
//#include <SimpleTimer.h>
const int btnPin1 = 4;             // pin for your physical push btn switch or toggle switch. just for example
const int RelayPin1 = 12;         // pin for your relay outputs or any trigger output. etc.
//declaire your other pins here for your next buttons
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "authcode";
char ssid[] = "jajal";
char pass[] = "sempak212";
SimpleTimer timer;              // add simple timer for the time interval scanning period of physical button input switch.
WidgetLED led1(V0);
void checkPhysicalButton();
int btnPin1State = LOW;           // ON
int RelayPin1State = HIGH;        // OFF
//*******Sets Relays to Off Position*****************
#define TURN_ON 0                 // TURN_ON and TURN_OFF are defined to account for Active High relays
#define TURN_OFF 1                // Used to switch relay states
void setup()
{
  Serial.begin (9600);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
  {
    pinMode(RelayPin1, OUTPUT);            //  initialize your pin as output.
    pinMode(btnPin1, INPUT_PULLUP);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
    digitalWrite(RelayPin1, RelayPin1State);
    digitalWrite(RelayPin1, TURN_OFF);     // remain off till command is receive
    // Setup a function to be called every 100 ms
    timer.setInterval(100L, checkPhysicalButton);
  }
  while (Blynk.connect() == false) {
    // Wait until connected
  }
}
void checkPin()
{
  // Invert state, since button is "Active LOW"
  if (digitalRead(btnPin1)) {
    led1.on();
  } else {
    led1.off();
  }
}
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V1);
  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V1, RelayPin1State);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V1) {                           // Map this Virtual Pin to your  Mobile Blynk apps widget.
  RelayPin1State = param.asInt();
  digitalWrite(RelayPin1, RelayPin1State);
  {
    //BLYNK_LOG("Got a value: %s", param.asStr());
    // You can also use:
    int i = param.asInt();
    int state;
    // Switch mode inverse//
    if (i == 0) {
      digitalWrite(RelayPin1, HIGH);
      Serial.println("Relay 1 = OFF");       // turn OFF your relay outputs
    }
    if (i == 1) {
      digitalWrite(RelayPin1, LOW);         // turn ON your relay outputs
      Serial.println("Relay 1 = ON");
      delay (300);                          // Delay response of your button switch (change it for you experimental but for me 300 is ok)
    }
  }
}
void checkPhysicalButton()
{
  if (digitalRead(btnPin1) == LOW) {
    // btnPin1State is used to avoid sequential toggles
    if (btnPin1State != LOW) {
      // Toggle LED state
      RelayPin1State = !RelayPin1State;
      digitalWrite(RelayPin1, RelayPin1State);
      // Update Button Widget
      Blynk.virtualWrite(V1, RelayPin1State);
    }
    btnPin1State = LOW;
  } else {
    btnPin1State = HIGH;
  }
}
void loop()
{
  Blynk.run();                               // Run Blynk
  timer.run();                               // Run SimpleTimer
}



The code you texted here that seems you used the one posted by Blynk from their examples. If it is correct then Blynk meant the code using for the physical push button (momentary button) rather the switch one you are using. In case you change to use the push button then the next step is you should select the switch option when do setting for the button widget on the Blynk app. Basically I dont see any relation between your code (virtual pin V1) and the Blynk app (the widget button shown with GP12)?!

Anyway if you want to use the physical switch, please search the forum again for the respective post as I am sure someone shared this case already. Last but not least, I wish to know the resistor value the one you use to pull down for the switch in your above schematic?

Hope this helps.

oh sorry, I forgot to give a description in the image, the size of the resistor is 1K. I have tried many examples of people in this forum but haven’t found a successful one, do you have a successful sample code that I can use. If I can, I want to try both of them, namely the switch button and the moment key. Thank you in advance.

Oh, I’m sorry, I’m not paying attention to this and now I’m successful. but what if the physical button I use is a switch not a momentary button?

basically code is shared by Blynk from their examples already. It is just about case by case that we need to revise it a bit against to individual hardware / schematic.

By the way, you should use the resistor of 10K for a better operation.

should you read my above comment once again for that case.

yes I have read it, and I have to break it by changing the code with an example from Blynk, I also use a 1K resistor and it works perfectly, but with a momentary button. Are there codes that use wall switches?

I have successfully used the sample code from Blynk: https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FSync%2FSyncPhysicalButton
To synchronize the physical button push button to the button widget on the Blynk App. But it didn’t work when I used the Wall Switch, is there here that can help me how to modify the Blynk code so I can use wall switches? Thank you in advance

Diagram-Blynk-push-and-wall

Please don’t create multiple topics for same or similar project… I merged your last one back into here

okay thank you, I think it’s different, because my problem for the status of the button update widget in the app has been resolved, once again well.
Then can you help me with this new problem?@Gunner

[Inappropriate comments removed by the author]

I don’t ask you

You ask on this forum you ask everyone on this forum…

Your issue is not a Blynk specific one, it is a “how to program” one… we (all us forum members and volunteers) teach about Blynk, how it works, how to use it, etc… and give advice as each is able. You can accept it and learn as much as you can in order to fix your own issues, or don’t. Your call.

If you browse to this forum, @PeteKnight He’s too stiff in replying to my comments. Not everyone in this forum is smart as he wants. he hopes that everyone in this forum has intelligence. then for what is there a forum asking like this, if the nature of him, @PeteKnight like that.

I have lived in this forum every day for two+ years… Pete is a soft and cuddly kitten compared to my limited tolerances for some members antics :innocent:

And he gave you some good suggestions… :wink:

But there is a really long and chock full of ideas Topic about switches, feedback, etc… You can read through it for more ideas of you don’t want any further assistance.

EDIT - Ah yes, now I remember you, sorry, I often glaze over forum names… there are so many… I guess that topic wasn’t enough reading for you after all :stuck_out_tongue:

Yes, us regulars do tend to see way too many “gimee gimee” requests in this forum. And yet we still do our best to give actual answers to the ungrateful… Look into what Pete has already suggested and let us know how it works out.

No problem @projecttrinof, I’ll try to avoid replying to any of your posts in future.
:cat: :cat: :cat:

1 Like

…But kitten, you kitten… OMG as the teenagers says :joy::rofl::joy:

@mikekgr this kitten is being a bit catty at the moment :wink:

2 Likes