Integrating physical devices with IOTA — Philips Hue edition

The 14th part in a series of beginner tutorials on integrating physical devices with the IOTA protocol

Hugo Gregersen
Coinmonks
Published in
7 min readJan 5, 2020

--

Introduction

This is the 14th part in a series of beginner tutorials where we explore integrating physical devices with the IOTA protocol. In this tutorial we will be revisiting the use-case from the first tutorial in this series, where we built a simple power circuit that would allow us to purchase services from a physical device using IOTA tokens. In this tutorial, we will take on the same idea and apply it on top of an existing ecosystem of hardware and software, namely the Philips Hue.

This will be a two-part tutorial where in the first tutorial we focus on integrating IOTA with the popular Philips Hue. In the second tutorial, we will look at smart home devices in general and how we could integrate IOTA with the opensource ZigBee protocol.

Note!
This is probably the easiest tutorial in this series to complete as it does not involve dealing with any electronics or micro-controllers.

The use case

So, why would our hotel owner choose to implement his IOTA payment system on top of an existing ecosystem like the Philips Hue vs building it from scratch as we did in the first tutorial? Well, the simple answer is “plug and play”. Instead of having to deal with a lot of wiring and microcontrollers, he can now simply go to his nearest electronics store, get all the smart devices (lamps, bulbs, plugs, etc.) he needs and have his new payment system up and running in a couple of hours.

Note!
The Philips Hue system also provides a lot of features that would be very hard to implement from scratch. Such as controlling light colors, light intensity, etc.

What is Philips Hue?

Philips Hue is an ecosystem of color-changing lamps, bulbs, LED stripes, switches, dimmers, motion sensors, smart power plugs etc. that can be controlled wirelessly from an app or the Phillips Hue API. In the center of the ecosystem is the Phillips Hue Bridge. The Hue Bridge functions as a common controller for the entire system. The Hue Bridge can manage up to 50 Hue devices simultaneously in a mesh type wireless network.

You should be able to get a Philips Hue starter kit with a Bridge and a couple of smart light bulbs for less than 100 USD at your nearest electronics store.

Note!
When i started working on this tutorial i did not own a Philips Hue system, so i had to go out and get one. At that point i had no intention of installing it on a permanent basis in my home. However, after getting familiar with the system i was quite impressed by the elegance, simplicity and quality of both the hardware and software. Point being, if you do not already own a Hue system, i can highly recommend getting one, it’s pretty cool.

Installing Hue

First of all, before we can move on you have to install your Philips Hue system according to the documentation that comes with the system. It’s pretty straight forward and should not take more than a few minutes.

After you have installed the Hue app on your IOS or Android device, and verified that everything is working correctly, you need to get the IP address of your Hue Bridge.

To get the IP address of your bridge, open the Philips Hue app and go to: Settings->Hue Bridges, select the (i) icon on your bridge. You will now see some technical details related to your bridge. Make a note of the IP-address as we will need it later on in our Python script(s).

Assigning IOTA addresses to Hue devices

The next thing we need to do is to assign a unique IOTA payment address to each individual Hue device. The simplest way of creating new IOTA addresses (including QR codes) is using the Trinity wallet. Make a note of each address as we will need them in our Python script(s) later on.

Next, print the QR code for each address on a piece of paper and attach it to, or place it next to its respective physical Hue device.

Required Software and libraries

A cool feature that comes with the Philips Hue system is that it has its own application programming interface (API), that allows developers like our selves to interface and build new apps on top of the system. And even better, a team of developers calling them self’s studio imaginare have already made a simple Python wrapper around the API called phue that we will be using in our project.

You will find a github repository with documentation and installation instructions for the phue library here.

The Code

Now that we have made all the preparations, let’s look at the Python code for this project.

The Python script we are using for this project is basically the same as we used for the first tutorial with some minor adjustments. Notice that there are no longer any references to the Raspberry PI GPIO pins or library, which means that we now can run the Python script(s) from any computer inside the local network.

The Python script(s) are basically just checking the balance for each IOTA address (created in a previous step) every 10 sec. As new funds are being added to an address, the script simply turns ON it’s associated Hue device, using a Hue API call. As time passes, the script continually removes time from a local device balance, switching the device OFF (again with an API call) when the balance is empty.

Note!
Notice that in the phue
set_light() function, the first augment is a reference to the id of the hue device being targeted. It is not clear to me how device id’s are managed inside the Hue Bridge/API as i can not find a reference to the this id inside the Hue app, only the name. I can only assume the id must be related to the order in which each device was added to the bridge.

Important!
Before we can have our Python code interact with the Hue Bridge, we first need to peer your computer with the Bridge. You do this by pressing the large button on top of the Bridge followed by running the following Python code on your computer within a few seconds: (This only needs to be done once)

And here is the Python script:

You can download the script from here

Running the project

To run the project, you first need to save the script in the previous section as a text file on your computer.

Notice that Python program files uses the .py extension, so let’s save the file as pay_the_light_hue_dev1.py
(*_dev1.py referring to the particular hue device id being controlled by the script)

Next, we need to make some minor adjustments to the script:

  1. Replace the IP address in the b = Bridge(‘192.168.0.71’) statement with the IP address of your Hue Bridge
  2. Replace the IOTA payment address with the address you created for this particular device id earlier in this tutorial.
  3. Update the device_id variable according to the particular hue device you are targeting (unless the id=1, then no need to change)

To execute the script, simply start a new terminal window, navigate to the folder where you saved pay_the_light_hue_dev1.py and type:

python pay_the_light_hue_dev1.py

You should now see the code being executed in your terminal window, displaying the current light balance for device 1, and checking the devices’s IOTA address balance for new funds every 10 seconds.

Pay the light

To turn on a particular Hue device, simply take your mobile phone with the Trinity wallet, scan the associated QR code for the device and transfer some IOTA’s to its IOTA address. As soon as the transaction is confirmed by the IOTA tangle, the device should turn ON and stay on until the balance is empty, depending on the amount of IOTA’s you transferred. In my example I have set the IOTA/device time ratio to be 1 IOTA for 1 second of service.

Managing multiple Hue devices

If you take a look at the Python script for this tutorial you will notice that the script itself is not prepared for managing multiple Hue devices simultaneously. I guess the appropriate way of dealing with this would be to re-write the code to include some type of device list or array that would allow us to manage multiple devices in parallel. Another alternative is of course (as i did) to simply have multiple instances of Python running at the same time. Each instance running its own script with its own device id/IOTA address.

Feel free to make a pull request in the github repository below if you want to take on the challenge of re-writing the script to support multiple devices in parallel.

Contributions

If you would like to make any contributions to this tutorial you will find a Github repository here

Donations

If you like this tutorial and want me to continue making others, feel free to make a small donation to the IOTA address below.

NYZBHOVSMDWWABXSACAJTTWJOQRPVVAWLBSFQVSJSWWBJJLLSQKNZFC9XCRPQSVFQZPBJCJRANNPVMMEZQJRQSVVGZ

Get Best Software Deals Directly In Your Inbox

--

--