BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

ESP12F + Blynk + Physical Switch


#1

Hi to every one. After reading some post about ESP12F + Blynk + Physical Switch I can’t get it working. I need to control the light from Blynk and from the physical button too, and this have to change the state of the button on the blynk app.

I uploaded the code to my board (ESP12F + 2 Relays with Electrodragon V1.5) and I can’t get the physical button working but there isn’t a problem from the button.

This is the board Im using http://www.electrodragon.com/product/wifi-iot-relay-board-based-esp8266/ and this is the code that I’m using

#define BLYNK_PRINT Serial

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

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Fibertel WiFi265";
char pass[] = "xxxxxxxxxx";
    
SimpleTimer timer;

void myTimerEvent()
{
  if (digitalRead(4)==0)
  { 
    Blynk.virtualWrite(V12, !digitalRead(12));
    digitalWrite(12, !digitalRead(12));
  }
}

BLYNK_WRITE(V12)
{
  int pinData = param.asInt();
  digitalWrite(12, pinData);
}


void setup()
{
  Serial.begin(115200);
  
  pinMode(4, INPUT_PULLUP);
  
  pinMode(12, OUTPUT);
  digitalWrite(12, HIGH);

  Blynk.begin(auth, ssid, pass);

  Blynk.syncVirtual(V12);
  
  timer.setInterval(250L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run();
}

I hope you can help me I can’t sleep because this!


#2

I properly formatted your code for forum viewing. Please take note on how that was done, for the future.


#4

I don’t think that is a valid use of the Boolean Operator !(not)


#5

Nope, this is valid. :+1:


#8

@JuandeSnaider Just a guess, but try different pins for your physical button.

Near as I can tell, this board has two built-in buttons, with pin breakouts, on IO0 & IO2, why not use one of those pins instead of IO4, which with the pull-up seems set for I2C use. Or try IO15?

http://www.electrodragon.com/w/ESP_Relay_Board


#9

@JuandeSnaider I tried your sketch (modded for my UNO/USB hardware) and after a few clicks, it works fine. I still suspect you have an issue with the physical button.

I did make a minor sketch change though; Assuming you are using the latest Blynk library, then instead of SimpleTimer use the built in BlynkTimer… probably works anyhow, but good to keep updated with the new timer formats.

Remove the SimpleTimer library

// #include <SimpleTimer.h>

and change to this line in your pre-setup

BlynkTimer timer;

#10

Add this to your loop:

reading = digitalRead(inPin); //define in pin as the pin your button is connected to

 if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH){
      state = LOW;
       }
    else{
      state = HIGH;
     
    }
    time = millis();    
  }

  digitalWrite(relayPin, state);

  previous = reading;

#11

Well, after a few weeks and change the GPIOS I get it working!!
Now I’m trying to solve the use of a physical switch, as I said, I get it working but it only work with a physical push button, if I use a normal switch button the board non stop changing the relay state.

Could any body help me?


#12

Please show us your current code with any changes you have made, and can you clarify what “button” works and what you are having issues with: E.G. Widget Button (momentary or switch) or Physical Button (momentary or switch)


#13

Well, the code Im using is the same has I published but I changed GPIO4 to GPIO15 that has an easy access.

The button that cause me troubles is the Physical switch button connected to the GPIO15.
With a Physical momentary button its works fine but as I said if I use a switch button its changes the relay state continously. The same happen if I hold down the momentary button like a switch would do.

The code:

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <SimpleTimer.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Fibertel WiFi265";
char pass[] = "xxxxxxxxxx";
    
SimpleTimer timer;

void myTimerEvent()
{
  if (digitalRead(15)==0)
  { 
    Blynk.virtualWrite(V12, !digitalRead(12));
    digitalWrite(12, !digitalRead(12));
  }
}

BLYNK_WRITE(V12)
{
  int pinData = param.asInt();
  digitalWrite(12, pinData);
}


void setup()
{
  Serial.begin(115200);
  
  pinMode(15, INPUT_PULLUP);
  
  pinMode(12, OUTPUT);
  digitalWrite(12, HIGH);

  Blynk.begin(auth, ssid, pass);

  Blynk.syncVirtual(V12);
  
  timer.setInterval(250L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run();
}

Thank u.


#14

¡UP! I need some helppp haha


#15

This is what I have in my test setup… adjust accordingly and compare

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    if (btnState != LOW) { // btnState is used to avoid sequential toggles
      relayState = !relayState;  // Toggle LED state
      digitalWrite(relayPin, relayState);
      Blynk.virtualWrite(V2, relayState);  // Update Button Widget
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

#16

Gunner, I dont understand what I have to change to get you code working… Could you give my code and the yours mixed? or comment what I need to change and what I need to leave as it is.

Yes, Im a beginer on Arduino.

Thank you so much of course!


#17

Sorry, not as simple as that. And honestly, teaching programming 101 is outside of this forums preview.

I too am always learning how to program, and the best way for me is to look at examples, and keep it simple with trial & error until I understand the process, then slowly add on each function until I have a working whole.

Don’t try to get my small function example to work on your setup… it is just showing a method of preventing sequential toggles. Rather, just read through it and study the Arduino reference page until you understand what all those ! + != etc. Comparison Operators and Control Structures meen.

https://www.arduino.cc/en/Reference/HomePage


#18

Gunner, after some logic “interpreting” of the code I get it working… And its “works”, its doesnt change the state at every loop start but if I change the switch position the relays doesnt change, I need to change the position again to get the relay change… Do yo know something about that? thank u!


#19

Repost your current code… I just can’t figure logic questions like that without seeing it :slight_smile: