Integrating physical devices with IOTA — The Track&Trace App.

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

Hugo Gregersen
Coinmonks
Published in
9 min readJun 20, 2020

--

Introduction

This is the 16th part in a series of beginner tutorials where we explore integrating physical devices with the IOTA protocol. In this tutorial we will be using a bar-code scanner together with our own web app. to simulate using the IOTA tangle to create a tamper proof audit trail of events in a typical supply chain with multiple actors.

Note!
You will also have the option of typing in bar-code ID’s manually in case you don’t already have access to a bar-code scanner.

In my personal opinion, supply chain management is probably one of the first areas where we will see real industry adoption of DLT technology. The main reason being that this technology has the potential of solving so many of the problems this industry is struggling with today, problems such as lack of transparency, tractability and trust. I would even go as far as suggesting that DLT technology will be at the core all supply chain interactions in the future.

The Use Case

Lately our trusted hotel owner have noticed a problem with degrading quality of seafood products he gets from his friend Bob the fisherman, who is located in a small fishing village up north. He suspects that the problem is not related to Bob or the product itself, but rather delay in the supply chain from when the product was shipped to when it arrives at his hotel. As there are multiple actors involved in the shipment, it is not clear who is responsible for the delay and who should give him a refund for the faulty product. When he tries to address the problem with the various actors in his supply chain, they basically blame each other and no one takes responsibility. He even suspect that some of them manipulate the paperwork that comes with the shipment to avoid taking responsibility.

Our hotel owner feels that the underlying problem with lack of accountability is actually related to lack of transparency. If only there was a publicly available, tamper proof system where each actor would have to record there interaction with the product, he could then audit the trail of interactions in his supply chain and identify who is to blame.

But where would he find such a system?, again he turns to the IOTA tangle.

Before we start looking at a functional solution, we first need to take a step back and look at some basic requirements for the system:

  1. The system must be globally available to all actors whiteout having to install any software on the client computer beside a web browser.
  2. All records related to a particular shipment must be publicly available to all actors to prevent any disputes.
  3. The system must include some type of private key encryption that proofs that the appropriate actor recorded the appropriate record in the system.
  4. A record should be created in the system when an actor receives a shipments (Inbound), and when it is delivered to the next actor in the supply chain (Outbound)
  5. The record must include some basic metadata that identifies the date/time when the record was entered into the system, the name of the actor and the type of transaction (Inbound or Outbond)
  6. The system should have an easy to use interface together with bar-code support to simplify entering and retrieving data to and from the system.

Note!
Any production ready version of the Track&Trace App. would probably include some type of actor login where metadata such as actor name and private key would be stored in a centralized database, instead of having to be provided in the UI for each transaction. However, to prevent additional complexity and to promote usability when playing around with the app., i decided to not include any login mechanism in my version of the App.

About the Track&Trace concept

Before we move on to the tech., lets take a quick look at the basic concept.

Lets imagine the purchaser at Hotel IOTA wants to place a new purchase order to Bob for 10kg of fresh salmon.

Together with the purchase order, a random 13 digit number is generated by the purchaser and provided to Bob to be used as a unique tracking ID for this particular order.

Why a 13 number you ask.., well one of the most commonly used bar-code systems in the world is the EAN-13 bar-code. This means that it can be read by most standard bar-code scanners out-of-the-box.

Next, Bob takes the 13 digit number and puts it into an online bar-code generator such as this one hosted by TECH-IT.

Then Bob prints out the bar-code and tapes it to the package of fresh salmon.

Finally, as Bob hands over the package to the “Trucks R Us” pickup guy, he takes out his bar-code scanner and registers an “Outbond” transaction in the Track&Trace App.

At the same time, when the “Trucks R Us” pickup guy receives the package, he takes his bar-code scanner and registers an “Inbound” transaction in the Track&Trace App.

As the “Trucks R Us” pickup guy hands over the package to the next actor in the supply chain (SeaTrans Inc.) he registers an “Outbound” transaction in the Track&Trace App.

And so it goes on with each actor in the supply chain until the package finally arrives at the Hotel IOTA warehouse.

As a result, we have now effectively created a closed loop, tamper proof audit trail of each shipment interaction that can be checked and verified by the Hotel owner, purchaser or any other 3. party actor at any time.

So, how do we prevent one actor from posing as i different actor when entering records into the system? Well, in my version of the project, each actor is provided a unique private key that is only known to them. The private key together with the unique bar-code ID is then used to generate a unique hash that will be stored inside the message fragment of each IOTA transaction. The hash can now be used to verify that the correct actor created the appropriate record.

Note!
When playing around with the Track&Trace App. you don’t necessarily have to generate and print a bar-code from scratch. EAN-13 codes are used on many everyday items you get from your local store. Just have a look in your refrigerator.

Bar-codes vs IOTA addresses

The Track&Trace App. uses IOTA transactions and addresses when storing records in the IOTA Tangle. Each Track&Trace record is stored as a unique transaction object in the IOTA Tangle, and all transactions related to one particular bar-code is “linked” to a corresponding IOTA address. So how does the system know what IOTA address to use when it is not provided by the user? Well, we use a clever function in the PyOTA Kerl package that takes a random string and converts it into an IOTA address. In our case, we send our 13 digit bar-code to the function and get a valid IOTA address in return. The function is deterministic, so we always get the same IOTA address from the same 13 digit bar-code.

About the Bar-code scanner

As previously mention, you don’t need a bar-code scanner to play around with the Track&Trace App. However, as this tutorial after all is part of the “Integrating physical devices with IOTA” series, i guess its appropriate we at least introduce one new “physical device” in the tutorial.

Bar-code scanners are actually pretty simple devices, just plug it into a USB port on your PC and start scanning. I believe most bar-code scanners functions as a keyboard emulator so there is no need for any drivers or software libraries to get this thing going. As such, you should be able to use any bar-code scanner with this tutorial.

I got this particular bar-code scanner of ebay for about 12 USD.

About the Track&Trace system infrastructure

Let’s start by looking at a potential system infrastructure that might support the requirements we discussed in a previous section.

First of all, the IOTA Tangle should be the ideal backbone for storing our records as it provides free, global, and tamper proof data storage, publicly available to all actors in the supply chain.

Next, we need a web-server where we can design and host a simple and easy to use UI for our supply chain actors. Having our code run on a centralized web-server also prevents us from having to deal with client side software. Any interaction with the IOTA Tangle will also be handled by the server to take the load off clients with respect creating and retrieving transactions to and from the Tangle.

A simple sketch of our infrastructure should now look something like this.

Note!
When i tried hosting the web server on my native Windows OS i ended up getting some threading issues when serving web pages and interacting with the IOTA Tangle at the same time, so i strongly suggest you run the web server on a Linux system. In case you don’t have access to a Linux system i suggest you check out the
Windows Subsystem for Linux (WSL). It worked perfectly for me.

About the Track&Trace software infrastructure

Next, lets have a look at the main software component used in our system.

There are probably hundreds of different web servers and coding languages out there that could be used for our project, but I think we should stay true the previous tutorials in this series and again build our solution using Python. After all, if you have followed the previous tutorials in this series you should already be familiar with the PyOTA Python library that we will use for interacting with the IOTA Tangle. When it comes to selecting a framework for building simple web-applications using Python, the first thing that comes to mind is Flask. If your not already familiar with Flask i suggest you check out this excellent tutorial by Miguel.

About the Track&Trace User Interface

The Track&Trace App. user interface basically consist of two input pages (forms) and a search result page.

The “Register transaction” page is used for creating new records (transactions) into the system (IOTA Tangle)

The “Display transaction history” page is used for retrieving existing records (transactions) from the system (IOTA Tangle)

Finally, the “Display transaction history result” page simply displays a list of existing Track&Trace records (transactions) incl. metadata for a particular bar-code ID (IOTA address)

Note!
If using a bar-code scanner with the Track&Trace App., just place your cursor in the “Barcode” field, scan the code and the 13 digit bar-code ID will appear in the field.

The code

This is where i normally would include the code for this tutorial. However, as most Flask Apps. (this included) consist of multiple files (Python, Html etc.) i don’t think that makes much sense. Instead you are better of checking out the code directly on the Track&Trace Github page.

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 below.

NYZBHOVSMDWWABXSACAJTTWJOQRPVVAWLBSFQVSJSWWBJJLLSQKNZFC9XCRPQSVFQZPBJCJRANNPVMMEZQJRQSVVGZ

Get Best Software Deals Directly In Your Inbox

--

--