Using button as switch with bridge

Hey everybody, I have a project box that has 3 buttons hooked up to the nodeMCU and two devices I want to control with this project box.

Basically I have relays/servo lightsw in the bedroom and living room and I want to control with the project box so I don’t have the wait for the app to load, then to switch to the tab I want.

As of now, it works to an extent. Whenever I plug in my project box, it’ll kick on the light or send the command for the servo int value on boot. That doesn’t bother me much. Once it has booted, I get one chance to press the button which turns the light off but after it sends the command if I press the button again nothing happens. I notice I get a nice (One or more device disconnected) message in the app. I believe that’s the crap in the loop I’ve placed. I haven’t set up the other buttons yet for the other two things I want to control.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <WiFiUdp.h> //OTA NEEDED
#include <ArduinoOTA.h> //OTA NEEDED

char auth[] = "b5"; //blynk auth token for Main device
char auth2[] = "9a"; //Auth Token for the additional project that you would like to control
char auth3[] = "d"; //relay above is lightsw
// Your WiFi credentials.
char ssid[] = "x"; //Wifi Name
char pass[] = "x"; //Wifi Password // Set password to "" for open networks.

//Bridge Widget on virtual pin 1 (not currently required in the app)
WidgetBridge bridge1(V25);
WidgetBridge bridge2(V26);

int but1 = 16; // Bedroom sw
int but2 = 5; //lamp
int but3 = 4; // led
int val;
int val2;

void setup()      
  pinMode(but1, INPUT_PULLUP);
  pinMode(but2, INPUT_PULLUP);
  pinMode(but3, INPUT_PULLUP);
  ArduinoOTA.setHostname("box"); //Device Name
  ///ArduinoOTA.setPassword((const char *)""); // Device Password for OTA Upgrades
  Blynk.begin(auth, ssid, pass, IPAddress(10, 0, 0, 142), 8442);

  bridge2.setAuthToken(auth3);// Place the AuthToken of the second hardware here

void loop()
  ArduinoOTA.handle(); //OTA NEEDED;
  val = digitalRead(16);

void butassw() {
 if(but1 == HIGH) { //If Pin 2 is high (At 5 Volts)
   val = 1 - val; //If Button is set as 0 then Button = 1 - 0 (=1) and if Button is 1 then Button=1 - 1 (=0)
 if(val == 1 && val2 == 0) { //If Button is 1 and OldButton is 0 (This means basically means "If the button was just pressed"
     bridge1.virtualWrite(V3, 103);//do bridge code//Put what you want to happen when the "Switch" is on here
    delay(500); //Delay for half a second
 if(val == 0 && val2 == 1) {
   bridge1.virtualWrite(V3, 22);//do bridge code//Put what you want to happen when the "Switch" is off here
   delay(500); //Delay for half a second

 val2 = val; //The data is now old

int but1 = 16; // Bedroom sw

if(but1 == HIGH)

Shouldn’t this be if(val == HIGH) ? but1 has a value of 16 and I’m unsure how it is interpreted by the program when HIGH/LOW checks for a 1 or 0.

You would probably want to get rid of any delay() in your code before my good friend @Gunner sees it and give you a slap on the wrist :yum:

Here is some useful examples on how to sync and check buttons:

1 Like