Raspberry Pi IoT Projects for Fun and Profit
In this post, I will explain how to run an IoT project from the command line, without graphical interface, using Ubuntu Core in a Raspberry Pi 3.
Now, let’s blink a LED. To install some libraries (including Machine Learning ones) you will need to install the latest version of setuptools, install pip and Rpi.GPIO, which is the library that allows you to send data through Raspberry pins. Also, you will need to set up some permissions.
sudo apt-get install python-pip python-dev sudo pip install setuptools sudo pip install wheel sudo apt-get update sudo apt-get upgrade sudo pip install RPi.GPIO sudo chown user /dev/gpiomem sudo chmod g+rw /dev/gpiomem
Below I present the Raspberry Pi 3 pin mapping, with respective numbers assigned:
You can also generate this mapping in the command line:
git clone git://git.drogon.net/wiringPi cd ~/wiringPi git pull origin ./build gpio readall
Connect your LED the following way, using a resistor:
Create the LED.py notebook:
touch LED.py vi LED.py
Then, CTRL+C Â i CTRL+SHIFT+V the following code:
import RPi.GPIO as GPIO from time import sleep GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) while True: Â Â Â GPIO.output(8, GPIO.HIGH) Â Â Â sleep(1) Â Â Â GPIO.output(8, GPIO.LOW) Â Â Â sleep(1)
Type ESC : wq
Then run:
sudo python LED.py
The Raspberry will send an energy pulse to pin 8 each one second, turning on the LED:
Another option is to attach a buzzer KY-012, that will emit a sound every time GPIO output is set to High.
The LED and CPU Temperature are the simplest projects one can develop using a Raspberry Pi. Now let’s install Keras and Tensorflow in a Raspberry running Ubuntu Core, a non trivial task at the time this project was developed. As Ubuntu Core is a minimalist OS, it does not have wget, unzip and many libraries’ dependencies.
sudo apt-get install wget sudo apt-get install unzip
Now let’s install Python libraries:
export LC_ALL=C wget http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-armv7l.sh sudo md5sum Miniconda3-latest-Linux-armv7l.sh sudo /bin/bash Miniconda3-latest-Linux-armv7l.sh
Reboot. Run commands to install libraries and their dependencies:
export LC_ALL=C conda install scikit-learn sudo apt-get install libblas-dev sudo apt-get install liblapack-dev sudo apt-get install python3-dev sudo apt-get install libatlas-base-dev sudo apt-get install gfortran sudo apt-get install python3-setuptools sudo apt-get install python3-scipy sudo apt-get update sudo apt-get install python3-h5py
Now we will install Tensorflow for ARM systems.
wget https://github.com/lhelontra/tensorflow-on-arm/releases/download/v1.8.0/tensorflow-1.8.0-cp35-none-linux_armv7l.whl
These steps below will take one hour or longer.
cd miniconda3 sudo apt-get install python-software-properties cp /home/user/tensorflow-1.8.0-cp35-none-linux_armv7l.whl tensorflow-1.8.0-cp35-none-linux_armv7l.whl easy_install pip==1.5.6 pip install numpy==1.14.5 sudo apt-get install python3-pip sudo pip3 install tensorflow-1.8.0-cp35-none-linux_armv7l.whl sudo pip3 install keras sudo apt-get install software-properties-common sudo apt-get install libstdc++6 sudo add-apt-repository ppa:ubuntu-toolchain-r/test sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade
Now all Machine Learning libraries are installed at $ sudo python3
(Note that Tensorflow lately has released an easier way to install its library in ARM systems via pip)
Now you can attach a camera, use OpenCV for object detection, face recognition, attach a Movidius stick, collect temperature, or even develop a self driving car project with built in kits. Here are some simple ideas:
1 - Connect a Temperature sensor and after a defined threshold, generate light (via LED) and play a sound (buzzer).
2 - Connect a sound detector (cylinder below on the left, red circuit) and every time a sound above a certain level (adjusted in the potentiometer, blue box behind wires) is detected, turn on a laser light (center) and a LED (right).
3 - Connect an infrared receiver (black tube on red circuit right below) so that you can deploy code automatically by switching on your air conditioning remote control, at the same time sensors emit light and sound when the process is successful.
This automation idea is an adaptation of what was seen here, published by Amazon Web Services: https://www.linkedin.com/feed/update/urn:li:activity:6437325206838140929
4 - Develop a simpler version of the project mentioned above, by connecting a push button sensor directly to the Raspberry Pi:
A general overview of the ongoing project and these ideas can be seen at my GitHub repository: https://github.com/RubensZimbres/Repo-2018/tree/master/Raspberry%20Pi3%20IoT-Project
Bio: Rubens Zimbres is a Data Scientist, PhD in Business Administration with emphasis in Artificial Intelligence and Cellular Automata. Currently works in Telecommunications, developing Machine Learning, Deep Learning models and IoT solutions for the financial sector and agriculture.
Related:
- Deep Learning on the Edge
- Role of IoT in Education
- Introduction to Trainspotting: Computer Vision, Caltrain, and Predictive Analytics