Remote light switch - relay and wall switch

hi all,

Trying to figure out this one, i would like to install ESP8266 with a relay and wall light switch, which is quite simple BUT, i also want my ESP8266 to know if the light is ON if i use the switch.

Basic diagram like on the image bellow works, i can turn ON and OFF the light with either wall switch remotely through the relay. The problem is when i use phone app to control the light, i do not know if the light has been turned ON with the switch.

i have seen somewhere that someone used 1N4007 and 1/2W 47kohm in line to go from mains HOT to A0 port (analog in) to detect current and thus know if the light is on... not sure is this safe solution in a long run...

Also, if i am to install my wifi device into the wall, where the light switch is, whats the best way to power the device from available 220V to 5V.

Many thanks for any suggestions.
Alek

You need to be careful using 220v to logic level. Mistakes can be fatal. There are wifi devices made and tested that let you do this. Sonnoff IIRC.

If you diy use a basic wall wart or phone charger type of thing to get from 220 to logic level. You don’t need wires from your uC switch to the relay like you have drawn as you can use wireless between multiple uCs.

You don’t need to detect if the light is on since the uC knows the state of the light as it controls the relay. You just need the uC to update your app. The switch should not be on the 220v but only for an input to the uC.

Thanks!
Yes, i was thinking of using an old phone charger to get from 220v to 5v to power my device but could i also use it to determine if the light is on ?

many thanks,
Alek

pmagowan:
You don’t need wires from your uC switch to the relay like you have drawn as you can use wireless between multiple uCs.

You don’t need to detect if the light is on since the uC knows the state of the light as it controls the relay. You just need the uC to update your app. The switch should not be on the 220v but only for an input to the uC.

Can you please elaborate, i am not sure i follow... :slight_smile:

Many thanks,
Alek

The relay turns the light on. The relay is controlled by a uC such as an esp32. The uC can take inputs from switches, apps etc over wifi. Since the uC controls the relay that controls the light the uC always knows when the light is on. If you want to turn the light off or on through any mechanism, phone, switch, online app etc all it has to do is send a message either physical or wireless to the uC

OK, perhaps there was a misunderstanding... my board knows the state of the relay, thats true but, it does not know the state of ordinary wall light switch, thats why i was thinking how to detect current to check if the light is on when i use wall switch... currently there is no way my board knows i have used wall switch to either turn the light ON or OFF...

Also, just to clarify, i would like to be able to independently control the light with both relay and physical wall switch. So if i turn ON the light by relay, i like to be able to turn it OFF by wall switch AND in a way that my board knows it has been turned OFF (or ON) by wall switch.

If you want to turn the light off or on through any mechanism, phone, switch, online app etc all it has to do is send a message either physical or wireless to the uC

YES! But how do i send that message to my uC?

Many thanks,
Alek

You don’t need a physical switch. That is the point in having a uC. The uC does the switching. If you want a wall switch that is fine but it doesn’t have to (indeed it is detrimental) actually physically switch the light. It only has to send a message to the uC. I.e. you switch the uC and it switches the light.

Having both is a bit weird unless there is some unusual circumstances we don’t know about. You end up with the weird problem of trying to detect if the light is on when you have a device that’s entire purpose is I/O

You send a message to your uC by wiring the switch to it! Quick edit: and only it!!!

pmagowan:
You don’t need a physical switch. That is the point in having a uC. The uC does the switching.

OK, imagine this scenario :slight_smile:

I have an app that controls the light, i go out for the day, kids walk in the room and they would like to turn the light ON... OR, my phone battery is dead, no way to reach uC... i have to have a physical wall switch, otherwise its really easy and no fun! :slight_smile:

Many thanks,
Alek

Oh, and by the way, you get all the functionality benefits of the uC if you do it this way. You can have a momentary switch which if you press turns the light on or off. If you press and hold turns all the lights on or off. If you press twice turns the light on for 10mins etc etc. The light can be turned on if there is a fire alarm activated or a pir detects movement or your phone comes within range of Bluetooth etc etc

The only physical switch you should need is at the breaker.

AHHH, i think i finally got it, hahaha, darn stupid. So, instead of physical switch actually switching the bulb ON, it would simply just switch a 5V signal directly to the uC and activate relay, thus making the uC know if the light has been turned on or off?

is that what you mean? quite genial!

Many thanks,
Alek

elcrni:
OK, imagine this scenario :slight_smile:

I have an app that controls the light, i go out for the day, kids walk in the room and they would like to turn the light ON... OR, my phone battery is dead, no way to reach uC... i have to have a physical wall switch, otherwise its really easy and no fun! :slight_smile:

Many thanks,
Alek

edit: sorry posted this as you posted the above.
I am not sure you understand. The only 'physical' switch is the relay. It is the only thing that should break the circuit other than the breaker at the fuse box. It is controlled by the UC. You can have as many wall mounted switches as you want but none of them should physically break the circuit. You could have 100 wall mounted switches all over the house and garden. Each one is wired to the uC either physically or through wifi. Your kids can flick a switch and the uC will detect this and do whatever it is you want it to do (in this case turn the light off or on).

elcrni:
AHHH, i think i finally got it, hahaha, darn stupid. So, instead of physical switch actually switching the bulb ON, it would simply just switch a 5V signal directly to the uC?

is that what you mean? quite genial!

Many thanks,
Alek

Now you get it! That is the whole point of the uC to control the light. If you do this for every light and switch in your home then you have a fully rewireable home just by changing code. Light switch A can turn on any light you want, multiple lights etc or you can have switch B do it, or both all by changing code, not wiring.

Look up home automation with ESP32. There is a lot of info on using wifi to do this through apps such as node red. Youtube has videos that explain in detail

Awesome! Thank you, Karma added!

I am using Blynk app but thinking of switching to something new, still not sure whats the best solution that doesnt cost a lot.
Researching "ubidots" as well.

Many thanks man, you made my day, so simple and so smart!
Alek

No problem, glad to help. + to you

Yes, i was thinking of using an old phone charger to get from 220v to 5v to power my device but could i also use it to determine if the light is on ?

Yes. That is a safe way to do it. Another safe way is a relay with a 220V coil. (Or you can use optical isolation.)

My [u]X-10[/u]/Insteon home automation system doesn't "know" if a light is on. It doesn't use a "3-way" switch. It has a "smart switch". You can turn it on locally and then send a remote "off" command. Of course, if you send an off-command and it's already off, nothing happens.

(I didn't build my home automation system except for a "sunrise dimmer" that plugs-into an X1-controlled outlet.)

Thanks guys.

OK, now need a bit of help with the code, so if i take signal from a physical switch and have it as INPUT on pin 4, how could i control OUTPUT pin 14 that switches on the relay, in a way that i can switch ON or OFF the relay with both remote command as well as physical switch?

Many thanks,
Alek

You need to look up state machines.

Set pin 4 to input pullup
pin 14 to output and high

Generally for relays it is high for 'off' and low for on

the way I see it is you have 2 objects (relay and switch) both with 2 states

state of switch can be equal to digital read switchpin
state of relay can be on or off (1/0)

have a variable for last state of switchpin and have a regular poll (digital read) of that pin. So when the poll detects a change in the state i.e. if digitalRead switchPin is not equal to lastStateSwitchPin then you know the switch has been pressed (look up debounce code). You then want to run the onOff function in your IF statement, i.e. relayState =! relayState (switch the relay off if on, on if off)

digitalwrite relaystate to relaypin to turn the light on or off

If you use a node red or other app you just need to update the relay state using whatever functionality the method you use has for communicating with a uC

That is my take on the logic anyway. It is pretty basic and there are tutorials on state machines and you will likely have to use millis to debounce your switch.

thanks man, unfortunately i am a bit lost, the switch is killing me and not sure how to interpret you explanation and make it into a usable code.

It seemed very easy, just read input pin and make the output pin HIGH or LOW, but... :slight_smile:

Thanks,
Alek

Work with individual objects. A switch is one the relay is one.

If you have the switchPin as an input pullup (set pin to 5V on many uCs) and connect to one side of the switch, then connect the other side of the switch to ground (same ground as uC) when you flick the switch it will connect the circuit and pull the pin to ground. When you switch back it will go back to 5v. In IDE that is LOW and HIGH respectively.

switchState = digitalRead (switchPin);

https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/

This will set the variable switchState to either HIGH (1) or LOW (0) depending on if the pin is 5V or ground which depends on which way the switch is currently switched

so now you have your current switchState (switch is on or off). To make things work you need to know your last switchState to see if it has changed. You set a variable, int lastSwitchState = 0; to hold the last state of the switch. You then compare the current switchState with the lastSwitchState to see if it has changed i.e. someone has switched the switch.
if (switchState != lastSwitchState){function to do stuff} (if switchstate is not equal to lastswitchstate, i.e. someone has switched the switch)

Now you have a place to stick a function to do stuff if the switch is switched. Remember you need to set lastSwitchState to current switchState for the next loop and you need to look at debouncing.

If you are doing research look up buttons as they are the same as switches except momentary (i.e they switch and then switch back).

Thanks!
I hope i will be able to make something out of that :slight_smile: Looking for button tutorials!!

Thanks,
Alek