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