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.
By Rubens Zimbres, Data Scientist
The frontiers between software and hardware are fascinating. We, Data Scientists, are used to develop Machine Learning algorithms for business purposes, and frequently the outputs of these models are measured in terms of profit, cost savings and even intangible outputs, like customer satisfaction. Of course we can visualize the outputs of our models using dashboards like Power BI, Tableau or in the cloud, with Kibana, Quick Sight or Tago. But it’s quite rewarding to see outcomes of our models in the physical world, outside a screen.
I’ve been working with IoT for finance and telecommunications. One can easily generate alerts in dashboards regarding malfunctions in a given system, like saturation of BTSs (base transceiver station), the nodes where telecommunications signals pass through. Now imagine that besides of an alert in a dashboard, we automatically redirect telecom data traffic to a different anthem so that Point-of-Sales (POS) remain connected all the time. This would leverage acquirers’ profit and merchants’ satisfaction. We may even adjust settings of a machine in the real world.
In IoT projects we work close to hardware. Devices like POS, power and water meters, cane grinders, Libelium sets (for agriculture), elevators, health appliances send telemetry data that can be analyzed in order to develop Predictive Maintenance models, so that businesses can cut costs and increasy efficiency, at the same time they optimize logistics. In these cases, connectivity can be achieved using SimCards, Wi-Fi, Bluetooth or radio signals (LoRa).
One of the parts of the IoT solution is the gateway. It intermediates the communication between the device (temperature, humidity sensor) and the cloud. In my previous post I explained how to turn your notebook into an IoT device, providing details on how to send data to AWS, from the device to the dashboard, in an end-to-end solution (kdnuggets.com/2018/06/zimbres-iot-aws-machine-learning-dashboard.html).
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. An easier way is to use Raspbian, that provides a graphical interface for your projects. Raspbian is a Debian-based computer operating system. However, I decided to try Ubuntu Core, given that security should be a main concern in IoT. Ubuntu Core can be used for digital signage, robotics, drones and edge gateways. A more challenging and secure decision.
The picture below shows the difference between regular Linux systems and Ubuntu Core:
Ubuntu Core is a lightweight system, with “security at its heart”. It is composed of snaps, “self contained, isolated and protected bits of code”. It’s quite difficult to alter a read-only file in the system, even using ESC :w !sudo tee % at vim command.
As it is an ARM system, it’s quite different to install Machine Learning libraries in it, since there are some specific packages for this type of system and lots of dependencies are not included, not even “wget”. So let’s start. When you buy a Raspberry, it comes without the energy source (top right below) and without the SDCard (prominence at right). Model 3 B comes with 4 USB entries and an ethernet entry. It also contains the HDMI, audio and camera entries. In this project we will not use the mouse, and monitor, ethernet cable and keyboard are only used in the first run of the system, to set up the internet connection.
The first thing to do is to install an Operating System in the SDCard (micro). So, download Ubuntu Core image from their website at http://cdimage.ubuntu.com/ubuntu-core/16/stable/current/ubuntu-core-16-pi3.img.xz
Then, download Etcher in order to flash the image into the card: https://etcher.io/
Now, you have to generate the private and public keys, that will be uploaded to your Ubuntu One account (https://login.ubuntu.com/).
Then, copy the key code and paste into your Ubuntu One account:
Insert the card into the Raspberry and turn on the switch of the energy source. Voilà, the system will start:
This will take a couple of seconds in the first time it runs, and you will be directed to the network configuration. Configure DHCP and your Wi-Fi username and password. Then Ubuntu Core will provide you with the address you are supposed to ssh.
Just in case, be sure that port 22 (used by ssh) is open:
Now, we will copy the keys to a new directory in our client computer, .ssh:
We are ready to connect to our Raspberry via ssh. Run:
The -vvv command will bring you details about the ssh connection. When connecting, a window will open asking for your password. You are in and the login will change:
At this point, you can disconnect your monitor, keyboard and ethernet cable, as the communication will be done via Wi-Fi and the whole project will happen via command line. Now let’s install snap classic, a container where we will install Machine Learning libraries:
At this point, you can go back to my previous article where I measure CPU temperature and send to Amazon AWS (kdnuggets.com/2018/06/zimbres-iot-aws-machine-learning-dashboard.html), or we can move on:
In Ubuntu Core, is not that simple to generate and edit notebooks. You cannot open Jupyter, Spyder or even Notepad and create a new file, as there is no graphical interface. You have to use the command “touch” to create a file:
After creating the new file, you can open and edit the file using “vim” command and paste the code:
Type “i” then CTRL+SHIFT+V the code at: https://github.com/RubensZimbres/Repo-2018/blob/master/CPU%20Temperature%20-%20IoT%20Project/AWS_Send_test.py
After that, type:
This saves the file. Remember to copy rootCA.pem + *.crt + *private.pem.key + *.public.pem.key to your Raspberry to properly access AWS IoT Core.
Run the following command to send Raspberry CPU Temperature to AWS:
You will check your connections via IoT Core dashboard:
It’s important to notice that you are running Python at Ubuntu classic environment, at chroot. So, it will be necessary to run “sudo python” to access GPIOs (general-purpose input/output) in Raspberry.