In this project I will show you how to create a network of Arduinos with RFM69 transceivers to turn on and off lights in your home. This network is made up of a base Arduino with an ESP8266-01 to be able to control your lights through WiFi. The Arduinos connected to the lights will communicate using the RFM69 transceiver to the base Arduino. This project will allow you to switch on and off your overhead lighting through WiFi and still let the physical light switch be usable. If you want, this project can also be adapted to an outlet as well (Just make sure you don't exceed the relay's max switching current). This network is expandable allowing you to control up to 255 different lights around your house from one location.
I decided to use the RFM69HCW over other radio transceivers like the nRF24L01 because of the range and wall penetration. Please not, metal can affect the range of the transceivers.
How does it work?
When everything is setup, the ESP8266 will send a webpage when requested by a browser. This will send the name of the base and load the connected nodes with a toggle button. The base gets the information from the nodes at startup as the nodes will continuously try and send the data to the base every five seconds. When the data is finally sent, it is stored into an array. This array is used later to send the name and id to the webpage. When the toggle button is press, a POST request with the node id is sent to the ESP. The data sent by the request will be used to send a generic character to the node. When the node receives this generic character from the base, it will either switch on or off the lights.
- 2x Arduino UNOs
- 2x RFM69HCW (915MHz [Use if in Americas]; 434MHz [Use if in Europe])
- 3x Logic Level Converter
- 1x ESP2866
- 1x Relay
THIS PROJECT REQUIRES YOU TO WORK WITH MAINS VOLTAGE. WITH IMPROPER SAFETY PRECAUTIONS YOU CAN BE INJURED. PLEASE BE CAREFUL AND DO THIS AT YOUR OWN RISK.
THIS IS FOR EDUCATIONAL PURPOSES ONLY AND SHOULD NOT BE DONE AT HOME IF YOU DON'T KNOW WHAT YOU ARE DOING.
Step 1: Wiring ESP8266 to FTDI Cable
To start off the project, you first need to wire up your ESP8266 to a FTDI cable. I'm using a ESP8266-01 for this project so here is the pinout for that module. (If you are using a different version the wiring may be different)
WARNING: The ESP8266 can only handle 3.3v, make sure that the FTDI cable is 3.3v (Even on the RX and TX!)
Step 2: Check If ESP Has Firmware
Now that your module is wired up to the FTDI cable, its time to check that your module has the correct firmware.
- To do this, you want to open up the Arduino IDE.
- Next you want to select the COM port of your ESP and open up the Serial monitor.
- Now in your Serial monitor you want to select the correct baud rate (Default is usaully 9600, or 115200). And you want to select in the dropdown menu Both NL & CR
- Next you want to type and send AT through the Serial monitor.
If you get OK as a response, you have the correct baud rate and your ESP has the AT firmware (It might not be the latest though).
If you get a response of random characters, you selected the incorrect baud rate.
If you get no response, it is possible that you selected the incorrect baud rate, wired the module incorrectly, or there is no firmware on the module at all.
Step 3: Uploading Firmware to the ESP
The firmware I'm using is from electrodragon which is version 184.108.40.206.
To be able to upload the firmware the ESP, you first need to bring GPIO_0 low by connecting it to ground.
Next, you need to download a tool to upload the firmware to the ESP8266. There are many different tools available, but the one I used can be found here.
Download the zip file and extract the file: esp8266_flasher.exe anywhere on your computer. Open up the program. You'll see a button that says Bin, Download, and a text box that says COM1. You won't need to worry about selecting an address as the firmware we're using goes to the default address.
First select the bin of the firmware you just downloaded, and then change the number of the COM port for your ESP8266 module. After you have all of those set, just press download. That should start the process. If you get an error, check to make sure that GPIO_0 is low and that you have the correct COM port selected.
Once it is done, it might end saying "Failed to leave flash mode." That's fine. Just bring GPIO_0 back to HIGH to use the module.
Step 4: Wiring ESP to an Arduino
WARNING: The ESP8266 is supposed to be rated max 3.3v, but mine can handle 5v. Use a voltage divider or any other method to step the voltage down to 3.3v (Even for the RX and TX lines!!)
Step 5: Picking the Right Relay
Now that you have your ESP wired to your Arduino, you need a relay to actually turn on and off the lights. The reason that I don't have a link for you to buy a relay is because everyone needs a different relay. The relay you need depends on where you live (Countries use 120 ~ 240VAC as mains voltage) and you also need to know how much current your lights take (If you get a 1A relay and your lights take 10A, you probably won't have a house anymore.) It's important to try and add some head space for current. If your relay has a switching voltage of 2A and your lights take 2A, the relay will most likely weld together quicker after sometime.
Please make sure that you get a 5v relay. 5v relays take a 5VDC input (From your arduino) to switch the relay on and off.
For me, since I live in the USA (Mains voltage is 120v), I got a 5v relay with a switch rate of 15A at 125VAC. My lights only use around 500mA.
Step 6: Setting Up the RFM69 Module
Since these devices are 3.3v devices and I'm using 5v Arduinos, a logic level converter is needed to so you don't damage the transceiver. I also bought the boards without the breakout to keep cost down, these boards don't have hole that line up with a bread board. Because of this, I will just be soldering wires directly too it.
To begin, I cut the 22AWG wire for the antenna (ANT) with the designated length below
- 915MHz - 78mm
- 434MHz - 164mm
I left about 3 or so mm to put the wire through the through hole. Try to keep the antenna as straight as possible. This will help maximize the horizontal distance of the signal.
If you would like to help increase the gain of the antenna, you can create a dipole. To make a dipole, just cut another wire the same length as the antenna and connect it to the ground (GND) pin adjacent to the antenna in the opposite direction.
Wiring without Logic Level Converter
Wiring to Logic Level Converter
Step 7: Upload Code
Once you have everything wired up correctly, it's time to upload the code.
For this project you will need the LowPowerLab's RF69 library
Once you've installed the library into the Arduino IDE you can get the code for the project from here
The github will have two files, "smarthome_base" and "smarthome_node." smarthome_base is for the Arduino that has the ESP (In my case the UNO), and the smarthome_node is for the nodes that will be connecting to the base (In my case the Pro Mini).
Open both files and don't upload them yet.
In "smarthome_base", under Network Configuration you wil see ssid, and pass
- Change ssid to the name of your network
- Change pass to the password of your network
Under RFM69 Configuration you will see NET_ID, NODE_ID, FREQ, ENCRYPT, KEY, IS_HCW
- Change NET_ID to any number between 0-255
- Leave NODE_ID to any number between 1-255 (Leave the base at 0)
- Uncomment the FREQ of your RFM transceiver is
- If you want to encrypt the data, uncomment ENCRYPT.
- If ENCRYPT is uncommented, change KEY to any password you want
- If you have a RFM69HCW or RFM69HW uncomment IS_HCW. If not, comment it out
Under Device Configuration you will see name, MAX_NODES (On Base Only), RELAY (On Node Only), SWITCH (On Node Only)
- Change the name of the node to anything you like (Max of only 61 characters)
- Change the name of the base to anything you like
- Change RELAY to the pin number that you are using
- Change SWITCH to the pin number that you are using
- Change MAX_NODES to any number you like (Cannot go over 255)
- I added this as the UNO doesn't have enough memory to store the variables needed for 255 node (Needs ~15,000 bytes for the variables of 255 nodes)
After you've set everything, you can upload the code to the Arduinos
Step 8: Wiring Node to Lights
WARNING: LIGHT SWITCHES CARRY HIGH VOLTAGES. BE CAREFUL.
To start, please be careful. I highly recommend switching off the breaker for the room you're working in, or at least don't have the lights connected to that switch on while doing this.
I first unscrewed the wires on the switch (not the ground pin) and plugged them into the relay (Power In -> COM; Power Out -> NO). Next I screwed in wires to the light switch and plugged them into the Arduino (Replace Power In with Wire connected to Arduino GND; Replace Power Out with Wire connected to D4 pin). I plugged my Arduino into a wall outlet with a USB power adapter.
Since I did everything on a breadboard, I didn't have the space to fit it inside the electrical box, so for the purposes of this Instructables I just taped it next to the switch. I would NOT recommend this if you plan to use this as there will be exposed high voltage wires. If you do put the Arduino inside the electrical box (If you have a metal electrical box remember put something between the Arduino and box so you won't fry the Arduino if the pins touch the box), you can power it with ground wire, and the power in wire (You do need a AC to 5VDC converter or you will fry the Arduino).
Step 9: Usage
Now that you have the node wired up to the lights, just plug in the base and it should connect to the internet and setup itself. The base take around 30 seconds to setup every time it's connected to power. With the base plugged in, you now need to get the IP address. If you have it plugged into a computer, the IP will be printed out into the Serial Monitor. But you can also get the IP by looking at the attached devices on your router. The name should be something along the lines of "ESP_XXXXXX".
Now to turn on and off the lights you want to open your browser and type into the address bar the IP of the ESP. This will load up a page with the name of all the nodes connected and a button. Pressing the button will send data to the node turning on or off the lights and reloading the page. (Note: Loading the page takes around 30 seconds to load if there is only 1 node.)
Thank you for taking the time to read through my first Instructables! I hope I clearly explained everything. If you have any questions people let me know. If any step is confusing, let me know and I'll go back and try to explain it better.