Integrating physical devices with IOTA — Car-IOTA Part 1

The 12th part in a series of beginner tutorials on integrating physical devices with the IOTA protocol

Hugo Gregersen
Dec 1 · 8 min read

Introduction

This is the 12th part in a series of beginner tutorials where we explore integrating physical devices with the IOTA protocol. This time we will look at simplifying and automating payments for typical car related services such as parking, toll-road, washing etc. using the IOTA protocol.

I decided to make this a two part tutorial as i believe there are multiple ways of approaching this use-case. Each with its own advantages and disadvantages. More about that later.

In this first tutorial we will try to solve the use-case by utilizing a technology commonly referred to as Automatic License Plate Recognition, or ALPR for short.


The Use Case

In the back of our hotel there is a parking facility available for hotel guests and staff. Lately there have been problems with both authorized and unauthorized people using the parking facility without paying the required parking fees. Our hotel owner believes that this is related to the current manual and complicated process for handling parking fees. If only there was an automated system that could take care of collecting parking fees without bothering his guests or staff. After all, the current situation is loosing him money together with making his guests and staff frustrated. Something must be done.

Note!
The solution proposed in this tutorial is based on the idea that the hotel owner issues IOTA tokens and manages the SEEDS for his guests and staff. This idea was discussed in a previous tutorial where we looked at using RFID technology when paying for various services at the hotel. I suggest you quickly browse through these tutorials as an introduction to how is might work.

The IOTA Debit card, Part 1
The IOTA Debit card, Part 2
The IOTA Debit card, Part 3


The components

Before we start looking at the coding for this project, we should take a step back and look at the overall concept and the various components used.

Above you will see a simple layout of the various components being utilized to solve the use-case as proposed in this tutorial. Let’s brake them down one by one before moving on to the code.

Ultrasonic Sensor
The ultrasonic sensor is used to detect when a vehicle is entering or exiting the parking lot. An ultrasonic sensor measure distance by generating and receiving sound pulses. So its basically just a speaker and microphone with some additional electronics combined into one unit. By measuring the time from when a pulse was sent by the speaker, to the time the pulse was received by the microphone, we calculate the distance (as we know the speed of sound) to the object (in this case the vehicle) that reflected the pulse.

The ultrasonic sensor i used for this project is the popular HC-SR04. You should be able to get one of these off ebay for a couple of bucks.

Use the following circuit diagram to hook up the HC-SR04 to your Raspberry PI.

Warning!
Notice the two resistors placed in the circuit. The resistors are used to lower the voltage from the 5V output pin of the HC-SR04 to the 3.5 V input pin on the PI. Not having the voltage resistors in the circuit could damage your Raspberry PI.

Note!
There are of course various technologies you could use to detect when a vehicle enters or exits the parking lot. The only reason i’m using an ultrasonic sensor for this project was that i already had one laying around from a previous project.

Note!
Notice that the sound pulses generated by the sensor is in a frequency range not detectable by the human ear, so you will not hear any sound when the sensor is active.

Camera
The camera is used to take a picture of the vehicle license plate as it enters the parking lot area. I’m using the Raspberry PI camera module V2 for this project, but you could basically use any camera that can be controlled from a Python script. You should be able to get this camera module off ebay or from your local PI shop. On ebay you can also get fully functional Chinese knock-offs for only a few bucks.

OpenALPR
OpenALPR is an Automatic License Plate Recognition (ALPR) software used to identify license plate numbers from a picture or image. You have the option of installing the OpenALPR SDK on-premise, or use there cloud service to perform the ALPR. For this tutorial we will be using the cloud service. Notice that OpenALPR is licensed software, but you may sign up for a free account that allows you to perform up to 1000 ALPR’s a month for free.

To sign up for a free OpenALPR account, go to https://www.openalpr.com/

After login in to OpenALPR, select Cloud API

On the Cloud API page you will find a secret key that will be used in our python script when uploading images to the OpenALPR cloud service.

The Plate/SEED DB
The “Plate/SEED DB” is a reference to some type of centralized storage where each license plate number is pared with the IOTA SEED that will be used as sender of the IOTA value transaction. In this tutorial i’m using a simple comma separated text file (or CSV) file stored locally on the Raspberry PI. However, in any real life scenario where security is a priority, the SEED’s should probably be stored in some type of encrypted database with restricted access.

The Car
We also need some object representing the car itself. As you can see from the images in this tutorial, i’m using a toy car that i found at a flea market near by. For building and testing you only need a simple box with a printed license plate on one side as shown below. Or even cooler, setting up the system in its intended environment, using real cars and license plates.

Note!
Make sure you draw a border around the license plate number as shown above. Otherwise the OpenALPR algorithms will have problems identifying where the license plate located within the picture.


How it works

Before looking at the Python code for this project, lets take a step-by-step look at the events as they unfold when a new car enters the hotel parking lot.

  1. A new car enters the hotel parking lot and blocks the ultrasonic sensor.
  2. A picture of the license plate area of the car is taken by the camera.
  3. The picture is uploaded to the OpenALPR Cloud service that returns the identified license plate number in the form of a json object.
  4. We convert the license plate number from the json object to a string before searching the Plate/SEED DB for a match.
  5. If a match was found, we return the related IOTA SEED before issuing a value transaction to the tangle, using the returned SEED as sender for the transaction.

For reference:
Here is a picture taken from the PI camera as the car enters the parking lot and blocks the ultrasonic sensor.

and here is the picture as decoded by OpenALPR

Note!
Notice that the two T’s at the beginning was not recognized as part of the license plate number by OpenALPR. This might be related to this being a toy car without a valid license plate number syntax. Other tests i have done with actual license plate numbers seems to be working fine.

Required Software and libraries

Before we start writing our Python code for this project we need to make sure we have all the required software and libraries installed on our Raspberry PI. For this tutorial, besides PyOTA itself, the following libraries are required:
* The PiCamera library (already installed with Raspbian)
* Requests (pip install requests)


The Python Code

And here is the python code for this project..

The source code for this python script can be downloaded from here


About the Plate/SEED DB

The python script is looking for the “Plate/SEED DB” in a text file called plates.csv located in the home directory on your PI. Make sure you update this file with your plate numbers and SEED’s before running the project. You will find an example of how the plates.csv file should be structured here.


Running the project

To run the the project, you first need to save the scrip from the previous section as text files on your computer.

Notice that Python program files uses the .py extension, so save the files as car-iota-p1.py on your Raspberry PI

To execute the scripts, simply start a new terminal window, navigate to the folder where you saved the script and type:

python car-iota-p1.py

You should now see the python script start checking for incoming cars every 2 sec. using the ultrasonic sensor. When a new car is found, we initiate the sequence as described in the “How is works” chapter. Check the raspberry terminal for statuses as the sequence progresses.

Note!
In a real life scenario you would probably want to have some type of indicator telling the car owner if and when the transaction/payment was accepted (or not). This could be done using a light indicator switching from red to green, or even a physical barrier being removed.

Note!
In my example the Raspberry PI does all the work with respect to creating and signing the required IOTA value transactions as new cars enters the parking lot. You will soon notice that this is a pretty slow process due to the limited resources of the PI. In a real life scenario this would probably not be acceptable and we must look for other alternatives. One option would be to outsource these activities to a centralized entity that has the resources of performing these activities efficiently.


Whats next?

While the solution proposed in this tutorial might work for some local use-cases. For other use-cases, the concept of a centralized entity (in this case the hotel owner) controlling the SEEDS would not be practical or acceptable. For a truly global and decentralized solution we probably need to take a different approach. More about that next time.


Contributions

If you would like to make any contributions to this tutorial you will find a Github repository here.


Donations

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.

GTZUHQSPRAQCTSQBZEEMLZPQUPAA9LPLGWCKFNEVKBINXEXZRACVKKKCYPWPKH9AWLGJHPLOZZOYTALAWOVSIJIYVZ

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Hugo Gregersen

Written by

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade