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.

Note!

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:

python iota_debit_card_pay.py

You should now see the code being executed in your terminal window asking you for the number of blinks you would like to purchase.