My newest hobby is mucking around with the Internet of Things (IoT). A while back I bought several NodeMCU’s from eBay for almost nothing. Along with a relay board (also from eBay), some creative wiring, and a bit of software, I’ve managed to put together a neato remote controlled relay board. In this demo I’ll actually use two NodeMCU’s, one to handle capturing some data from some sensors, and another as part of the relay build.
If you follow this tutorial in entirety, you’ll get a completely functional system where you can remotely turn outlets on/off, and add rules and logic to your setup to do have the outlets turn on and off under certain situations. We’ll be setting our system up to turn on an outlet when our sensor node detects humidity over a certain level.
First of all, here’s what you’ll need for the relay system:
- (1) NodeMCU or other comparable ESP8266 controller. You can source these from China for about $4/board – check out eBay. You could also use an Arduino or other microcontroller, but then you’ll be on your own for handing programming/implementing the controller.
- (1) 4-channel relay board – also easily sourced on eBay. You can get one with more/less channels, depending on what you need.
Note: see user comments below about running a 5v trigger relay on a 3.3v board. I’ve updated the link here to point to a 3v trigger board – full disclosure, I haven’t used that particular board, but it should be a better fit for the NodeMCU which is a 3.3v board.
- (3) 2-gang, plastic electrical boxes, similar to this. Two with outlet covers, one with blank cover. These should have connections on two sides. See note.
- (1) 1-gang, plastic electrical box with blank cover. You only need one side with a connection on this box. See note.
- (4) Electrical outlets. I used high-amp outlets.
- A small USB wall adapter and cable.
- A clamp fitting, or other appropriate fitting, that will secure the extension cable to the last opening of your box chain.
- A heavy-duty, 3-prong extension cable that you are willing to sacrifice. I recommend buying/sourcing one much larger than you need, so you can some lengths of it to use as jumper wires within the enclosure. Otherwise, you’ll also need some electrical wire of the same gauge as your extension cord to make jumper wires.
- Misc. bits and bobs, jumper wires, wire nuts, electrical tape, etc.
If you want to also build the sensor board, an easy way to get started would be:
- Another NodeMCU
- A DHT22 Temperature/Humidity Sensor – also on eBay.
I found that if I bought boxes with 3/4″ openings and 1/2″ openings, they would interlock without having to buy couplings and such. It’s a tight fit, and I didn’t end up using any glue because I plan on screwing these to a wooden board when I am finished. You are free to package all of this up any way you want. In fact, I’ll take this moment to note – I am not an electrician, I give no warranty to the safety of this setup – build this at your own risk! If in doubt, consult an electrician to help you!
Further Safety Note
This tutorial assumes you have some basic knowledge of electrical wiring. If you’ve never wired in an outlet our switch in your own home, you might want to go learn up a bit before you attempt this tutorial. Be sure you follow proper safety procedures – use three-prong outlets, use a three-prong, heavy-duty extension cable, and connect to a properly grounded three-prong outlet when in use. If there is even the slightest chance that this device or anything connected to it could come into contact with water, you should install and connect this to a GFCI outlet for protection.
Part I – Let’s Get Started! Build out the Main Hardware
- Gather all your parts. You can build it the way I suggest, or figure out your own enclose. I opted for the multiple outlet boxes because they fit together nicely and were cheaper than a larger enclose that would fit everything, but that I would have to hack at in order to utilize.
- You may need to prep the box that will contain your relay. The box I bought had two sets of screw posts in it, one set for where the outlets would go, and another set that is used for screwing on the cover. My relay board was too big to fit into the box without removing the screw posts for the outlets. This involved cutting at them with a knife. It took a while, but once they were out, the relay board fit in snugly.
- On the single-gang box, you’ll want to cut a small opening in one of the walls of the box so you run the USB cord for the NodeMCU through it (see below).
- Assemble your boxes as shown here, with the three 2-gang boxes connected together and the 1-gang box on the end. If you look at the relay box (second from left) compared to the box directly to the right, you’ll see the missing screw posts that were cut out to fit the relay board.
- Take your heavy duty extension cord, and cut the outlet end off of it, we won’t need it!
- If you are using the extension core as a source for your jumper wires, cut a few feet off and peel the outside insulation off, freeing up the three wires inside.
- Now take the raw end of your extension cord and cut back about 3″ of the outside insulation, freeing up the inner cables.
- If you’re using a clamp fitting or other fitting to secure the extension cable coming out of the box, you probably need to install it before you run the wire into the box.
- Run the raw end of the extension cable into the end box, run it through the first box into the second box, and pull some slack so it’s accessible.
- Strip 3/4″ of insulation from each wire.
- Determine how you want to control your outlets. I recommend you have at least one as an “always on” outlet, particularly so you have a place to plug in your USB power adapter that will power your NodeMCU. For my layout, the top-right outlet is always on, the left outlet is entirely controlled by one relay, the bottom-left outlet is split, with each socket controlled by independent relays, and the bottom-right outlet is controlled by the fourth relay.
- Now you need to make your jumper wires. The easy ones are the negative and ground wires. You simply need to create one negative and ground jumper for each outlet, long enough to run from the middle box to the correct screw post on your electrical outlet. Strip 1/2″ off each end of your jumper wires, connect them to your outlets, and then connect them all together with the appropriate wires coming out of your extension cord. Be sure your wire nuts are big enough to accommodate all the wires, you’ll have 5-6 wires coming together for each of your positive, negative, and ground wires. It may also make it easier to use electrical tape to hold all the wires together before you put on the wire nut.
- The positive/hot wires are a bit more complicated. In my setup, I have 5 separate outlets that will get controlled. One always-on, and 4 ran through the relays. To do this, you will need to split the positive into 5 wires. You’ll have one wire that comes from the connection with the extension cable down to the relay board. You’ll then have another (longer) jumper cable to run from the relay board up to the corresponding outlet. Be sure you cut all these jumpers with enough slack to be able to work with them and route them easily.
- Get all your relay jumpers together and connect them to the hot cable coming out of the extension cord. Run each of these down to the relay board and screw one into each set of terminals for each relay. Each relay has two terminals, when the relay is on, the connection between the two terminals is made and the current can flow.
- Now connect your jumpers from the relay board up to the outlets. If you’re going to have two individual plugs on one outlet controlled independently, be sure to follow the instructions for separating the connection between the two plugs, and then you can wire them individually. The end result will look something like this:
- Now you need to connect your relay board up to your NodeMCU. My particular relay board has screw terminals for the data connection. I had some Male/Female ribbon jumper wires hanging around, which worked perfectly for this. You need (6) total wires: VCC, GND, R1, R2, R3, R4.
- Connect the male ends of your ribbon cable to the posts of your relay board, taking note of the order and corresponding colors of your cables.
- Now connect the other end of the ribbon cable to your NodeMCU. Connect to the VCC and GND to any free corresponding 3V and GND pin.
- Now connect the remaining 4 cables to digital pins on your NodeMCU. I connected mine to D0 through D3.
- Before you close everything up, it’s a good idea to test your connections first. At this point you should be able to safely plug the extension cord into the wall and do some testing. If you have an always-on outlet, that outlet should be working. If you’ve got an outlet tester, plug it in and see if your wiring shows up as OK. We’ll test the other plugs once we get our NodeMCU programmed and fired up.
- The hardware is essentially complete! Now let’s do some programming. Check out part II of this tutorial to keep going.