Arduino+esp8266+relay+buttons

I am making a project so that I can switch the lights in my room using blynk app over internet. So, far I am able to control the lights with blynk and arduino+esp8266 but the problem is that if in case internet connectivity is not there or the esp fails to connect to wifi then there is no means by which I can switch the lights. I don’t want to run local server but instead I want the lights to be controlled with toggle(rocker)switches in case of failed esp only. So far I have used the code below :slight_smile:

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

char auth[] = "blynkcode";
char ssid[] = "my ssid";
char pass[] = "password";

#include <SoftwareSerial.h>
#define RX 2
#define TX 3
SoftwareSerial EspSerial(RX,TX); 
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(20);

  Blynk.begin(auth, wifi, ssid, pass);
}

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

I don’t know how should I access the connectivity status of esp8266 based upon which a button can be coded to switch the relay. Actually, I am making a kind of switchboard which may have switches (I have a four channel relay module) for controlling the lights, fans, or may be a three pin socket. Until now I am successful in making it work using blynk app which was easy enough. But the problem is that if there is some error in connectivity with esp8266 to internet or something else then there is no way to switch the relay. What I want is some physical buttons may be push buttons or toggle so that it retain functionality in case of esp8266 connection error. Thanks in advance

Its better to use a (momentary) pushbutton connected to the circuit. And then then in the loop you check for the switch to be pushed and throw the switch, something like this:
PSEUDO CODE!!

buttonPushedState = digitalRead(PUSH_BUTTON_PIN);
if (isPushed){
	if(!buttonPushedState) then isPushed = false
}else{
	if(buttonPushedState) then lightOnState =  !lightOnState
	isPushed = true
}

you need the isPushed bool or else if you hold the pushbutton for a couple of seconds the light starts to flick on-off-on-off etc.

if you’re adamant on using a rocker, you need to check its current state and

IF currentRockerState != oldRockerState then lightOnState != lightOnState
oldRockerState  = currentRockerState
1 Like

yes, you are right it starts flickering on-off if I use a toggle switch. But the main problem for me is the wifistatus:thinking:. I want to use button only in case the wifi is out and I am unable to connect to the arduino through blynk.

First of all, you didn’t format your code properly. Go back and edit your post and add in the back-ticks and cpp as described in the text that you deleted before posting this message.

Are you talking about lights that are built into the ceiling, or lights that plug into wall sockets?
Which country are you in?

Pete.

thanks for reply. The lights are plugged into the socket. I am from India. And for the code part I will take care of formatting in future posts.

You should go back and edit your original post.

The easiest solution - if your sockets will take the two-pin European plugs - is to simply buy a Sonoff S20 and flash it with Blynk code. Or buy one of these sockets and flash it with my code:

If you want to add a push button to your own device then look at the comments in my code and you’ll see how to do it so that it works with my code. You’ll need to modify this code to work with an Arduino + ESP-01 and remove the OTA code as this won’t work with an Arduino.

Pete.

there’s a small pencil like icon beneath your first post, click on it to edit it.

is it okay now?

sorry, there is a misunderstanding. I am making a kind of switchboard which may have switches (I have a four channel relay module) for controlling the lights, fans, or may be a three pin socket. Until now I am successful in making it work using blynk app which was easy enough. But the problem is that if there is some error in connectivity with esp8266 to internet or something else then there is no way to switch the relay. What I want is some physical buttons may be push buttons or toggle so that it retain functionality in case of esp8266 connection error.

yes.

well that was the pseudo code I provided in my first post. What else are you looking for?

I understood the basics of what you want to achieve.
The thread I linked you to is about a device that’s an ESP8266 processor with a relay, a push button switch and an LED.
It’s capable of being programmed with Blynk code like an Arduino.

A push button switch is the best option for your situation, as it makes writing the code much easier. With a push button switch you need to deal with “switch bounce” which is where the contacts might make and break the circuit many times in a fraction of a second when the button is being pressed or released. The code in the project I linked to has a switch debounce routine in it.
With Blynk you need to use a timer to sample the status of each switch multiple times per second to see if it’s been pressed or not. My code does this, but just for one switch.
What my code doesn’t do is to incorporate any routines to prevent Blynk from blocking the code flow if there is no internet connection. I made this clear in the write-up, but the forum has some good examples of how to work around this.

You might want to take a look at these two threads where @zodiac has take the mains power strip to a new level:

I don’t think there are any code examples here, but there’s is some interesting reading for someone’s who’s about to make a similar project.

Pete.

Hi, I am able to control the relay with psysical button and sync its state with blynk online but the problem is that as soon as I turn off the data connectivity the button looses its functionality means it cannot control the relay. But after switching on the data it again starts functioning. Can you please suggest me how can I gain physical button functionality in case of no network…

The Blynk connection process (via Blynk.begin or Blynk.connect) is constantly trying to connect to the server when the Wi-Fi connection is unavailable, which is blocking your other code from running.

If you search the forum for posts about non-blocking connection and running code while offline then you’ll find plenty of examples of how to overcome the issue.

Pete.

1 Like

@Ranit I have a form of boilerplate example here… this is just my take on how I do it… lots of different (and possibly better) ways

The timed functions, in this case UpTime to serial monitor and blinking LED, keep running regardless of connection.

1 Like

Thanks, it helped me a lot. Now, my project is fully working with push buttons in case of no internet. The code for configuring the esp8266 arduino shield

Blynk.config(wifi, auth, server, port);
  if (Blynk.connectWiFi(ssid, pass)) {
    Blynk.connect();
  }

also proved a lot helpful.