Sajith Abeywardhana

Connecting a Raspberry Pi with AWS Internet of Things (IoT) Service

IoT – also known as the  ‘Internet of Things’ – is a hot topic these days even though the term has been out there in the industry for quite some time. In this article I will work through a list of sub topics, so that at the end of this article you will be able to connect your first IoT device to the AWS IoT platform with secure connections over TLSv1.2 by using Python.

RPI-thumb-logo
Introduction

IoT – also known as the  ‘Internet of Things’ – is a hot topic these days even though the term has been out there in the industry for quite some time.

In this article I will work through the below list of sub topics, so that at the end of this article you will be able to connect your first IoT device to the AWS IoT platform with secure connections over TLSv1.2 by using Python.

  • The required hardware and software services
  • How to install NOOBS OS on your Raspberry Pi
  • AWS IoT Configurations
  • Setting up your Raspberry Pi to connect with AWS IoT.

 

The required hardware and software services

Hardware –

As I’m going to use the Pi 3 model B v1.2 – which has built-in WiFi capabilities – we don’t need an additional internet connection method. Also, I’ll be connecting my TV to the Pi HDMI output rather than installing an SSH agent in the Pi. If you prefer to install an agent to your Pi device you will not need an output device.

Connecting the hardware

(Image 1 – Connecting the hardware)

Things you will need  (hardware)

  • Raspberry Pi device
    • Raspberry Pi device (I’ll be using Pi 3 model B v1.2 but any Pi will work)
    • Micro SD card
    • HDMI cable
    • HDMI output compatible monitor or a TV
    • Keyboard and mouse
    • 5v output charger.

Things you will need (software services)

  • NOOBS OS.
  • OpenSSL version 1.0.1+ (TLS version 1.2)
  • AWS IoT Service.

 

How to install NOOBS OS in the Raspberry Pi

Before we start – if you have not already downloaded and installed NOOB OS, you may do so here.

For your information – while preparing for this article – I used noobs offline version 2.7.0 and this OS version contains Python version 3.5.3 as well.

After downloading the OS zip file,  extract it to a separate directory and copy the complete file list (as illustrated below – see image 2) to the SD card root.

NOOBS OS file list

(Image 2 – NOOBS OS file list)

Now insert the SD card to your Pi device and boot the device by connecting the charger. You will be prompted to install the OS from the SD card itself. Follow the wizard as it prompts.

I simply explained the steps very briefly. If you require more help please take a look at this video. It explains things in a more detailed manner.

Now we do have a Pi with noobs installed. The Python 3.5.x is also installed in our device as it came with the OS.

 

AWS IoT Configurations

On the AWS IoT platform you will have to do several steps to connect your device to their IoT service, such as creating a thing, and assigning certificates and policies. I have already published an article with guidance to those steps. Please do read and follow the section of “AWS IoT Configurations”.

Note: As explained in the above mentioned article – in this example too – I will be using the same public key, private key, certificate and the same topic which is – “demo-topic”.

 

Setting up Raspberry Pi to connect with AWS IoT

To connect with AWS IoT services, we use the AWS IoT Python SDK which is built on top of the Paho MQTT Python client library. As we are to use an MQTT connection over TLSv1.2 we will use the port as ‘8883’.

Furthermore, make sure that your device meets the below mentioned minimum requirements in your Raspberry Pi in order to progress with using the SDK:

  • Python 2.7+ or Python 3.3+ (With noobs-2.7.0 we also get Python 3.5.3).
  • OpenSSL version 1.0.1+ (With noobs-2.7.0 we also get OpenSSL version 1.1.x).

 

Verifying OpenSSL version
  • To verify, open the Python shell and type the below given commands;
Verifying OpenSSL version

(Image 3 – Verifying OpenSSL version)

 

After the verification we are going to install the AWS Python SDK for our Pi device. You too can download the Python SDK by following this link: https://s3.amazonaws.com/aws-iot-device-sdk-python/aws-iot-device-sdk-python-latest.zip

Unzip the package and install the SDK by running setup.py.

Installing AWS Python SDK

(Image 4 – Installing AWS Python SDK)


sudo python setup.py install
Implementing the publisher.py

Now we are ready to implement our publisher program. Simply follow the below steps to do so:

  • Create a new directory named as demo as a working directory in the /home/pi directory
  • Copy the AWSIoTPythonSDK directory – which can be found in the downloaded SDK – and paste in the demo directory
  • Create a new directory called demo-cert in the demo directory
    • Copy the AWS Root CA certificate to this directory (I renamed it as aws-root-cert.pem)
    • Copy the certificate file which we associated with the thing, to this directory. (I renamed it as iot-cert-.pem.crt)
    • Copy the private key file which we associated with the thing, to this directory. (I renamed it as private-key.pem.key)
  • Open the Python 3.5.3 shell by clicking:  home -> Programming -> Python 3 (IDEL)
  • Create a new file, copy the below source code block to it and save the file by naming it as publisher.py in our working directory which is demodirectory.
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import logging
import time
import argparse
import json

host = "YOUR-THING-END-POINT"
certPath = "/home/pi/demo/demo-cert/"
clientId = "sajith-pi-demo-publisher"
topic = "demo-topic"

# Init AWSIoTMQTTClient
myAWSIoTMQTTClient = None
myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
myAWSIoTMQTTClient.configureEndpoint(host, 8883)
myAWSIoTMQTTClient.configureCredentials("{}aws-root-cert.pem".format(certPath), "{}private-key.pem.key".format(certPath), "{}iot-cert.pem.crt".format(certPath))

# AWSIoTMQTTClient connection configuration
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)
myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
myAWSIoTMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10)  # 10 sec
myAWSIoTMQTTClient.configureMQTTOperationTimeout(5)  # 5 sec
myAWSIoTMQTTClient.connect()

# Publish to the same topic in a loop forever
loopCount = 0
while True:
    message = {}
    message['message'] = "demo-topic-sample-message"
    message['sequence'] = loopCount
    messageJson = json.dumps(message)
    myAWSIoTMQTTClient.publish(topic, messageJson, 1)
    print('Published topic %s: %s\n' % (topic, messageJson))
    loopCount += 1
    time.sleep(10)
myAWSIoTMQTTClient.disconnect()

 

Now the directory will look similar to the below image (image 6).

Working directory

(Image 5 – Working directory)

Executing the publisher and testing

Navigate to the publisher.py file and click the Run -> Run Module menu option. The Python shell output will open when you hit the Run Module button. In this shell output we can see that our JSON formatted message is published to the AWS IoT topic.

Running the publisher and output

(Image 7 -Subscribing via AWS IoT Test service)

Conclusion

In this article we have discussed how to connect Raspberry Pi with AWS IoT over TLSv1.2. Using a sample publisher program which we wrote using Python, we published JSON formatted MQTT messages to the AWS IoT and tested the subscription using AWS IoT Test service.

Thank you for reading this Tech Guide. We hope you will also read our next tutorial so that we can help you solve some more interesting problems.

Sajith Abeywardhana - SSE

Sajith Abeywardhana

Senior Software Engineer | Mitra Innovation