Integrating physical devices with IOTA — The reverse vending machine

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

Hugo Gregersen
Coinmonks
Published in
7 min readJul 3, 2020

--

Introduction

This is the 17th part in a series of beginner tutorials where we explore integrating physical devices with the IOTA protocol. In this tutorial we will be creating the basic functions of an IOTA powered reverse vending machine using a Raspberry PI, a touch display, a camera, and a bar-code scanner.

Note!
The touch display and/or bar-code scanner is not required to play around with this tutorial as you also have the option of interacting with the UI using a traditional monitor, mouse and keyboard.

When I started working on this tutorial my initial idea was to do some type of IOTA powered vending machine. However, i soon realized that the main functions of such a machine would be more or less identical to the projects we did in the first four tutorials; only difference being that instead of turning ON/OFF a power relay, we would now have to trigger some type of robotic motion.

After puzzling over this problem for a while i decided to go with a different idea.., what if we took the previous tutorials and reversed the use case? The result would then be some kind of a reverse vending machine. Instead of the customer paying the machine for its services, the machine would now have to pay the customer. This would surely introduce some new challenges.

Get Crypto Products on Discount

The Use Case

Lately our hotel owner have noticed his guests leaving empty bottles and cans all around his hotel and swimming pool area without bothering to picking them up and throwing them in the recycling bin. He could of course have his staff do it but they are normally busy doing other work.

What if he could provide his guests with some type of incentive or reward for cleaning up there own mess? After all, it would probably be cheaper than hiring a new employee.

After thinking about this problem for a while he decides to try a concept he has seen implemented in other places; A machine that rewards the customer for recycling, in other words: A reverse vending machine.

For this idea to work, he feels his new “machine” should fulfill some basic requirements:

  1. The machine must be easy to use with a minimum of effort from the guest.
  2. Individual bottles and cans should give individual rewards depending on there size and type.
  3. The reward should be payed out with IOTA tokens (after all, IOTA is the standard currency at Hotel IOTA and can be used to pay for other hotel services later on)

The concept

Now, lets take a look at the basic concept behind this idea and how it might work.

Lets say Alice is packing up after a long day beside the hotel swimming pool.

On her way out of the swimming pool area, she brings her empty bottles/cans and makes a stop at the recycling bin next to the exit.

She then takes the bar-code scanner mounted next to the bin and scans the bar-code on each individual bottle/can before throwing it into the bin. A small LCD display next to the scanner shows the recycling reward for each individual item together with the total reward.

After scanning all the items, she pushes the “Get reward” button on the touch display.

She then takes her mobile phone with the Trinity wallet and generates a new receiver address.

Finally, she opens the QR code for the new address and places her phone in front of the camera placed next to the scanner.

As soon as the receiver address (QR code) has been detected by the camera, an IOTA payment transaction for the total recycling reward is created and sent to Alice’s receiver address.

Alice can now go and sell her recycling reward on the open IOTA market, or spend it on other IOTA supported services inside or outside the hotel.

The components

Next, lets take a quick look at the hardware components used in this project.

The Raspberry PI
For this project i’m using my new Raspberry PI 4, but any PC should basically work.

The Bar-code scanner
I got this USB connected bar-code scanner off ebay for about 14 USD

The camera
In my version of the project i ended up using an old Logitech webcam i had laying around. You may also use the Raspberry PI camera module with some minor adjustments to the Python code.

The touch display
My plan was to use a 7 inch touch display for this project. However, since i did not have any micro-hdmi (from the new RPI4) to standard-hdmi (on my touch display) cable, i ended up using a normal monitor and mouse for interacting with the UI. More about this later.

The User Interface (UI)

As with the 3th tutorial in this series, i decided to go with Python and the Tkinter toolkit when building a simple UI for the customer to interact with the system.

Here is a quick description on how to interact with the UI:
For each recyclable item, place the cursor in the upper left text box and scan the bar-code printed on the item (you may also type the bar-code ID manually into the text box); then press enter or push the “Scan Item…” button to register the item. The Python script will now check if the bar-code found on the item exists in the local bar-code database (barcodesdb.csv). If the bar-code was found, the item will be added to the item list together with its corresponding recycling reward value. As more items are added to the list, the total recycling reward will be displayed below the list. When all items have been registered, push the “Get Reward” button. As soon as the “Get Reward” button is pushed, the camera is activated. Now, hold a QR code of the IOTA reward receiver address in front of the camera. When the the QR code has been detected by the camera, the reward payment is executed in the background (see the bottom message area for status). Finally, the UI is reset and prepared for the next customer.

Note!
As i did not have the micro HDMI to standard HDMI cable when writing this tutorial, the layout of the current Tkinter UI is tailored for a normal PC monitor. Please check out the
3th tutorial for guidance if you want to implement the UI on a smaller size touch display.

The Bar-code database

Before adding new items to the recycled items list, we first need to check that the item is approved for recycling. We also need to get the reward value for each individual item. For this purpose we use a simple lookup table in the form of a comma separated text file (.csv), where the first element represents the bar-code ID, and the second element represents the recycling reward in IOTA’s

Note!
Before you start scanning bottles or cans, make sure you update the barcodesdb.csv file to include any bar-code ID’s you plan to recycle with the app. You also need to include the reward (in IOTA’s) for each individual bar-code ID.

Required Software and libraries

Next, let’s take a a look at the software and Python libraries required to run the app.

The hardest part is probably getting OpenCV installed and running on your machine. Fortunately, i found this great tutorial on how to install OpenCV on a Raspberry PI 4, written by Adrian Rosebrock.

The rest should be pretty straight forward using pip.

Pillow, imutils, pyzbar, PyOTA, PyOTA-CCurl

The Python Code

I decided to split the code for this project into two separate Python files. The rvm_lib.py file contains a class with the main functions of the app., while the rvm_app.py file is used for launching the app.

Besides that, the code is pretty well documented so i will not include any more details here.

Here is the rvm_app.py file:

You can download the source code from here

and here is the rvm_lib.py file:

You can download the source code from here

You also need the barcodesdb.csv file that you will find here

Running the project

To run the project, you first need to save the Python and barcodesdb.csv files from the previous section to your machine.

Next, update the bacodesdb.csv file with bar-code ID’s that you plan to use with the app.

To execute the app., simply start a new terminal window, navigate to the folder where you saved the files and type:

python rvm_app.py

You should now see the UI appear on your LCD/Monitor.

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

--

--