2 way lighting circuit control with blynk and feedback updates

hello!

sorry for just dropping in, but i still do not understand exactly what do you want to accomplish with this setup…

do you want to turn on / off a lamp from:

  • blynk app
  • physical switch
  • motion sensor

this is correct?

@wanek - Jump right in !

It’s to monitor a physical switch, so I know at a glance if it’s on.

Cheers

Lane0138

ok, but if that’s all, wouldn’t be easier to use a simple push button instead the physical switch? like you have on the door bell?

than every time the button is pressed, it toggles the lamp. and in the mcu you have to monitor only the state of the digital pin, which controls the relay, to know if the lamp is on or off.

why to complicate with current measurement and other stuff?

exactly, but to feedback and update the widget when either the physical switch is pressed or the sensore triggers. without the optoiolator board(which senses the feed to the lamp becoming live and in turn sends a 5v signal to the arduino digital in pin which then updates widget state) I don’t know another way of achieving this.

Unless someone out there does!!!

Cheers

kev

not exactly. I want to be able to flick a physical switch on(or off), or switch widget on blynk to turn a light on(or off) but for the state of the widget to reflect this. If I just flick the physical switch how does the widget know it’s on ???

its basically a 2 way circuit so I can switch on and off at each end, but the app to reflect the actual state of the lamp

Cheers

Kev

very easily!
because you should wire the physical push button not to the mains, but to only the mcu digital pin (5volts).

so, when you press the button (physical or virtual in the app, doesn’t matters) or the motion sensors triggers, that actually sends a signal to the digital pin, and the software triggers that relay that turns on the lamp.

i have made this exact project some years ago for an entire house. it was possible to turn on off the lights from phone and phisycal push buttons, but we ran the buttons on 5 volt only, all hooked up to an arduino mega, and used 3 X 16 channel relay board. there were motion sensors also involved, and the user could achieve different actions based on short click or long click on the push button in the walls.

no current measurement and analog pins needed, and complicated hardware!

@wanek,

So you use two Digital Pins to control every one “lamp” .
D1 - Relay Control “lamp” (live mains (Blk) ) to NO (so if the power blips,it comes back, lamp off.)
D2 - Physical Switch - Position Sensor “lamp”

Do I understand you correct? Then you just monitor a state change in D2 && V1 to turn on a Blynk Lamp indicator LED V5.

Basically right?

Lane0138

yes, it is something similar. it depends what do you want to do, it is fully customizable.

all the pir sensors + physical buttons (lets call them buttons, not switch, because switch has at least 2 stabile position) are working with 5v ttl signals (high, low)

the mains are only used at the relays. and yes, every lamp (or wall plug) you want to control needs 2 digital ports: one for the button (input) and one for the relay (output).

this is why advisable to use arduino mega, because it has around 70-75 pins. from that some are used for networking (i used enc28j60 module), some are used for rx/tx, so you actually will have around 60 usable ports. 60 / 2 = 30 lamps. i think that is enough for one normal house. but if you have very big house, you can have 1 arduino mega at every floor, say. this way the system is more redundant, if something dies.

regarding the lamps / wall sockets state after a power loss: the arduino mega is powered from a small capacity ups or battery, so in the event of blackout, the current states are not lost. if you want to make the system super fancy, you can continuously save the state of every relay into eeprom, so even if the system freezes or crashes, on restart it will restore every relay in the last used position.

we used these boards:

(they are cheaper on ali)
it needs 12v source. has onboard 5v supply for arduino, optically isolated digital channels, feedback leds for the state of every relay. every relay has 2 basic position: you can use them either no or nc. they are rated for 10a, but i wouldn’t recommend more than 5-6a for continuous consumption. (for longer life and piece of mind)
but, actually, for lamps it is hardly needed more than 0.5a nowadays)

if one wants to use for wall socket with big consumption, it can use the relay to cascade a much stronger heavy duty relay with it, so theoretically this setup has no limits, you can even turn on / off 30-40 amps.

Hi @wanek had thought of this route but I am using existing two way wiring and also do not want reliance on arduino circuitry. If the network or the arduino fails I can still switch on from the physical switch.

So all I am doing is replacing one of the mains powered 2 way switches with the a relay. Zero wiring alterations required.

Cheers

kev

1 Like

yeah that’s very similar to what I am doing, except I do it with mains and the switches are switches, not push buttons. I also store also the states of everything so in the event of arduino reset or power failure everything is restored from where it was prior to failure. Again in a similar manner I need 2 digital pins per channel one for relay and one for ac feedback.

both result in the same, one a low voltage solution and one a mains solution.

cheers

kev

indeed, if you have an existing wiring, it is less modification with your solution.

the project i’ve made was for a newly built house, and in that case the electrical wiring it was designed especially for this task.

the code was written that if the internet is down, it can work in offline mode (non blocking), only with the physical switches. although, if the arduino or relay board dies, it is a real problem. (not happened so far)

cool man - so I guess somehow you must latch the relay from within the code as it is only a push button correct ?

Would you share your code always nice to see other ideas ?

cheers

kev

PS guess you are a Floyd fan ? my dog is even called Floyd!!!

please elaborate this, i do not understand what are you asking (english is not my native language, sorry)

and, yes, pf rocks! (but also ac/dc, and dp, and others :wink:

Actually I had the same issue as you all are facing then I dig up from the Blynk Docs that the virtual pins article was found that it is the key. I intend to share with you when I have seen this thread.

I spent time to read this thread from beginning that I appreciate for @newdos his passion on his solution. I am sure it works perfectly for the purpose but surely I will not use it in term of costing even physical wiring. Hence the solution advised by @wanek is really the best or at least it is the solution I am aware the best till now.

Using a physical pin on ESP like input pin (input_pullup) to connect to a momentary push button. Anytime you push the button once (triggered with LOW) it will change an output pin stage which connected to relay and as long as your code linking to virtual pin then … @newdos and @Lane0138 you could refer to my code as following

#define BLYNK_PRINT Serial

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

const char* ssid = "xxx";
const char* pwd = "xxx";

char auth[] = "xxx";
    
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, pwd);

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

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

In the above code I used GPIO4 as input_pullup to control the GPIO12 connected to relay. On Blynk you use a button set with V12. Following to this solution, relay used NO only. Momentary push button connected to GPIO4 (triggered when LOW) and we do not need to use this http://www.ebay.co.uk/itm/272461222192?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

I send attached how the wiring is

As well as the video how it worked. Note 1st clip in case you do not pullup GPIO4 (using hardwire) while 2nd clip it pulled up (the wire 3.3V removed)

Hope this could help.

Hi @wanek

What I mean as the button you push is not a latching one ie a momentary one, how do you hold the relay in that state - guessing you do this with software and the push button just toggles the state of the switch from on to off with each push etc. My switches are either always on or always off ie the switch latches and stays in that state until you change it.

does that make sense ?

Cheers

kev

HI,

Yes agree this is fine if you can run 5v wiring in your building, but no good if existing mains 2 way wiring circuit. That was the whole point of my exercise, was to replace a 2 way light switch at one end and to be able to control it either from phone or the other existing 12 way switch and provide feedback to the widget which you can only do in this scenario with the current sensing opto isolator board.

Both work equally well so I guess it ‘horses for courses’ and what state your wiring is in at the outset.

Cheers

kev

1 Like

Do I see a problem with the switch sometimes ‘bouncing’ here or is it just a slightly dodgy switch? it seems to stay on when you pressed it a couple of times ?

Cheers

kev

@newdos: Yes it is fine and it is up to you bro ! My way is just to share what we can make more simply in terms costing, wiring that you could put all in one place …actually just a hole in wall :slight_smile: where you could put 5VDC switching power supply along with 3.3 dc step down board as this is really good for installation especially to modernize the conventional switches transforming to use this IoT thing. People they will not use it if we make something that it is a bit different from what they are used to … :laughing:

a) 5vdc switching power supply …cheap, less space, enough surely for 4 relays https://www.aliexpress.com/item/1pcs-AC-DC-85V-265V-700mA-3-5W-Isolated-Switching-Power-Board-Module-Industrial-Power-Supply/32295592941.html?spm=2114.13010608.0.0.BOFmCa

b) 3.3vdc step down board to feed for ESP board https://www.aliexpress.com/item/DC-DC-Step-Down-Converter-Power-Supply-Buck-Module-4-5V-7V-to-3-3V-AMS1117/32593069829.html?spm=2114.13010608.0.0.pKOxkr

About the bouncing: there are 2 root causes here:

  1. hardware …perhaps the button as my press is quite light :slight_smile: …hopefully as I can feel when pressing
  2. yes it is really about the bouncing issue but you could eliminate this by adjusting the interval.

timer.setInterval(**250L**, myTimerEvent);

In my code it is 250L ~ 250ms so we can shorten it by 100L (I have just seen this factor advised by Blynk in their DOCs http://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FSync%2FSyncPhysicalButton ) hope it will better. For the reality I dont think someone press once then do the next press within 1-2 seconds (kid or tester like me :slight_smile:) so I do not code for the bouncing but you could revise it to see how.

Thanks for your comments and concerns.

yes, it makes sense.
i have made it in code, of course.
i’m on phone right now, but i will send you some code probably later today.