1 year ago

Pi Mi-Light

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

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

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:

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:

After that, you need to download a messaging library that provides a higher-level interface to the basic RF module using the following commands:

Now download the code that Torsten Tränkner wrote using the following commands:

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:

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:

You also need to install the Python Bottle library that will be used for the web server, using the commands:

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.

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:

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 (light_on and light_off) 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 light_on or light_off. 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:

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

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

  • Jamilio Rasidi

    Is it possible to make this work with the new milight controllers ?

  • The RF24 software is asking for a username and password!?

  • I just noticed that you do not have Pin #8 on the NRF24L01 connected.
    It’s the IRQ Pin, and should be tied to GPIO23.

  • Is the run(host=”0.0.0.0″ correct, or should this be set to the the IP of the Pi?

  • Al Roberts

    thank you works great except when i disconnect the ssh it stops working how to fix

  • disqus_FaMjhT1DL2

    Can’t seem to get this running on a Pi Zero. It just exits without saying a word. Anyone else been able to get it going?

  • Leon Cheung

    thanks except is there a different interface where i can turn up the brightness and change the colour of my mi light

  • Leon Cheung

    yes it is because it is a 2.4ghz frequency and thats universal

  • Laurent

    On Raspberry 3, I get :

    $ sudo ./openmilight
    in listening mode
    bcm2835_init: gpio mmap failed: Cannot allocate memory

  • Koenraad de Haas

    did you get in running on the raspberry 3, could you share the code?

  • haaaas

    did you get in running on the raspberry 3, could you share the code?

  • Nicola Mosca

    Hi,

    at the end of this tutorial I have the same problem with all buttons (on off for all 4 zones).

    Indice di file:///?zone=zone1&state=on
    and a list of my folders (working on raspberry with jessie lite)

    Can you help me to understeand what happen?

    Thanks

    Nicola

  • Laurent

    No way, I started again from scratch today and same error

  • haaaas

    I just received the rf module, will try and let you know!

  • Nicola Mosca

    I finally solved all small issues and now I can play with on/off on the 4 zones.
    I would like to share a command that I added on the file home.tpl
    At the top os the html page I insert this line:

    This line permit that everytime that you sent a message to the lights automatically set the width on your devices..
    for me is better now 😀

    Nicola

  • Nicola Mosca

    I found the same problem. I’m not sure but I think that the cause was a “bad connection” of jumpers. I did it with watching the RF module reverse…
    Try it and I hope to help you.

    Nicola

  • Roberto

    I have the same problem here, any news ?

  • Kasper laursen

    Is it possible to use the method to add more than 4 groups?

  • Petter Miltén

    Hi, after downloading the openmilight_rasp……. file and trying to use the next command on it I get the message that it’s not “zip-able”(can’t remember the entire message atm). And when checking what file type it is it says that it’s a html file. Is that correct? Any ideas what might have went wrong?

  • Petter Miltén

    I too have the same issue, any of you solved it?

  • Stig Nørgaard Færch

    I have a Raspberry Pi B+ lying around. Can I use it?

  • Petter Miltén

    May have solved it, stupid error for me. The picture above with the rf module is “upside down”… The cords seems to be going towards you but are actually going away from you, look for the metal part.

  • Petter Miltén

    But still no reply in listening mode… only blank

  • Petter Miltén

    I can’t “hear” anything in listening mode. Tried double sets of both modules and wires. Have a RF remote for w/w bulbs, only on/off 4 zones and a wheel to change temperature or strength.

    Anyone with any other ideas?

  • Stijn Ai Ghesquiere

    Can you pair your lights with your NRF24L01+raspberry pi?

  • Stijn Ai Ghesquiere

    Yes and no. The protocol allows only 4 groups. But you can assign the lights to other remote id’s (65536 different id’s possible).

  • Stijn Ai Ghesquiere

    run with ‘&’ at end to run it in background. Or use tmux. Or start it, press ctr-z and tupe bg to place it in background.

  • Petter Miltén

    By holding the button down in the web browser you mean? Might be possible!

  • raspberry pi zer0

    can u use a pi zer0 instead of a pi 2

  • Martijn Janssen

    I connected the RF module correct;y, I was able to test it with another program, which returned an error when I disconnected one of the leads. But When I turn on listening mode, nothing is displayed, whatever I do.

    What can be the cause of this?

    Regards,

    Martijn