Introduction: Raspberry Pi RF Remote-controlled Mains Sockets (Power Plugs)
Control cheap 433MHz mains sockets (wall outlets) using a Raspberry Pi. The Pi can learn the control codes output from the sockets' remote controller and use them under programme control to activate any or all remote sockets throughout the house.
The design doesn't rely on external internet connectivity (ie) 'Internet of Things' and is therefore (IMHO) much more secure than web-based controllers. That said, I did try integration with Google Home but quickly lost the will to live when commands sometimes took several tens of seconds to execute or never executed at all.
An obvious application around Christmas time is controlling Christmas tree lights and (if you're that way inclined) outside display lights. Although that's a simple use, by building this Instructable you'll end up with a super flexible sockets controller which can respond to sensor inputs and to other devices on your home network, such as Raspberry Pis running Linux Motion.
For example, I've a set of kitchen lights which come on when a camera running 'Motion' detects movement in the kitchen and then switches them off after five minutes of no activity. It works really well!
With 'Tasker' and 'AutoTools SSH' from the Google Play store, you can set up all sorts of fancy phone-based remote controls.
The project relies on cheap 433MHz receiver and transmitter boards widely available on eBay. These are compatible with (in the UK at least) 433MHz remote mains sockets sold with remote controls. My project includes a receiver so new remote control command sets can be incorporated easily and quickly. One point to note - remote sockets available in the UK seem to come in two flavours - those with an ID programmed by a switch on the socket and those reliant on programming from the remote controller. This project is compatible with both but the former don't lose their identity in a power cut and are therefore preferable.
The project uses an old router case - I have a few of these and they very handily have most of the necessary external connectors, such as power, ethernet, USB and antenna(s). What you use will depend upon what you have available so this Instructable is probably more useful as a general guide rather than a step-by-step set of instructions.
Although not strictly necessary for this project, I've also added a cooling fan and controller board. Without a fan, the Pi can get quite warm (circa 60°C). Details may be provided in a later Instructable.
I ought to mention that I'm no programmer. The software is (mostly) written in Python and the clever stuff is copied from people who know what they're doing. I've acknowledged the sources where I can - if I've missed any, please let me know and I'll correct the text.
The Instructable assumes some soldering ability and a passing familiarity with Python, Bash and talking to your Pi via SSH (although I'll try to make the instructions as comprehensive as practicable). It is also written in British English, so if you're reading on the other side of the pond, please ignore the extra letters in words and the odd names for things (such as 'mains sockets', which you will know as something like 'wall outlets').
Any comments, suggested improvements & uses etc. are also very welcome!
Step 1: Case Preparation
I used an old TP-Link TD-W8960N router for this project. It's a nice size and once I'd worked out how to get in it, pretty easy to work on.
I also retained the router's 12v @ 1A power supply, which is a little under powered but in practice is OK for this application.
Opening the case is a matter of removing two screws in the bottom of the case and then using a prying tool around the edge of the case to ease the clips open. The two screws are under the rubber feet at the rear of the case (see red arrows). The most difficult clips to open are those at the front but I had faith and they bent to my pry tool.
Once the case is open, undo the two nuts on the antenna connectors and the circuit board can be lifted out.
As you'll be using both antennas later, unsolder the coax leads at the circuit board and put them to one side.
If you're feeling brave (as I was), you can remove the push switch, dc socket and RJ45 sockets from the circuit board. The best way I've found to do this is to clamp the board in a vice and apply heat from a heat gun whilst prising with a suitable thin case opening tool or screwdriver. The logic is that all the solder connections are melted at the same time, reducing the overall heat stress on the component's plastic case compared to using a soldering iron on each junction. That's the theory at least. In practice, some luck is involved! How much heat to apply is a matter of judgement but be careful and err on the side of too little. If all goes well, you'll end up with the usable components shown in the photo (however you'll note the melted switch knob and the slightly deformed RJ45 socket strip!).
Otherwise, it's off to the internet to buy your bits.
Step 2: Parts List
- Raspberry Pi - I suspect any flavour will do but I used a 3B+
- 433MHz transmitter board - search eBay for '433MHz RF Transmitter with Receiver Kit for Arduino Arm Mcu Wireless' or similar.
- 433MHz receiver board - ditto. Typically £1.98 per pair
- LM2596 Buck regulator - eBay, typically £1.95. To convert 12v power to 5v for the Pi.
- Light pipe - search eBay for 'Fibre Optic Cable - 0.25 / 0.5 / 0.75 / 1 / 1.5 / 2 / 2.5 /3mm Dia - Light Guide' - I used 2mm pipe but 1.5mm would have been easier to work with (I paid £2.95 for 1m).
- 2 pole miniature toggle switch (nice to have but optional)
- USB type A 180° solderable socket - via eBay, I paid £1.90 for ten.
- Dual pole push switch (nice to have but optional) - I got mine from the modem/router board.
- RJ45 socket(s) - recovered from modem/router board.
- DC power socket - via eBay (10X DC Power Supply Jack Socket Female Panel Mount Connector 5.5 x 2.1mm £0.99)
- 430MHz antennas - convert the modem/router's 2GHz antennas.
- 12v dc 12W power supply (minimum) - ideally, this will come with the modem/router. If not you need to make sure the dc power socket above matches one you use. The 12v requirement is determined by the 433MHz transmitter.
The parts for the cooling fan mod will be detailed in a later Instructable.
Step 3: Consumables and Tools
You'll need the following consumables:
Solder (as required)
Hot melt glue (as required)
Interconnect wire - (eg) 22 & 24AWG (as required)
Heat shrink sleeve (as required)
Sacrificial Cat. 5 ethernet patch cable
Sacrificial USB 2 patch cable.
Wire cutters (preferably flush cutters)
Suitable screwdriver to take the case apart.
Hair dryer (to bend the light pipes and for any impromptu hairdressing interruptions)
433MHz FM communications receiver (optional - for troubleshooting transmitter problems) - (eg) AR1000
Step 4: Assembly
How you assemble the Pi and ancillary boards depends upon the case you're using. The photos show what I did.
The Pi sits roughly in the middle of the case, allowing enough room for the various connectors to be used (note that the HDMI isn't used as the Pi is communicated with via SSH (ie) 'headless'.
I attached the Pi to the base using a couple of salvaged plastic fasteners (see photo). As the box isn't intended for portable use, you can get away with only using two fasteners. You could easily use 2.5mm screws with stand-offs or even hot-melt glue (which I've used in the past - just be sure not to use too much and avoid any surface mount components on the underside as you'll inevitably have to remove the board at some time (first law of construction - you WILL have to take it apart)).
I used hot glue to fix the various boards to the sides of the case. The same considerations as above apply.
Once everything's in place you can wire things up.
The block diagram shows the wiring scheme I used. Note that I use the optional toggle switch to alternate power between the transmitter and receiver boards - there's probably little risk of doing so but I didn't want to fry the receiver when transmitting.
It also occurred to me that the push switch could have been used to gracefully power down the Pi (there are a number of designs available on the internet). I didn't bother - in this case it acts as a simple power on/off switch. I just have to be careful to shut the Pi down via SSH before pressing the switch.
You'll note the light pipes used to channel the light from the two LEDs on the Pi and from the power supply status LED to the front of the case. I used heat from a hair dryer to bend the pipes (you definitely DON'T want to use a heat gun!). It's very trial and error but worthwhile in the end as you can see directly what the LEDs are signalling rather than relying of software and external LEDs. It's your choice of course. Cutting the pipes is done with a sharp pair of wire cutters (flush cutters are best) but you can also use sharp scissors. Again, hot-melt glue can be used to fix the pipes in place but be careful to only use a small amount - which cools quickly - as the glue can distort the pipes.
Ideally you should modify the antennas. They will typically be sized to operate at 2GHz and will make very inefficient antennas when used at 433MHz.
To do this, you should first remove the antenna cover to expose the antenna wire. I think I was lucky as the cover came off each antenna with only a small amount of prising.
Cut where shown to remove the original 2GHz antenna and expose the co-ax. Carefully access the inner core, removing the braid well away and solder it to a new piece of wire as shown. The length of the new wire is roughly a 1/4 wavelength of 433MHz (ie) length = 0.25 * 3E8/433E6 = 17cm. The lower part can be coiled using a small drill bit or similar to permit the whole length to fit in the antenna cover.
Before reassembly, check there isn't a short circuit between the inner and outer antenna contacts.
I only modded the transmitter antenna as a 'deaf' receiver is probably advantageous when learning the RF remote control codes (see later).
The ethernet connection is made by wiring a sacrificial Cat. 5 interconnect cable to the RJ45 socket rescued from the modem. Cut the cable to suit the distance between the Pi ethernet socket and the RJ45 case socket and bare all eight wires. Use a continuity tester to ensure you wire cable pin 1 to socket pin 1 etc. A simple way to do this is to plug the connector into the socket you're wiring to and ring between the socket contacts and the bare cable ends. As only one of the four external RJ45 sockets is used, mark the wired socket accordingly to avoid embarrassing errors later.
Likewise, the USB connector is wired using a sacrificial USB 2 patch cable, wired pin 1 to pin 1 etc. The outside world USB connector is hot glued into place on the case, using the hole in the case left by the telephone line socket.
Step 5: Transmitter Notes
The 433MHz transmit and receive boards I used are ubiquitous on the internet and as they are so cheap I ordered two pairs of each (to allow for experimental cock-ups). I found that the receivers to be reliable but the transmitter I used needed modifying to make it work reliably.
The circuit of the FS1000A transmitter I bought* is shown in the diagram. I found by trial and error that a 3pF capacitor needed installing in the C1 SoT (select on test) position to get the thing to work. As I have a wideband receiver which covers 430MHz it was relatively easy to troubleshoot this. How you might test without a receiver is an interesting question....
*Note: I bought a second lot of transmitters after I couldn't get the first two working. These all came missing the collector coil. Hmmm!
I had a 3pF capacitor in my junk box but this won't be the case for most people I guess and in any case, the value needed might be more, say 7pF. A crude replacement can be made with two bits of twisted wire (twisted pair cable of my acquaintance has a capacitance of around 100pF per foot to give you a guide to length) but it's not recommended as other issues can arise. Hopefully you'll be lucky and you'll have no such problem. You could always buy a more expensive (and therefore probably) better-made transmitter.
Note also the transmitter's frequency is not very precise or stable but in practice has been good enough to reliably operate the remote sockets.
Please also note that the plated-through hole adjacent to the word 'ANT' on the transmitter is NOT the antenna connection - it's the one in the corner with no marking (see photo). This was the first mistake I made....
The pin connection helpfully marked 'ATAD' should actually read 'DATA' of course.
Step 6: Software Overview
Please bear in mind I am no programmer. As stated previously, the clever stuff is other people's code but I do know enough to pinch it and adapt it to make it work together. This is also the first Instructable I've published with code in, so apologies if I've done it wrong! If you have any questions, please bear that in mind...
The basic software I used is as follows:
- Raspbian Stretch Lite
- PiGPIO (a fantastic library for driving servos etc.)
- _433.py code (to encode and decode RF control codes) - linked to from the PiGPIO website.
- Python3 (comes with Raspbian)
Additional software which I use:
- pyephem (calculates dawn and dusk times - useful for light switching)
- The excellent 'Tasker' and 'AutoTools SSH' to create a remote control on my Android phone - see photo (both available in the Google Play store). [How to create a Tasker 'scene' is outside the scope of this Instructable as there's a fairly steep learning curve involved but I'm happy to discuss what I did]
My own code (in Python). Crude but functional:
- tx.py - menu and/or command line argument software which sends the appropriate code to the 433MHz transmitter.
- dawn-dusk - calculates dawn & dusk times at my location and updates the user crontab (used for Christmas tree lights etc.)
The above personal code can be accessed via GitHub: https://github.com/nightcustard/RFcontrol.git
The functionality of the project is provided by the PiGPIO and _433.py code. The latter has a receive function which listens for remote control commands from your 433MHz RF remote control and decodes the timing pulses, producing an output which can be stored for later use by the transmit function. This permits the system to learn any 'normal' 433MHz RF remote control. In principle it can also be used to learn your neighbour's RF remote controls also. I would advise strongly against this as neighbours rarely see the funny side of randomly ringing doorbells. I wouldn't.
As the Pi in this application is run 'headless' (ie) without a monitor or keyboard, you need to talk to it via ssh. There are plenty of guides available covering how to set up a Pi headless but to keep things simple, I'll assume you first start the Pi with a monitor & keyboard. Once booted, start the terminal and enter 'sudo raspi-config'. Select '5. Interfacing options' and then 'P2 SSH'. Enable the ssh server and close raspi-config (which will probably end in a reboot).
Subsequent comms with the Pi can then be conducted from a remote terminal via ssh. Note that the code doesn't necessitate a fixed LAN IP address for the Pi but it certainly helps (and it's certainly necessary if you delve into Tasker control). Again, there are plenty of tutorials on line covering how to do this. My home router allows me to assign a fixed IP address to the Pi's MAC address, so I do it that way, rather than by editing the Pi's setup.
ssh into the Pi and enter the following commands:
sudo apt update
sudo apt install pigpio python-pigpio python3-pigpio
sudo apt install git
git clone https://github.com/joan2937/pigpio
sudo apt install python3-RPi.GPIO
To run PiGPIO on boot:
add the following line:
Get the Python code for transmitting and decoding 433MHz RF remote codes:
Move the unzipped _433.py to a suitable directory (eg) ~/software/apps
Typing (in that directory)
places the Pi into 433 rx mode, waiting for demodulated RF remote control codes on GPIO pin 38.
With the 433MHz receiver connected, when a 433MHz remote control is used nearby, something like the following data will be seen on the screen:
code=5330005 bits=24 (gap=12780 t0=422 t1=1236)
This data is used in your Python programme to regenerate the transmission from the remote control.
To pipe this data to a file for later use, run:
_433.py > ~/software/apps/remotedata.txt
Once you've got the data, the next step is to use it to edit the 'tx.py' code you can copy from my GitHub repository. This code uses the data to generate waveforms understood by the remote socket(s) to be transmitted by the 433MHz transmitter. Hopefully the required edits will be reasonably obvious and the rest is up to you.....