This tutorial will give you some inspiration and ideas on how to switch lights or other devices using a Raspberry Pi and a cheap remote control switch from the hardware store. Of course you can modify the code so that it suits your needs. The components used here cost around 100$ only.
As a basic connectivity we use the MQTT protocol, that is quite famous in IoT for its low footprint. If you don’t know MQTT or want to get more information, check these links:
MQTT, Das M2M und IoT Protokoll
MQTT 101 – How to Get Started with the lightweight IoT Protocol (in English)
As you can see from the figure below, the Raspberry Pi microcontroller hosts two components — a broker and a client. The broker will receive an MQTT message with a topic prefixed by /lights/ from a client, e.g. a mobile app. The sender.py script will communicate with a sender hardware attached to the Raspberry Pi to send the signals to the switches.
We used cheap radio remote control switches by ELRO. The AB440S/3C series come with one sender and three receivers. You can buy this set for 56.72$ on Amazon or for 51.04$ on eBay. The author bought a kit for 10€ only (about 12$) in a hardware store.
To use other controls, modifications to the solution presented here might be needed. If you have used other models successfully, please let us know.
We used the receivers to connect to the lights. The sender of the kit is not used for the final setup. Only if the signal codes provided are not working for your remotes then you need the sender to figure out the codes.
We used a 3rd generation Raspberry Pi as a controller. You can check its hardware characteristics on RaspberryPi.org.
Other microcontroller alternatives such as Arduino, Banana Pi, Odroid-Platine, etc. should also suit.
We need a sender in the 433 MHz band for the microcontroller to communicate with the receiver of the remote control switch set. We will use this sender instead of the default sender of the remote control switch set. Once you have figured out the signal codes, you only need the sender for the final solution. In order to connect sender and receiver units to the microcontroller, please check these links:
The components are:
To get messages from the client to the Python script that triggers the sender component we use an MQTT broker in-between. The MQTT protocol was developed for telecommunications and is now pretty popular for IoT due to its low footprint.
In our solution we use an open-source Mosquitto v.3.1 message broker.
We have three types of clients with different libraries installed.
Raspberry Pi which runs both broker and client in one place uses Paho MQTT Python which is a new version of a previous Mosquitto Python module.
PC runs an MQTT client tool called MQTT.fx for debugging. This tool is helpful when we want to watch the messages and their topics that are published to the Raspberry Pi broker.
Moscapsule framework is installed in the iOS app and represents a Mosquitto library wrapper with most of its functions.
Feel free to use libraries for other platforms as well.
You can find all Python scripts on GitHub.
Script sender.py is triggered from the Raspberry Pi client script at the time it receives a message from the Raspberry Pi broker.
We have written a receiver.py script to determine the look of the signal that the receiver unit gets from the remote control. Once all signals are known, we can use a sender script.
Configure your remote so that the lights react to all buttons of the remote control.
We used the channel code you can see on picture below. For that you need to set one or more dipswitches 1 till 5 under the cover. In the instruction manual of the controls you will find more information on how to set the code. If you use the same channel code, this solution here might work out of the box.
Install Mosquitto broker on your Raspberry Pi. Installation instructions can be found here:
For each of the clients in section "Software Components. MQTT Clients" refer to the corresponding source links of their libraries:
All scripts mentioned in section "Software Components. Python Scripts" are available on GitHub.
The receivers can only be triggered by the right binary signal. For the ELRO remotes, the right signal configuration is already hardcoded into the Python scripts. If you are using some other brand please continue with the following paragraph. Otherwise skip to the next section.
The most challenging yet interesting part is signal determination. We did it in two steps. First, we wrote a script for receiver (Raspberry Pi microcontroller) that gets signal codes from the radio remotes when some button is pressed. After getting the feel about how much time the code delivery took and how many iterations did the signal perform, we wrote a sender program. The sender script is launched in final solution from the lights script which configures the client. Commented source code for all three scripts you will find on GitHub.
You can use anything that can send MQTT message with a specific topic as a remote control, e.g. apps, PC tools and scripts. The most straightforward approach is to use a simple MQTT client tool on PC called MQTT.fx (download link). There are other alternatives available, which are listed in this article by HiveMQ team.
We have also written an iOS app as shown below and used it as a remote control. Please find the source code on GitHub.
Make lights.py Python script run on start up on your Raspberry Pi. Use your preferred remote control to manipulate the lights. We hope you will enjoy!