Hello i have a lamp thats opens and closes from a switch. I want to add the functionality to open and close it from both raspberry pi and switch. I read in some posts to add a relay with multiway switching. I will create a web interface but i want to read the state of the lamp and track it if that changes from either raspberry pi or switch. How can i achieve that? Thank you
For switching yes you could use multiway switching very easily - you can also use various methods to test if the light is on:
Test when the bulb is drawing current:
This could work by having the base of a transistor (via a separate wire and probably a large resistor) connected to the to light bulb curcuit, and a Pi GPIO input (probably via another resistor) (and ground power supply etc). When the bulb draws current - i.e. is on - the transistor should switch and send current the the Pi's GPIO, so you can know when it is on. A common ground wire will likely be needed.
Test to see if there is light:
You can use a LDR (Light Resistor - you could use a light sensor package) to test:
A very basic view of how a LDR works... Source
The resistance of a photoresistor decreases with increasing incident light intensity, so (yet again probably via a resistor or transistor) once the LDR output current gets above a certain level, a Pi GPIO should be able to detect when the light is on.
If thats too complicated there are many sensor light packages available - for example:
N.B. The maximum current that can be outputed through the GPIOs is apparently 30mA (What is the maximum current the GPIO pins can output?), so make sure the current in isn't too hight/
For turning on or off through Raspberry Pi I would connect Gate pin of a FET(field effect transistor) to a GPIO pin of it. I'd choose a transistor which requires low gate current to get into active mode since GPIO pins can supply little current. So it'd be like when you turn that pin high, load will get current(on), when low, no current(off). Then I'd connect the switch parallel so when you turn either of them on, the lamp will turn on.
For understanding if the lamp is on or off a photocell can be used as a cheap sensor and according to analog value you can understand if lamp is on or off, but it wouldn't work well if other lamps also illuminate the sensor which is only supposed to sense the lamp of our subject.
Of course there are tens of other ways for doing such a task