This is the 13th part in a series of beginner tutorials where we explore integrating physical devices with the IOTA protocol. If you have been following the IOTA project for some time you may have heard about strange ideas such as “cars having their own wallets” or “cars paying for their own services”. While this may sound intangible and futuristic, it’s basically the idea we will be taking on in this tutorial.
The use case
As you may remember, we ended the previous tutorial by pointing out some problems related to the centralized nature of the ALPR approach, where a centralized entity (aka, the hotel-owner) would have to have control over the seeds used when performing the payment transactions. The great thing about this approach is that the car itself does not require any new electronics, (as the infrastructure (parking facility) takes care of all the payment transactions). While this might be acceptable for some local use-cases, it would not be very practical if we wanted to implement our parking payment system on a truly decentralized and global scale.
In this tutorial, we will try and deal with this problem by simply turning everything upside-down. Instead of having the parking facility manage the payment transaction, the car itself will do all the work. This way the IOTA seed never leaves the car (seed) owner.
The first thing we need to deal with when taking on this problem is that we need some type of data exchange between the infrastructure (parking facility) and the car so that the car knows what payment address to use when sending the payment transaction. I’m guessing there are multiple wireless data protocols and technologies that could be used for this purpose (radio, blue-tooth, RFID, etc.) but I felt they all had some disadvantages that were not optimal for this particular use-case. After puzzling over this problem for a while I decided to go with a simple, yet familiar technology that you probably use every day when sitting in front of your television, namely Infrared communication, or IR for short.
About IR and IR communication
IR communication is based on light pulses being sent from an IR transmitter to an IR receiver. To prevent interference from “normal” light, IR uses light in the infrared light spectrum, hens the name Infrared. As the technology suggests, IR is a binary communication protocol where the length in time between each individual light-pulse determines if the data being sent is a 0 or 1. Different implementations of IR communication use different logic with respect to pulse length, the time between pulses, the number of pulses in a data packet, etc. In this tutorial, we will be using a particular IR protocol called NEC. The NEC protocol is a 32-bit protocol that allows us to send 32 bits of data (0 and 1) in one data packet. As it takes 8 bits to define a byte, the NEC protocol allows us to transfer 4 bytes of data in each data packet. As the typical IOTA address consists of 90 bytes (including the checksum), it would then take 23 NEC data packets to transfer the complete IOTA address.
The main components used in this tutorial is the IR transmitter and the IR receiver. For my project, I’m using the popular NE555 IR modules. They often come in a pair and you should be able to get them both of eBay for a couple of bucks.
Besides the IR modules themselves, we also need to hook up each module to a micro-controller that will take care of the logic with respect to sending and receiving data.
The receiver controller
As the receiver micro-controller for this project, I decided to use my Raspberry PI together with the PyOTA library. The main reason being that the receiver will not only be used to manage IR communication, it will also have to take care of any interaction with the IOTA tangle.
There is now an IOTA C client library available, capable of communicating with the IOTA tangle, if you prefer using a different micro-controller as the receiver controller, such as the ESP32.
The transmitter controller
The IR transmitter controller is basically just telling the IR transmitter module what data to send and requires no network or IOTA capabilities. For this reason, I selected the cheapest and simplest micro-controller i had laying around. My trusted Arduino UNO.
Now, let’s take a look at how each module is connected to its respective controller.
Here is how the IR receiver module is hooked up to the Raspberry PI board
And here is how the IR transmitter module is hooked up to the Arduino UNO board.
How it works
Before moving on to the code used for this project, let's take a step back and look at the general idea behind the concept proposed in this tutorial and how it works.
Imagine the receiver part of the project (IR receiver module and controller) is placed inside the car, constantly monitoring for incoming IR data from its surroundings. At the same time, imagine the IR transmitter placed at the entrance of a parking facility constantly publishing its IOTA payment address.
As the car approaches the parking facility, it starts picking up the IR signals from the transmitter. As soon as the receiver (car) has validated the received data as a valid IOTA payment address, it automatically executes the payment as it enters the facility.
In a real-life scenario, some type of approval from the driver would probably be required before the payment is being executed.
Additional data besides the IOTA payment address itself could be added to the data transmission to include metadata such as the name of the parking facility etc.
Next, let’s have a look at the code used for this project.
Let’s start with the Arduino code being run on the transmitter side.
The first thing you should notice is that we are using an external Arduino library called IRemote when sending data from the IR transmitter module. You can download the IRemote library as a zip file here. To install the IRemote library, select Sketch->Include Library->Add .ZIP Library inside the Arduino IDE
Secondly, we need to specify the IOTA address that are to be published by the transmitter. This data is stored in the msgArray variable where each element in the array consist of four bytes.
Notice that the irsend() function requires the data to be specified in a hexadecimal format, so when setting up msgArray variable, we first need to convert our IOTA address to a hexadecimal format.
I made a simple Python script to help converting an existing IOTA address to a format acceptable by our Arduino sketch, you will find a link to this script here.
Notice that before we start sending the actual iota address data, we start by sending a 0x30303030 message to let the receiver know where the address data sequence begins (and ends)
Finally, there is a variable (delay_time) that can be used to control the time between each individual IR message being sent. A smaller delay_time leads to faster data transfer.
I tested using a delay_time as low as 50 milliseconds with success. This would give us a total transfer time for the complete IOTA address to 50x23=1,1150 milliseconds.
And here is our transmitter Arduino code:
The source code can be downloaded from here
Now let's move on to the Python code being run on the receiver side.
I’m not going into details here as the code itself is pretty well documented. However, I will give you a broad overview of whats going on.
First, we start listening for binary data is they come in through the IR receiver module. As we know the sender follows the NEC protocol (ref.
irsend.sendNEC() from our Arduino sketch), we analyze the data stream by measuring the time between each individual pulse to determine if the data is a 0 or 1. Notice that the NEC protocol also specifies the beginning and end of each message. Next, we convert the binary data to hexadecimal, before again converting it to bytes. If the received byte data equals 0000, we know a new sequence of IOTA address data is coming. As new address data is coming in we simply append the new byte string to the previous byte string until the next 0000 message comes. We then check the complete string to see if it is a valid IOTA address. If yes, we execute the IOTA value transaction.
And here is our receiver Python code:
The source code can be downloaded from here
Running the project
To run the project you should start by connecting your IR transmitter module to your Arduino UNO. Then upload the Arduino sketch from the previous section to the board using the Arduino IDE.
Make sure you have installed the IRemote library before compiling and uploading the code.
If you have connected the IR receiver module to your Raspberry PI you should now see a small LED start blinking on the module every 500 milliseconds. This indicates that your transmitter is working properly.
Next, download the Python script from the previous section and save it on your Raspberry PI as car-iota-p2.py
To execute the Python script, simply start a new terminal window on your Raspberry PI, navigate to the folder where you saved the script and type:
You should now see the Python code being executed in the terminal window, displaying data as it is received from the transmitter.
Remember to update the car-iota-p2.py file with a valid seed (with a positive balance) to be used when publishing value transactions to the IOTA tangle.
If you would like to make any contributions to this tutorial you will find a Github repository here.
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.