Controlling a device both in the app and with another device over wifi

Hi, I am using the Nodemcu v3 esp8266 board and want to achieve a blynk connection where I can trigger a function over the Blynk app as well as with another board with a physical button connected to the same wifi, so I dont need to open the app every time I want to change the value. With the code bellow the light on the button in the blynk app changes color, but it do not press it as I want it to. Is there any way this can be achieved?

This is the code I am trying to make for the “transmitter” device, so that it can trigger viritualpin 1 when the physical button is pressed.

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

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "------";
char pass[] = "------";
// Pin number for the button
const int buttonPin = D5;

// Variable to store the button state
int buttonState = 1;

void setup()
  // Debug console

  // Set the button pin as an input
  pinMode(buttonPin, INPUT_PULLUP);

  Blynk.begin(auth, ssid, pass, "", 80);

void loop()
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    // Set the value of V1 to 1
    Blynk.virtualWrite(V1, 1);  //syncVirtual
  // If the button is not pressed
  else {
    // Set the value of V1 to 0
    Blynk.virtualWrite(V1, 0);  //syncVirtual
  // Read the button state
  buttonState = digitalRead(buttonPin);;

Your code is written so that it makes two calls to the Blynk server (a Blynlk.virtualWrite and a Blynk.syncVirtual) every single time the void loop executes. This could be hundreds, if not thousands, of times per second.
You shouldn’t be making more than ten calls to the Blunk server every second, and you should never have Blynlk.virtualWrite or Blynk.syncVirtual in your void loop.

Your code only needs to write either a LOW or HIGH value to Blynk when the stat of your physical switch changes, not constantly.

I also suspect that you are using two devices with the same Blynk auth token, which is totally the wrong way to approach this problem.

In addition, your sketch is missing the Firmware Configuration lines of code from the beginning of the sketch, so won’t work with Blynk IoT.


Ok, I think I understand a bit more now. I am using the same auth token on both devices because I thought they needed that to be connected. How could the two devicec be connected then? Could you give me a example code for both devices ?

You can try automation, which is the easiest way.

To avoid you send more than one pin change to the blynk server , use a toggle function.
See this example

Replace the LED with a write to Blynk