Sunday, September 4, 2016

Internet of Things - First Project - AWS - Lou Person

Internet of Things - First Project - AWS - Lou Person


The goal of this post is to summarize how I built my first IoT project.  Earlier posts showed "Hello World" applications.  Well, this project is a Hello World for IoT.  If you are a Maker, Student, Teacher, Entrepreneur, Hobbyist, etc. and are interested in building this on your own, feel free to contact me. 

By way of background, this whitepaper is helpful in describing AWS IoT (I work for AWS): http://d0.awsstatic.com/whitepapers/core-tenets-of-iot1.pdf

My Project demonstrates how a device can read temperature data from a sensor, send it back to AWS IoT and have actions taken upon the data (rules).  In the case of this post, the IoT rule will text my phone with the temperature from one of the sensors.

First, I built the device with the sensors.  The Grove Indoor Environment Kit for Intel Edison by Seeed Studios was used.  You can buy one here.  The kit contains 11 sensors, an Intel Edison Processor and a base shield where all the sensors connect.

This post is a prototype solution allowing the maker or student to get started with prototyping of their device. 

The kit runs an Intel Edison processor with Linux embedded into it.  The kit is attached to a PC or Mac using 2 USB cables.  The Linux operating system is then logged into via COM ports or SSH over WiFi once the unit is configured.  At first login, the root password is set and the device is configured (given a name and connected to WiFi).  A number of libraries are installed that are prerequisites for the software which is installed on the unit.  On Github in one of the AWS repositories a python script exists.  This script reads the sensors on the board and makes calls to AWS IoT to send the sensor data back. 

An AWS Console login is required.  
Once you login, go to AWS IoT and create a thing type.   Then, create a Device in the thing registry: http://docs.aws.amazon.com/iot/latest/developerguide/create-device.html

Choose the thing type created above.  Hit Create.


Create and activate a device certificate: http://docs.aws.amazon.com/iot/latest/developerguide/create-device-certificate.html

Download the keys.  They will be entered into the device in later steps.

Attach a thing to a certificate: http://docs.aws.amazon.com/iot/latest/developerguide/attach-cert-thing.html

Select the certificate.  Select Actions.  Select Attach a thing.  On Confirm, enter the thing name and hit attach.

Create an AWS IoT Policy: http://docs.aws.amazon.com/iot/latest/developerguide/create-iot-policy.html
Press Add 

Attach an AWS IoT Policy to a Device Certificate: http://docs.aws.amazon.com/iot/latest/developerguide/attach-policy-to-certificate.html
Select the certificate.  Select actions.  Select Attach a policy.

On confirm, enter the thing name and hit attach.
Click on the thing you created and view the detail.  In the detail, look at the REST API endpoint.  You will need the information between the https:// and /things.  For example: xxxxxxxx.iot.xx-xxxx-1.amazonaws.com You will need this in steps below.

Physically connect the boards
Connect the board to the PC
Install the Intel Windows standalone driver from here: https://software.intel.com/en-us/iot/hardware/edison/downloads
Install the FTDI drivers from here: http://www.ftdichip.com/Drivers/D2XX.htm
MacOS 10, no drivers needed
Determine the Serial Port you’ll need to communicate with the Edison in the Device Manager (MacOS, don’t worry.

Connect via Windows
a.       Use PuTTY to connect:
b.      Change the Connection type to “Serial”
c.       Enter the COM port in the “Serial line”
d.      Change the Speed to 115200
Connect via MacOS 10
a.       Open a terminal window
b.      Run ‘ls –l /dev/cu.usbserial*
c.       Using the device found in the command above, launch: ‘screen /dev/cu.usbserial-… 115200

Configure Edison
Open a terminal window
Run ‘ls –l /dev/cu.usbserial*
Using the device found in the command above, launch: ‘screen /dev/cu.usbserial-… 115200
Login as root with no password.  Then run configure_edison --setup
Assign a password
Attach to a WiFi Network
Name your unit

Install the certificates created in IoT Console
Make sure you are in the home directory cd ~
Make a directory called certs, changed into it
mkdir certs; cd certs
Create 2 files and copy and paste the appropriate contents (from when you created the certificates) into each file
nano certificate.pem.cert
nano private.pem.key
Go to the files that were downloaded before, xxxxx-private.pem.key and xxxxx-public.pem.key.  Open them in Wordpad.  Copy the entire file including the -----BEGIN and -----END lines.  Save the text copied from xxxx-private.pem.key into private.pem.key and xxxxx-public.pem.key into certificate.pem.cert
Ctrl-O to WriteOut, then Ctrl-X to exit
Download the latest version of the root certificate
wget https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem
Rename this file to CAroot.pem
mv VeriSign-Class\ 3-Public-Primary-Certification-Authority-G5.pem rootCA.pem

Install the required code and libraries     
Make a directory called code. mkdir code
Change into the directory.  cd code  
Use git to download the AWS IoT SDK for Python
git clone https://github.com/aws/aws-iot-device-sdk-python.git   
Change into the newly downloaded directory
cd aws-iot-device-sdk-python
Install the newly created code
python setup.py install
Run package updates on the image from the server, enter these two commands:
a.      Opkg update
b.      Opkg upgrade

Install MRAA and UPM
The MRAA library is used for low-level communications with the input/output hardware on the Intel Edison device
The UPM library is used for higher-level communications (on top of MRAA) with various sensor devices
More information on the libraries is available here:
Make sure you’re back in the code directory
cd ~/code
Download the MRAA library
git clone https://github.com/intel-iot-devkit/mraa.git
Change into the newly downloaded MRAA directory
cd mraa
Make and install the MRAA libraries
mkdir build && cd build && cmake ..
make && make install
Now Install UPM.  Make sure you’re back in the code directory
cd ~/code
Download the UPM library
git clone https://github.com/intel-iot-devkit/upm.git
Change into the newly downloaded UPM directory
cd upm
Make and install the UPM libraries
mkdir build && cd build && cmake ..
make && make install
Install the application code to read the temperature from the sensor. 
cd ~/code; wget https://s3.amazonaws.com/gasworkshop/sendTempHumid.py
Edit the file sendTempHumid.py and change the name to your thing

Run the script:
python sendTempHumid.py -e xxxxxxxxxxxxx.iot.xx-xxxx-1.amazonaws.com -r ~/certs/rootCA.pem -c ~/certs/certificate.pem.crt -k ~/certs/private.pem.key

Note: the value xxxxxxxxxxxxx.iot.xx-xxxx-1.amazonaws.com came from the REST API Endpoint as described above
Create a rule to send the results to your cell phone
Click on create a resource.  Click Create a rule.
Give it a name and description.
For attributes, for the purpose of the lab, enter * to pick up all attributes.
For the topic filter, enter # to pick up all topics.
Leave the condition blank for the purpose of the lab.
For action, choose Send message as a push notification (SNS) 

If you don’t already have an SNS target:
Click create new resource to go to the SNS Console.
Click Create new topic.
Give the topic a name and description.  Click create topic.
Click the topic and select “Subscribe to topic” from the actions.
Change the protocol to SMS and enter your phone number.
Click create subscription
Hit refresh next to the SNS target.
Select the target you created and want to use.
Click create new role, give it a name, and hit create.
Click add action and then create.
Run the code again and you should get a text message to your phone with the temperature and humidity.

Post by Lou Person, I work at Amazon Web Services.