Pi Mi-Light

By Russell Barnes. Posted

Solve real-world electronic and engineering problems with your Raspberry Pi and the help of renowned technology hacker and author, Simon Monk

Mi-Light light bulbs look just like normal LED light bulbs and are available at a similar price, but they include a 2.4GHz RF radio link. This can be used with an RF remote control to switch lights on and off, with the lights grouped into four zones. The remote control also allows you to vary the brightness and colour of the light. Mi-Light produces a ready-made module that allows you to link the lamps to your WiFi router and then control the lighting with a smartphone app. However, by using a £3 ($5) radio module connected to a Raspberry Pi, you can let your Raspberry Pi take control of Mi‑Light bulbs in your home, opening up all sorts of possibilities for home automation.

One possible use is to have the Raspberry Pi operate as a web server, providing you with a web interface that will let you turn lights on and off from the browser of any device connected to your network.

You'll need

NRF24L01 2.4GHz wireless radio transceiver module (find one on eBay)
Mi-Light RGB LED light bulb
Mi-Light 2.4GHz remote control
7 × female-to-female jumper wires

This project is based on an original blog post by Torsten Tränkner, which is in German. The project has only been tested on a Raspberry Pi 2, and may need some adaptation for other models of Pi.

As you’ll see from the list of required components, this project does not involve any soldering. The RF module is connected to the Raspberry Pi using female-to-female jumper wires. Even though the Raspberry Pi will eventually take over the operation of the lights, you do need a Mi-Light remote control to set things up in the first place.

Build the Pi Mi-Light

Start by connecting jumper wires to the RF module using the wiring diagram as a reference. You will need connections to every pin except the pin in the bottom right of the connector (pin 8). Using leads of the same colour as the wiring diagram will help you make the correct connections.

 Attach leads to the RF module

Connect the leads from the RF module to the GPIO header as follows:
Pin 1 (GND) of the RF module to GND on the GPIO header.
Pin 2 (VCC) of the RF module to 3.3V on the GPIO header.
Pin 3 (CE) of the RF module to GPIO 25.
Pin 4 (CSN) of the RF module to GPIO 8.
Pin 5 (CLK) of the RF module to SCLK (GPIO 11) on the GPIO header.
Pin 6 (MOSI) of the RF module to MOSI (GPIO 10) on the GPIO header.
Pin 7 (MISO) of the RF module to MISO (GPIO 9) on the GPIO header.

 Connect the RF module

Finally you can pair the Mi-Light. First, you will need to plug your light bulb into an appropriate light socket. Then turn on the normal light switch and immediately press the On button for one of the four light zones on the Mi-Light remote. The lamp will blink three times as confirmation. Once paired, the remote will be able to turn the light on and off and make other adjustments.

Get the software

This project requires quite a lot of software setup before you can go ahead and use it. The radio module uses the Raspberry Pi’s SPI interface, which needs to be enabled, so run raspi-config using the command:

sudo raspi-config

Scroll down to the Advanced option, select it, and then select SPI. Enable it, and when asked if you want the SPI module to load automatically, say Yes.

Next, you need to install and build the NRF24 library for the RF module by issuing the following commands:

git clone https://github.com/TMRh20/RF24
cd RF24
make all
sudo make install
After that, you need to download a messaging library that provides a higher-level interface to the basic RF module using the following commands:
cd ..
git clone https://github.com/mysensors/Raspberry
cd Raspberry 
make
sudo make install
Now download the code that Torsten Tränkner wrote using the following commands:
cd ..
wget http://torsten-traenkner.de/wissen/smarthome/openmilight_raspberry_pi.tgz
tar xzvf openmilight_raspberry_pi.tgz
This code is all in C++ and so that we can make use of it in Python, we've has written a C++ program called send_cmd, which is designed to be called from Python with a command message to be sent to the Mi‑Light. Download this program and the Python code for the project from your Pi’s command line, using:
git clone https://github.com/simonmonk/pi_magazine.git
Copy all the files in the project folder 11_mi_light into the openmilight folder, and then compile two of the programs by using the commands:
cp pi_magazine/11_mi_light/* openmilight
cd openmilight
sh ./comp.sh
You also need to install the Python Bottle library that will be used for the web server, using the commands:
sudo apt-get install python-pip
sudo pip install bottle

Before you can run the main program, you need to find the ID of the Mi-Light remote that you used to pair with the Mi-Light so that your Raspberry Pi can impersonate it.

Run the program openmilight using the command below, and then press the On button for Zone 1 on the remote a couple of times. Each time you press the button, you should see a stream of hexadecimal number like the ones in the example below.

sudo ./openmilight 
in listening mode
B0 A1 56 41 C1 03 72 .
B0 A1 56 41 C1 03 73 .

The first three digits (in this case B0, A1, and 56) will be different for your remote, so quit the program using CTRL+C and edit the file lights.py, changing the value of the ID variable at the top of the file to be the three hex digits for your remote.

Finally, you are ready to fire up the web server using the following command:

sudo python lights.py

Point a convenient web browser at the IP address of your Raspberry Pi and you will see a webpage with On and Off buttons for all four zones. Click the buttons for Zone 1 and you should be able to turn all the lights connected to that zone on and off.

How the code works

You will probably find it handy to have the code up in an editor while we go through it.

The program starts by importing the bottle and os libraries. The os library is needed to invoke the
send_cmd C program from Python.

The constant ID needs to be set to the three hex digits for your remote, as described earlier. The codes in MIDDLE do not need to change. Note that the spaces in both these constants must be kept as they are, so that when the hex message is constructed, there are spaces between each hex digit.

The Mi-Light remote control allows up to four zones to be defined, and you can attach multiple lights to each zone. There are separate On and Off codes that control each of the zones independently. These are stored in the variable ZONES, which is a dictionary or dictionaries, making it easy to look up the appropriate hex code for the command you want.

Two functions (lighton and lightoff) switch all the lights on or off for the zone name specified as their parameter. Both these functions use the send function to actually send the command to the RF module via the C program (send_cmd). The send function first constructs a message string by concatenating the ID, MIDDLE section, command code, and finally 00 into a message string. The C program is then called five times, passing the message as a parameter. Since it can be a little unreliable, sending the message five times makes it almost certain to get through to the light bulb.

Next, there is the web server part of the code, contained in the index function. This expects the webpage to provide two request parameters: the zone name (zone) and whether it is to be turned on or off (state). This information is then used to call either lighton or lightoff. At the end of the index function, the contents of the template home.tpl are returned, to provide the browser with the HTML for the web interface.

The final line of code starts the web server running. Switch over to a browser on the Raspberry Pi, or another computer on your network, and type the IP address of your Pi into the address bar. To discover the IP address of your Raspberry Pi, type the command below into the terminal:

hostname -I

It will be at the start of the response as four numbers separated by dots; for example, 192.168.1.22.

 The final product, ready to be controlled remotely

Using your PiMi-Light Controller

As well as controlling the lights from the browser on your computer, you can just as easily use the browser on your smartphone, as long as it is connected to the same network as your Raspberry Pi.

You could also adapt the alarm clock program back in issue 33 of The MagPi to turn the lights on and off at certain times.

This project simply turns the lighting on and off. Considerable work has been done in reverse-engineering the Mi-Light protocol to figure out what all the codes do. So, if you want to extend this project to adjust the colour and brightness of the lights, then you might want to look at Henryk Plötz’s work.

Code listing

bottle import route, run, template, request
import os

ID = 'B0 A1 56'
MIDDLE = ' 06 C9 '

ZONES = {
    'zone1': {'on': '03', 'off': '04'},
    'zone2': {'on': '05', 'off': '06'},
    'zone3': {'on': '07', 'off': '08'},
    'zone4': {'on': '09', 'off': '0A'}}

def light_on(zone):
    print(zone + "on")
    send(ZONES[zone]['on'])


def light_off(zone):
    print(zone + "off")
    send(ZONES[zone]['off'])

def send(code):
    global ID, MIDDLE
    for i in range(1, 5):
        message = ID + MIDDLE + code + ' 00'
        os.system('./send_cmd "' + message + '"')

# Handler for the home page
@route('/')
def index():
    zone = request.GET.get('zone', 'zone1')
    state = request.GET.get('state', 'off')
    if state == 'on':
        light_on(zone)
    else:
        light_off(zone)
    return template('home.tpl')

# Start the webserver running on port 80
run(host="0.0.0.0", port=80)

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.