Integrating physical devices with IOTA — The IOTA debit card, Part 2
The 7th part in a series of beginner tutorials on integrating physical devices with the IOTA protocol.
This is the 7th part in a series of beginner tutorials where we explore integrating physical devices with the IOTA protocol. This tutorial is the second part in a sequence of tutorials where we will try to replicate a traditional fiat based debit card payment solution with an IOTA based solution. In this second tutorial we will build a simple LED circuit representing the physical device that will allow us to pay for its service using our new IOTA debit card.
The Use Case
Now that we have our new IOTA debit card configured and ready to go as described in the previous tutorial, we will shift our attention to how we can use the card to pay for services. In the previous tutorial i used the coffee maker in the reception and the swimming pool locker as examples where we could integrate our new IOTA debit payment solution. But to be honest, a payment solution like this could probably be integrated into any machine or device that accept payments for its services, such as vending machines, slot machines, parking meters or the hotel room refrigerator from the first tutorial. And why stop there? The guest could even use his IOTA debit card to pay for dinner in the hotel restaurant or items the hotel gift shop, preventing the hotel owner from being charged additional fees by Visa or any other third party payment provider. The possibilities are basically endless.
We will use a simplified version of the LED powered circuit from the first tutorial to represent the device being payed in this tutorial. The main objective of this tutorial is to send valued transactions using our IOTA debit card so we will remove any unneeded components, wiring and coding from the first tutorial as this is not needed to get the point across.
About value transactions
A value transaction is an IOTA transaction that transfer IOTA tokens between IOTA addresses, as appose to tutorial 5 where we sent non-value transactions to the tangle. A typical value transaction actually consist multiple transactions packaged in a bundle. A bundle consist of 3 types of transactions:
- Input TXs
Negative value transaction(s) that deduct value from the sender address(es)
- Output TXs
Positive value transaction(s) that add value to the receiver address
- Unspent TXs
Zero sum transaction(s) that transfer remaining value from the sender address(es) to a new sender address. The purpose of these transactions is to prevent future spending from an already used sender address.
While this sounds rather complicated, the good news is that we don’t have to worry about the complexity of setting up the bundle, this will all be taken care of by the PyOTA library.
Notice that when all transactions in the bundle have been executed, the total amount of existing IOTA’s is unchanged, only the ownership addresses of the IOTA tokens has changed. This is of course an essential property of the IOTA protocol that must be enforced at all times.
Wiring the project
Next, lets have a look at wiring up the project. As described earlier, our circuit for this tutorial will be a simplified version of the circuit used in the first tutorial. I will remove the relay and battery from the circuit as they are not needed to demonstrate the point we are trying to make in this tutorial. If you like to include them in your version of the project, that’s OK. The Python code will work fine with or without them.
See tutorial 5 for information on how to set up the MFRC522 RFID reader/writer if you haven’t done so already.
See tutorial 1 for information on hooking up the LED. If you want to simplify the LED circuit as i did when writing this tutorial, you can skip the relay and battery and connect the LED directly to the Raspberry PI GIO pins. Note that you still need the resistor in the circuit or you may overload and damage the LED.
As pin 6 that was used for GROUND in the first tutorial now is occupied by the the MFRC522, i chose to use pin 9 instead as GROUND for my LED. In case you’r using a breadboard, you can also connect ground from both the MFRC522 and the LED to a common ground on the breadboard.
The Python code
The main objective of this tutorial is to use our IOTA debit card to pay a physical device for its service. In my example the service consist of a blinking LED. Before we start coding, lets to a quick rundown of the logic behind the code and how it works.
- The python script starts by checking the balance of the receiver (hotel owner) address so that we have a baseline for monitoring when new payments are being added to the address.
- Next, the python script shows a welcome message where you will be asked how many “blinks” you want to purchase. In my example i have set set the price to 1 IOTA for 1 blink.
- Next, you will be asked to hold your IOTA debit card close to the RFID reader.
As soon as the RFID reader detects the card, the Python script will read the IOTA seed from the card.
- Next, we check if there is enough funds on the debit card seed to pay for the number of blinks your ordered.
- If funding is OK, we define a new transaction before sending it to the Tangle.
- Next, we start monitoring the receiver (hotel owner) address balance to see if any new funds are being added. Meaning, if the transaction from the previous step has been confirmed.
- Finally, when the transaction has been confirmed we blink the LED the number of times ordered.
If you followed the previous tutorials in this series you will be aware of the address reuse problem that will occur when the hotel owner wants to spend any funds from the receiver address where the LED blink tokens are collected. In tutorial 4 we solved the address reuse problem using an LCD display with a QR code as a convenient way of enabling the user to pay using his mobile IOTA wallet. Now that we use our IOTA debit card to do the payment, there is no need for the user to know what receiving address is being used to pay the LED, this can all be managed behind the scenes. If you want to implement a solution for the address reuse problem in this project, use a similar approach as we did in tutorial 4, without the LCD.
You can download the source code from here
Running the project
To run the the project, you first need to save the code in the previous section as a text file in the same folder as where you installed the MFRC522-python library.
Notice that Python program files uses the .py extension, so let’s save the file as iota_debit_card_pay.py on the Raspberry PI.
To execute the program, simply start a new terminal window, navigate to the folder where you saved iota_debit_card_pay.py and type:
You should now see the code being executed in your terminal window asking you for the number of blinks you would like to purchase.
Improvement to the previous tutorial
Now that we have a basic understanding of how to create value transactions with PyOTA, it should be pretty straight forward to add a new option to the previous tutorial that will allow us to transfer funds from the hotel owner seed to the debit card seed. As you may remember, we had the function of generating a new address that was to be used when transferring new funds to the IOTA debit card. Now its just a matter of reversing the payment process used in this tutorial. Instead of sending value transactions from the IOTA debit card seed to the hotel owner address, we instead send value transactions from the hotel owner seed to the IOTA debit card address. Implementing this feature in the code from the previous tutorial should be a nice learning exercise that i will leave up to you as the reader.
You have probably noticed that you are not asked for any authorization or credentials when paying with your IOTA debit card. While this might be fine in some use cases, there may be other use cases where this is not acceptable. Imagine if you lost your IOTA debit card and it was picked up by a bad actor. Without any protection mechanism there would be nothing preventing him from using your card. In the next tutorial we will be addressing this issue by implementing a PIN code authorization mechanism for our IOTA debit card. Stay tuned…
If you like this tutorial and want me to continue making others, feel free to make a small donation to the IOTA address shown below.