Home » Feature Story » Apps » Using the Intel NUC and Ubuntu to Build a Cloud-Connected Sensor Application
Using the Intel NUC and Ubuntu to Build a Cloud-Connected Sensor Application
By
Promotion |
Updated on 19-Sep-2017
Introduction
This paper will show you how to install Ubuntu* Linux* on an Intel® NUC and use it to build a modular IoT application with cloud connectivity. You'll see how to read real-time sensor data from a temperature and humidity sensor, view it locally on the Intel® NUC, and transmit it to Amazon Web Services* IoT. AWS IoT is used here as an example cloud back-end, where data can be integrated with other cloud applications. You'll build and deploy the application as a snap package — a universal Linux package format that makes it easy to deploy and manage Linux applications running on devices such as the Intel® NUC.
Setup and Prerequisites
- Intel® NUC connected to a LAN network with Internet connectivity.
- Computer keyboard and monitor connected to the Intel® NUC (needed for Ubuntu install).
- RH-USB temperature and humidity sensor.
- A developer workstation or laptop.
- An active Amazon Web Services* IoT account.
Installing Ubuntu* on the Intel® NUC
We'll use Ubuntu Server 16.04 to support both application development and run-time deployment on the Intel® NUC. Make sure the BIOS has been updated to the latest version and configure the BIOS for Linux operating system, with the internal eMMC drive enabled.
Follow the Alternative install: Ubuntu Server 16.04 LTS installation instructions to download an ubuntu-server-16.04-XXXXXX.iso image file and install it on the Intel® NUC. The installation process involves loading the image file onto a USB drive, plugging the USB drive into the Intel® NUC, booting from the USB drive, and installing Ubuntu into the /dev/mmcblk0 internal flash drive. These operations are done using a temporary computer monitor and USB keyboard connected to the Intel® NUC. During the installation process you'll be asked to set a hostname and username/password for Ubuntu. You can set them to your own preferences, however for this example we will be using ubuntu for the hostname, ubuntu for the username, and ubuntu for the password.
Once the operating system is installed, remove the installation USB drive and reboot the Intel® NUC so it boots Ubuntu from the internal eMMC drive. Log into the Intel® NUC as the ubuntu user using either the keyboard and monitor, or by logging in from a developer workstation over the network using ssh.
Update the base operating system by running the following commands.
$ sudo apt update
$ sudo apt -y upgrade
$ sudo reboot
Allow the Intel® NUC to reboot.
Connect RH-USB Sensor
The RH-USB sensor is an industrial temperature and humidity sensor with a serial interface that connects via USB. Plug the RH-USB connector into the USB jack on the front of the Intel® NUC. After that's done, Ubuntu will create a serial tty device named /dev/ttyUSB0. Confirm that the device was created by running the following command and inspecting the output (the dash option is the lowercase letter L).
$ ls -l /dev/ttyUSB*
crw-rw—- 1 root dialout 188, 0 May 10 12:37 /dev/ttyUSB0
Give the ubuntu user read/write access to the sensor's serial device by running the following command:
1$ sudo usermod -a -G dialout ubuntu
You must log out and log back in as the ubuntu user to pick up the permission change.
We'll use the screen command to verify the sensor is working. screen is a terminal program that allows you to type characters and view responses. Install and run screen using the following commands:
$ sudo apt install -y screen
$ screen /dev/ttyUSB0 9600
After screen has started, press Enter and you should see the >' character appear. That is the RH-USB command prompt. Type the letters PA and press Enter. You won't see the letters PA but you should see a set of digits like the following.
>50.4,72.8
>
The digits are the humidity (%) and temperature (degrees F) readings from the RH-USB, separated by a comma.
Exit the screen program by typing Control-A followed by backslash and then typing y to confirm exiting screen.
Creating a Snap
Snaps are self-contained Linux packages that facilitate deploying complete applications on Linux systems such as Ubuntu. They can contain application programs along with prerequisite commands and libraries needed by the application. This alleviates the need to manually install prerequisite packages on target systems in order to run the application. Snaps also make it easier to check for and deploy new versions of applications as well as rollback to earlier versions if there is a problem.
We'll create our sensor application as a self-contained snap package. Snaps can be built on a separate development computer and then deployed on a target computer, or can be built and deployed on the same computer which is how we'll do it on the Intel® NUC.
Install the snap build tools using the following command:
1$ sudo apt install -y snapcraft build-essential git
Snapcraft can be used to create and initialize a new snap, however we'll clone an existing application repository and use that to initialize our files. The application is written in Node.js* and uses the AWS* IoT Device SDK for JavaScript* to provide functions for securely transmitting data to AWS IoT.
$ cd ~
$ git clone https://github.com/gregtoth/snap-rhaws.git
snap-rhaws source code on GitHub.
The name of our snap is rhaws and the source files making up the application are:
- snap/snapcraft.yaml – defines the overall Snap package.
- src/package.json – identifies required Node.js support libraries.
- src/rhaws.js – the Node.js application that reads the sensor and sends data to the cloud.
The rhaws.js application implements the following functions:
- Initialize the RH-USB sensor serial port and load AWS IoT security credentials.
- Periodically send a PA command to the RH-USB to read temperature and humidity.
- Read and parse the temperature and humidity values returned by the RH-USB.
- Generate a JSON payload message containing time and raw data and transmit it to AWS IoT
Build the snap using the snapcraft command:
$ cd ~/snap-rhaws
$ snapcraft
The build process downloads and assembles prerequisite components including Node.js and NPM packages listed in package.json. It then builds a snap package and puts the result in file rhaws_1.0.0_amd64.snap. The snap file is a self-contained package containing Node.js, dependent libraries, and our sensor application in a distribution package that's ready to deploy to the Intel® NUC.
Configure and Load AWS* IoT Keys
Before we can connect to AWS IoT, we must configure access keys and load them on the Intel® NUC. The access keys are not stored directly in the application or the snap package for security reasons, and because they will be different for each user.
Log into the AWS IoT cloud console from your developer workstation and navigate to Security > Certificates. Create a new certificate and download the certificate file, private key file and root CA file. Activate the certificate and attach a policy that allows (at a minimum) connecting to AWS IoT and publishing to the 'nuc/temperature' topic.
On the Intel® NUC create a directory that will hold the AWS IoT keys, use the following commands.
$ cd ~
$ mkdir aws-iot
$ chmod 755 aws-iot
Copy the downloaded certificate file, the private key file and root CA file from your developer workstation to the Intel® NUC, using a tool such as sftp, and place them in the /home/ubuntu/aws-iot directory on the Intel® NUC. Rename the files with the following filenames, so it will match what's in the rhaws.js application program:
- Certificate: mynuc-certificate.pem.crt
- Private key: mynuc-private.pem.key
- Root CA: rootca.pem Adjust the access permissions on the files using this command.
1$ chmod 644 /home/ubuntu/aws-iot/*
Install and Test the Snap
Install the snap we built earlier by using the snap install command:
$ cd ~/snap-rhaws
$ sudo snap install –devmode rhaws_1.0.0_amd64.snap
Installed snaps are listed using the snap list command:
$ snap list
Name Version Rev Developer Notes
core 16-2 1689 canonical –
rhaws 1.0.0 x1 devmode
The snap is configured to run as a service so it will automatically start running after it's installed, as well as when the Intel® NUC is rebooted. You can check the run state of the rhaws snap using the systemctl command:
1$ systemctl status -l snap.rhaws.rhaws.service
In the AWS IoT cloud console navigate to the Test function and create a subscription to the nuc/temperature topic. You should see messages being received every 3 seconds containing a JSON object with time, temperature and humidity values.
On the Intel® NUC you can view the sensor data and transmission status by monitoring the log file where output from the rhaws.js program is written. Snaps run under systemd on Ubuntu and log output goes to the systemd log. Use the journalctl command to view systemd log messages; the -f option tails the log to display new entries. Type Control-C to stop viewing log output. The logs are useful for troubleshooting application problems.
$ journalctl -u snap.rhaws.rhaws.service -f
May 23 15:07:19 ubuntu snap[12602]: RH-USB: 50.1,73.6
May 23 15:07:19 ubuntu snap[12602]: Publish nuc/temperature: {"time":"2017-05-23T19:07:19.672Z","temperature":"73.6","humidity":"50.1"}
May 23 15:07:22 ubuntu snap[12602]: RH-USB: 50.1,73.6
May 23 15:07:22 ubuntu snap[12602]: Publish nuc/temperature: {"time":"2017-05-23T19:07:22.695Z","temperature":"73.6","humidity":"50.1"}
The rhaws application can be manually stopped and started using the command.
$ sudo systemctl stop snap.rhaws.rhaws.service
$ sudo systemctl start snap.rhaws.rhaws.service
Remove the Snap
Use the snap remove command to stop the snap and remove it from the system.
$ sudo snap remove rhaws
rhaws removed
$ snap list
Name Version Rev Developer Notes
core 16-2 1689 canonical –
Where to Go From Here
This application provides the basic foundation for creating and running a sensor application on the Intel® NUC as a snap package under Ubuntu. It continuously reads temperature and humidity data from the sensor and transmits the data to AWS IoT in the cloud. Once the data is in the cloud it can be further processed and stored by other applications, manipulated via rules, or visualized using a number of different tools.
We developed the rhaws snap package in developer mode, which allows us to create and deploy the snap without having to sign it and to publish it to the Ubuntu Store. Snaps can be signed and published so that other people can find and use them without having to build the snap themselves. Publishing snaps involves creating an account on the Ubuntu Store, creating a signing key, signing the snap, and uploading it to the store. Developer mode also relaxes package security constraints that are typically tightened before a snap is published to the store.
Snaps can also be deployed to an Intel® NUC running Ubuntu Core, which is a lighter-weight version of Ubuntu. In order to deploy to an Intel® NUC, the installed Ubuntu Core image must contain support for accessing and using the USB serial port through which the RH-USB communicates.
For more such intel IoT resources and tools from Intel, please visit the Intel® Developer Zone
Source: https://software.intel.com/en-us/articles/using-the-intel-nuc-and-ubuntu-to-build-a-cloud-connected-sensor-application