A Wi-Fi Controlled Light Switch
by kononowicz24 in Circuits > Remote Control
405 Views, 1 Favorites, 0 Comments
A Wi-Fi Controlled Light Switch
Hi! Have you ever been soo tired that you forgot to switch off the lights in your room? Or you just don't fancy getting up from a warm, cosy bed and hitting the bedside lamp switch? Probably we all know that feeling. That's why I would like to present you how I made a smartphone-controlled base to a future home automation system - a Wi-Fi controlled light switch.
The Parts List:
Determining what parts should we use is always fun. A lot of searching for the cheapest and most reliable part while also looking at other ones in the store and thinking about another good project :D
First, we would need a brain for our project.
I chose Wemos D1 mini board, as it not only provides Wi-Fi connection for the switch, but has 11 GPIO and is programmable via the Arduino IDE software, so we don't need to use another microcontroller. I bought one for $2.69 from Aliexpress.
OK. Now we would need an actual switch that will break the connection in the mains line.
So I purchased a 2-channel relay module for $0.85 again on Aliexpress.
Another important element is a power supply. I chose this 230VAC to 5VDC transformer for $1.99. It can handle 3W of power which would be more than enough.
We would also need a project case (as which I used a pralines box) :D , a power cord, some jumper wires and a wall socket (the best is a one that sticks out of the wall and is mounted on 2 screws)
Eating the Pralines From the Box
Once the box is empty we can proceed to mounting the wall socket onto the case. My box was too hard to drill in it, so I used a knife to make 2 small cutouts. Then I secured the socket with small bolts, nuts and pads in place.
I followed with mounting the transformer on a piece of perfboard to make soldering easier. Then I stripped the power cord and soldered the wires to AC pins on the transformer.
Wiring All the Components
Now the fun part - connecting every piece and meanwhile not messing anything :D
The schematic shows all the connections we have to make between the boards, except the neutral for the controlled socket, which goes directly to power cord. For the high voltage connections always use the wires that can handle the power - e.g. we can shorten our power cord earlier and use the cut fragments in between the socket and relays. It's also good to keep the wire colors right (live wire brown, neutral blue and ground yellow-green).
Coding the Wemos Board
Now the most boring part - the coding. As I'm a very lazy person I used an existing example sketch and modified it a little bit to respond to our commands. The full code is available on my GitHub.
Now we can upload the code to the board. Hmm, but wait man, how? It's not an Arduino board. First we have to add board definitions to our IDE to make it visible. Then we search the boards for our Wemos D1 mini, set the upload speed to 115200bps and then, finally upload the code.
Controlling the Socket
Now, if we disconnect our USB cable and connect our board to the mains socket, it should connect to our Wi-Fi network and respond when we open it in the browser.
But what is the address we should write on the browser bar? There's where "mDNS responder" comes in handy, because we don't need to know the IP address of the board. In the code we have declared that the mDNS would respond on the name "remoteSocket01" and the server is set up on port 81, so the full address would be http://remoteSocket01.local:81 . All the work is set up on /[output]/[on or off]
What if we can't connect to the board with the mDNS address, e.g. on the smartphone? Here comes in handy an Android app called IP Tools. One of the features is that we can search the local network for all connected devices. We would seek for a device produced by Espressif Inc. and read the IP that is shown above.
If you don't want to type the address on the bar each time - I made an android app to make everything easier. The basic features as for now are toggling the socket on or off, and "refreshing" - making it off for one second and swithing it on back again. You can download it from here.
Hope you would enjoy a DIY smart bedside lamp :D
Until next time, Krzysztof :)