Smart home and IoT are rapidly growing branches of IT. But turning your home into a place packed with electronics can get pretty expensive pretty fast.
Most smart switches cost around 60$ just for one light. Given the number of lights in a house, the amount of money you’d have to invest gets really high. Add to it the fact that when you decide to buy one brand of switches you also need to look for other devices compatible with the ones you have, and you get a major headache that turns many people away from smart home technology.
But worry not! There is a solution for a cheap, open, and secure smart home. You just need to get your hands dirty and code a little. If that doesn’t scare you, let me explain how we’ve managed to control lights by voice with only 5$ per switch!
The elevator pitch is this: we will put cheap smart switches with our own custom firmware inside a light box. They can turn the power to the light on or off and they can be controlled over wifi. There will also be a regular light switch that will connect 2 pins on the device, which will turn it on or off (like in the image above). This device communicates with your local server (in our case raspberry pi), which can be controlled from anywhere. Result: You get switches that look and act like normal ones, but you also can control them thorugh a website or Apple’s HomeKit. There’s lot to do to achieve it, but the result is well worth it. Interested? Then let’s just jump into it!
The main heroes of our setup are Sonoff Basic switches (https://www.itead.cc/sonoff-wifi-wireless-switch.html). They sell for 5$ on AliExpress with no shipping cost for small purchases. However they are not ideal out of the box. They connect to Chinese servers over the internet , and you can only control them using this remote server. Depending on your location, this might take some time, and you may not want your switches’ digital footprint to travel outside of your home. Fortunately, we can flash them with our own custom firmware to connect with our local hub locally over wifi.
IMPORTANT - We will put the switches inside light switch boxes. If you don’t have the space to fit them there, this solution will not work. Standard European ones are too small, so make sure to mount extended ones in your house. If this is not an option, you can use Shelly 1 (https://shelly.cloud/shelly1-open-source/). It’s very small, but twice as expensive. It supports MQTT out of the box, so you don’t need to flash it.
The hub we will use is the most versatile one - Home Assistant. It’s created by a community of DIY lovers, and it has lots of features. It requires you to know a little bit about coding, but just the basics. We will run it on a Raspberry Pi 3.
So, the first step is to the buy required devices:
Some Sonoff basics, one for each light switch (https://www.itead.cc/sonoff-wifi-wireless-switch.html)
Raspberry Pi 3 model B+
Micro SD card for the Raspberry PI, 16 GB should be enough
USB to TTL adapter for flashing microcontrollers
At least 5 male-to-female jumper wires for microcontrollers
A few male-to-male jumper wires for microcontrollers
Light switch boxes with additional space
Regular light switches that pass current only when you press them, like the ones for door bells
First, download the software we will be using:
Software to flash the device (https://github.com/letscontrolit/ESPEasy/releases)
Sonoff alternative hardware (https://github.com/arendst/Sonoff-Tasmota/releases). Download sonoff.bin from the newest release and move it to the ESPEasy folder.
Terminal to connect with Sonoff (https://www.compuphase.com/software_termite.htm)
Now you need to properly connect Sonoff to your computer in order to flash it with custom firmware. It’s the most frustrating part, as getting the pins to connect can be tricky. First, put the male-to-female jumper wires on 4 pins on the TTL adapter: 3v3, Rx, Tx and GND. Then plug the male ends of those wires into the holes in the Sonoff, as shown in an image below. The first square hole, closest to the button, must be connected to power with 3 volts (5 volts may destroy the board!). The following holes should be connected to recieve, transmit, and ground. Leave GPIO 14 alone for now, we will use it later.
WARNING: New versions of Sonoff basic boards don’t have GPIO14. Instead, you can use the pin marked as IO2, which can be programmed as GPIO2. The other 4 pins stay the same as on the old one.
My tip to keep the pins connected would be to pull the wires firmly to one side. Also, you absolutely want to connect the adapter to an USB extender wire, and then the extender to the computer, so that the pins stay still when you connect them.
To flash the device, press and hold the black button, connect the USB extender to the computer, and release the button. Open the folder with ESPEasy and run FlashESP8266.exe. Select the COM port of Sonoff and under firmware select the previously downloaded sonoff.bin file. Click flash and pray that it works. If it doesn’t, here are some things to try:
If it’s not working, it’s most likely due to pins disconnecting. Keep them pressed to one side and try again.
Make sure the pins are in correct order.
Some versions of Tasmota work with some versions of ESPEasy. The newest one of both worked for me.
After the flash, be careful not to touch the device! If it successfully flashed, it means that pins are correctly aligned. Now disconnect and reconnect the USB extender (without the button this time). The green LED on the Sonoff should start flashing. Now open Termite, the piece of software we downloaded earlier. It’s a terminal that we can use to give commands to a Sonoff. In Termite’s settings, set the port to the port of the device (same one as in the previous step) and change the baud rate to 115200. After a few seconds you should see some green text, meaning you can access your Sonoff via this terminal. If you don’t see it, don’t worry - sometimes it outputs text only after inputting command. There are two main things to do here - tell the device what wifi network it should connect to and what is the password. You can do it in one move! Just type in “Backlog SSID1 my_wifi_name; Password1 my_wifi_password;“. After some time you will see that your Sonoff connected to your network, along with its IP address! The IP address is, simply put, 4 numbers that let other devices find your Sonoff in your local network. You can type it into the browser and it will show control panel of this device. Good job! You’re halfway there!
While we’re at it, we can change some things using the console. You want the IP addresses of your smart devices to stay constant, so that other devices can find it at all times. The best way to achieve it is to type the following:
ipadress1 192.168.1.50 (change to the IP address that you want your Sonoff to have)
ipadress2 192.168.1.1 (IP address of your router, most of the time it’s this one)
ipadress3 255.255.255.0 (mask of your network, this one should be OK)
restart 1 (restart to apply changes)
After some time you will be able to access the Sonoff under the newly assigned ip address. That means we can mount the devices inside a switch box. First, let’s attach small male-to-male jumper wires to GND and GPIO 14 (as shown in the previous photo). After connecting them the Sonoff will change its state to on or off. My tip is to cover whole device with tape, to make sure that everything stays in place. You may also want to cover the LED to avoid having green a light in your switch.
Remember to turn off the current in your house while you tamper with wires! Put the device inside the light box, connect electric wires as described on the Sonoff box, and turn the power back on. After a minute, you should be able to access the device on your computer by typing its IP address into the browser. Press the “switch” button to marvel as the light in your room changes while you sit on the couch!
While we’re looking at this panel, let’s go to Configuration > Configure Module. Set the GPIO14 to “09 Switch1”. Then go to the main page and select Console. In here, you can type in same commands that we did in Termite, but remotely! For now, write “switchmode1 3” and hit enter. These two changes will make it so that when we connect the two wires going to GND and GPIO 14, the switch will change its state. You can test it yourself. Don’t worry - it’s only 3 volts, so you won’t get electrocuted.
The last thing to do is to cover the box with a switch. Connect the aforementioned wires to opposing sides of the switch and mount it. You now have a working light that you can control both manually and by a computer. Congrats! 🎉🎊
Now the goal is to control multiple switches from one place, accessible both by computer and smartphone. That place will be Home Assistant - an amazing, open source, smart home platform with tons of possibilities and support from the community.
To install Home Assistant on Raspberry Pi follow the official guide: https://www.home-assistant.io/hassio/installation/. It’s very simple, just flash it with Etcher. Etcher is a program that flashes Micro SD cards with operating systems, in our case - Home Assistant. After half an hour, connect it to your router using a network cable and you will be able to access it by typing http://hassio.local:8123 in your browser.
We want Hassio to always have the same IP address, so we need to change it in the router settings. Each router does that differently, but you can usually learn it by typing “<my router company> static ip” into google. Remember this IP, we will use it later.
Now open up your Hassio page in a browser. In the menu, select Hass.io, then Add-on store, and from the list click on Mosquitto broker. Install it, and when it’s done, remember to set a username and password in the config. This will enable your Sonoffs to connect to the home assistant.
The second (and very important) add-on to install is the configurator. With it, we will be able to access configuration files through the website. Remember to give it a username and password as well. After the installation is completed, click “Open web UI”.
You should see a configuration.yaml file. This is the root of all your settings. Here is the code I have there, you can just copy it and paste it at the end of the file:
#Enables access to configurator from menu
# Setup of mosquitto a.k.a. Mqtt broker. Set your username and password here.
# Examples of how you define your sonoffs. In first one, light-l1 is mqtt name for my sonoff, change it to whatever you like.
- platform: mqtt
name: "Living Room main light 1"
- platform: mqtt
name: "Living Room main light 2"
#One line to enable HomeKit automation!
Connecting the two
We’re almost there, I promise! Make sure that you have your switch defined with its unique MQTT name in configuration.yaml file. Go to the Sonoff setup page by typing its IP address into your browser and navigate to MQTT configuration. In the host field, enter the IP address of your Raspberry Pi. Then, type in the user and password you defined before in configuration.yaml. Change the topic to the MQTT ID of your light, in my case that would be “light-l1”. Save, go to Home Assistant, and you should be able to control the light from there!
As a cherry on a top, we can add HomeKit support. Since Apple changed its platform to software authentication, it’s as easy as putting the line “homekit:” somewhere in your configuration.yaml (if you copied my code, it’s the one at the end). Restart Hassio, then open the “Home” app on your iPhone. Click Add Accessory, then select Don't Have a Code or Can't Scan? and choose the Home Assistant Bridge. Confirm that you are adding an Uncertified Accessory by clicking on Add Anyway. Enter the PIN code and navigate further. After the process is complete you will see your switches in Homekit, and you’ll even be able to control them with Siri!
Creating a smart home can be pretty expensive. But with the right tools and some patience you can do it for much cheaper. With this solution we connected 10 lights and 6 roller blinds, with a total cost of around 190$. With finished market products this would cost at least 960$!
If you’re interested in going further with DIY smart home, we highly recommend the DrZzs YouTube channel. If you have any problems, feel free to ask questions in the comments, we'll do our best to solve them. Also, keep in mind that this solution has a big community. Hope this article inspired you to try it yourself!