For beginners: Switch a motor on/off using a Raspberry Pi and IOTA
Purpose of this program: the payment of IOTA coins to a specific address will switch a little motor on until the paid coins have been used (1 second = 1 IOTA).
This article is based on my article “For beginners: Switch a light on with the payment of IOTAs using a Raspberry Pi”.
Contact me if you are stuck. Also please let me know if you see anything that needs improvement.
1. Hardware Setup
- Raspberry Pi 3 (or 2) with the following components: WiFi, 32GB Micro SD card, Power supply.
- 1 Channel DC 5V Relay Switch Board Module for Arduino Raspberry Pi ARM AVR
- Breadboard wires (F/M and M/M)
- Mini DC motor. I searched for the cheapest motor in eBay. The motor I purchased is 6V which means you need a resistor or use the motor for a short time only when using a 9V battery
- 9V Battery
In addition, you will need for the setup: USB keyboard, USB mouse and HDMI cable.
Look carefully at the diagram.
- The output side of the relay decides the on/off operation. If the orange cable (as seen in the diagram below) 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.
- Once you turn the Raspberry on, the red light on the relay should light up. The green light will only light up if the relay is activated.
2. Software setup
On your computer do the following:
- Download and install SD card formater on your computer: https://www.sdcard.org and format the SD card.
- Download Raspbian (NOOBS): https://www.raspberrypi.org/downloads/noobs/ . You can download NOOBS lite.
- 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:
Reboot the Raspberry Pi:
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 motor
Let us test if the installation was so far successful.
Open the terminal on the Raspberry and type:
sudo nano motor.py
insert the following code:
To save the file press Ctrl + X then press Y and then press Enter.
Now run this program:
sudo python motor.py
You will hear the relay clicking and the green light on the relay turning on and off. If the motor is not starting 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):
sudo apt-get install libffi-dev (to avoid the error: “failed building wheel for cryptography”)
sudo pip install pyota[ccurl]
To test if the installation was successful (this will take around 10 minutes):
python setup.py test
5. Using IOTA payments to switch the motor on/off
You pay IOTAs to an address and the motor switches on and stays on (1 IOTA = 1 second of power) according to the amount that was paid in. This solution checks only when new coins arrive and ignores coins that are already in the account.
In the Terminal type the following commands:
sudo nano power.py
You need to make the following change: 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:
- Get the IOTA address
- Insert the address in the program power.py
- Start the program power.py
4. While the program is running, transfer for example 11 IOTA (make sure it is not 11 Mi) to the above used address.
5. It will take around 2–10 minutes for the coins to be transferred. You will then see that the motor switches on and in our example after 11 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.
6. Issues / Complications/Advice
- IOTA has non reusable addresses. It is safe to receive any number of transactions to a given address until an outgoing transfer (a “send” transfer) is made. After that, this address should no longer be re-used! If you are using for this program the Trinity wallet, then you will encounter this problem as the wallet will start sending funds from an address that has funds on it. For example you start sending 12 IOTAs from address A to B (B being the address you use in your code), the after 2 or three trials you are not able to send any IOTAs t B anymore (the wallet will not allow it). Solution:
- use a Trinity wallet to send and another one to receive coins.
- Simply use a new empty address every time the wallet does not allow you to send to an address anymore
- Working with the relay can be tricky if you are a beginner. Important: the red LED should always be on, the green light only when the relay is activated. Check the voltage port (PIN 1 or 2).
- Try to get motor.py working before starting with power.py.
- To test power.py without having to send IOTAs change the formula in line 61 from “if powerbalance > 0:” to “if powerbalance == 0:”
- Anything else: please let me know!