For beginners: Switch a light on with the payment of IOTAs using a Raspberry Pi

Roberto Rey
Jan 19 · 5 min read

This article is based on the instructions written by Hugo Gregersen: Integrating physical devices with IOTA. I was not able to get things working according to his instructions so I started trying until I got it working. Please let me know if you see anything that needs improvement.

1. Hardware Setup

1.1 Components

In addition, you will need for the setup: USB keyboard, USB mouse and HDMI cable.

1.2 Assembly

Look carefully at the diagram.

  1. The LED has two legs. The shorter leg has to be on the side of the resistor.
  2. The output side of the relay decides the on/off operation. If the orange cable is connected to the bottom, then it is OFF/ON. If you connect the orange cable to the upper slot the operation will be ON/OFF.
  3. Once you turned on the Raspberry, the red light on the relay should be on

2. Software setup

On your computer do the following:

  1. Download and install SD card formater on your computer: https://www.sdcard.org and format the SD card.
  2. Download Raspbian (NOOBS): https://www.raspberrypi.org/downloads/noobs/ . You can download NOOBS lite.
  3. Copy the NOOB content to the micro SD card (only copy the content inside the downloaded folder and not the folder itself).

Remove the micro SD card from your computer and insert it into the Raspberry Pi.

Connect to the Raspberry Pi:

  • Monitor using the HDMI cable
  • Mouse and keyboard
  • Power cable

Power up the Raspberry Pi and install the full version of Raspbian.

After reboot, select menu →Preferences → Raspberry Pi configuration, then select tab Interfaces and enable: SSH, SPI and VNC.

If you prefer to connect remotely to the Raspberry Pi perform the following steps:

On the Raspberry open the terminal and use the following codes:

sudo apt-get update

sudo apt-get install realvnc-vnc-server realvnc-vnc-viewer

To get the raspberry Pi ip address (for example 192.168.1.20), type in the terminal: ifconfig

Reboot the Raspberry Pi:

sudo reboot

On your computer download the VNC viewer: https://www.realvnc.com/en/connect/download/viewer/ and follow the instructions.

Once installed use the Raspberry Pi IP address as well as username and password to log into your Raspberry.

3. Installation of Python

1. Ensure your Raspberry Pi is running the latest version of all the software. Run the following two commands on your Raspberry Pi to update it.

sudo apt-get update

sudo apt-get upgrade

2. Install the python2.7-dev package

sudo apt-get install python2.7-dev

4. Testing the light

Let us test if the installation was so far successful.

Open the terminal on the Raspberry

sudo nano light.py

and insert the following code:

To save the file press Ctrl + X then press Y and then press Enter.

Now run this program:

sudo python light.py

You will hear the relay clicking and the green light on the relay turning on and off. If the LED is not turning on you most likely will have the cables to the battery wired wrong.

4. Activate IOTA

To communicate with the IOTA tangle you need to install the PyOTA library (https://github.com/iotaledger/iota.lib.py):

cd ~/

sudo apt-get install libffi-dev (to avoid the error: “failed building wheel for cryptography”)

sudo pip install pyota[ccurl]

git clone https://github.com/iotaledger/iota.lib.py.git

To test if the installation was successful (this will take around 10 minutes):

cd iota.lib.py

python setup.py test

5. Using IOTA payments to switch the light on/off

Hugo’s idea a good one: You pay IOTAs into an account and the light switches on according to the amount that was paid in. His solution checks when the new tokens arrive and ignores tokens that are already in the account.

Let’s now use the program written by Hugo. In the Terminal type the following commands:

cd ~/iota.lib.py

sudo nano lightson.py

You need to make 2 changes:

  • Row 13 change LEDPIN to 21
  • Insert your IOTA address

You will need an IOTA address. For this I recommend installing the trinity wallet on your computer (https://trinity.iota.org) and get a new address that way.

This is the process to get it working:

  1. Get the IOTA address (it does not matter if there are no tokens on that address)
  2. Insert the address in the program lightson.py
  3. Start the program lightson.py

python lightson.py

The program will start running (the 0:00:00 stands for the time the lights will be switched on. It is a countdown until Lights Off).

4. While the program is running transfer for example 12 IOTA (make sure it is not 12 Mi) to the above used address.

5. It will take around 2–3 minutes for the tokens to be transferred. You will then see that the light switches on and in our example after 12 seconds (for every IOTA 1 second) it will switch off again.

FYI: To find the addresses in your Trinity wallet:

To copy an address simply click on it.

Coinmonks

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

Roberto Rey

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