Integrating physical devices with IOTA
The full article was originally published by Hugo Gregersen on Medium. Read the full article here.
This beginner’s tutorial is a simple, yet powerful demonstration of using the IOTA protocol for making payments and receiving services from a physical device. The goal of this tutorial is to demonstrate how we can build a simple power circuit that can be switched ON/OFF based on the current balance of a given IOTA address.
We will be using an internet connected Raspberry PI together with the Python programming language to check for balances on the IOTA tangle and perform switching of a connected relay using the PI’s internal GIO pins. The relay will again be connected to a simple battery powered circuit that turns ON/OFF a Light Emitting Diode (LED), representing the physical device in our project.
A simpler version of this project would be to connect the LED directly to the Raspberry PI’s GOI pins without using the relay. However, as the Raspberry PI GIO pins can only provide a maximum of 5V we will use a relay to demonstrate that we can use the same basic setup to manage high voltage devices. The main reason for choosing a low powered circuit is however that no one should be playing around with high voltage unless they absolutely know what they are doing.
The Use Case
Before going into details on building this project we should take a step back and look at the bigger picture as to how a simple demo project like this could be applied to a real world use-case, solving real problems.
Imagine you are staying at a hotel where each room is equipped with its own refrigerator. In most cases these refrigerators just sits there, consuming energy and not being used, still you end up paying for it indirectly as part of the rent. What if there was a mechanism that would allow you to pay the refrigerator directly for the time it is being used, and at the same time have it automatically turned off when it’s not being used? This is basically the use case we are going to recreate on this tutorial, only difference is that we will be replacing the refrigerator with a LED for convenience and safety.
Now, let’s try and describe a sequence of events to demonstrate how the system can be implemented and used.
First, let’s imagine the hotel owner has installed a refrigerator in your room, placing a relay in the refrigerator power circuit. The relay is then connected to the internal GIO pins on a Raspberry PI serving as a control unit for the refrigerator payment system. Next, he creates an IOTA address for the refrigerator to be used for monitoring when new refrigerator funds are being added. Finally, he prints a QR code of the IOTA address and attaches the QR code to the refrigerator.
Now that the physical part of the system is completed, he creates a simple Python program that runs on the Raspberry PI, continuously checking the refrigerator IOTA address for new funds, switching the refrigerator (relay) ON/OFF accordingly.
Now image you as the guest coming back from shopping having bought a nice bottle of white wine for later that evening. To make sure it stays cool you pick up your mobile phone, open your favorite IOTA wallet, scan the QR code attached to your refrigerator and transfer a certain amount of IOTA’s to the refrigerator depending on how long you plan to use it.
As soon as your transaction is confirmed by the tangle, the refrigerator balance is increased and the change in balance is picked up by the Python program running on the PI. The PI will then switch on the relay using its GIO pins and the refrigerator will turn on.
The Python program will keep track of time used and the amount of IOTA’s you transferred, continuously removing time from your active balance, and finally turning off the refrigerator when your balance is empty.
That’s it… Finally, you enjoy a nice bottle of cool wine before hitting the town.
In a scenario where you want to control multiple devices using the same setup it would probably be better to have a central Raspberry PI functioning as a common control unit for all devices, where each device is assigned its own unique IOTA address.This can easily be achieved using a multi-channel relay with some slight modification to the Python code. To simplify wiring and coding we will manage only one device in this tutorial, but feel free to extend the project later on to manage multiple devices.
In this section we will take a look at the different components required to building the project. You should be able to acquire them at most electronic stores or on EBAY/AMAZON for less than 50 USD.
The “brains” of the project is the Raspberry PI. The Raspberry PI will be running the Python code that monitors our IOTA tangle address for new funds and handles the Raspberry PI’s GIO pins.