You are on page 1of 15

Exp 8: Integration of IoT Components in AWS/Azure

Install AWS Amazon SDK on Raspberry Pi 3 and Test Connection


(Publish - Subscribe IoT Architecture)

Aim:
To integrate AWS IoT Components in EC2. EC2 receives and stores environmental
temperature for further processing.

Procedure:
In order to use amazon cloud services on Raspberry Pi, we need to install aws amazon
sdk on Raspberry Pi (Raspbian stretch OS).

Steps to install aws amazon sdk om Raspberry Pi 3 and Test connection:


Step 1:

i) Sign up on AWS Amazon website and (login from Raspberry Pi)


ii) Go to: Services > AWS IoT > Onboard > Get Started

iii) Choose get started under onboard menu

iv) After click on get started, connect to AWS IoT window will appear
v) Choose Platform: Linux/OSX

vi) Choose a AWS IoT Device SDK: Python


vii) Set any name as a thing name (we will refer it as <Thing_Name>). Here given as RPI
viii) Download connect kit on Raspberry Pi

ix) After downloading the connection kit , copy those three files into into Rasperry pi
machine through WinSCP emulator.
x) Now log into Rasperry pi machine by giving user name and password

2)       Install:
  Unzip the downloaded file. You should have following files:
a.       <Thing_Name,>.cert.pem
b.      <Thing_Name>.private.key
c.       <Thing_Name>.public.key
d.      start.sh
                    Put all the unzipped files in one folder.

i) After downloading SDK, Click on Next Step

ii) Check whether all the files are present in the Rasperrypi such as private.key, public.
Key, root-CA.crt as mentioned in below figure.
iii) Ensure all files exist in the Rasperry Pi

iv) Check whether Python SDK is Installed


v) To Install MQTT

vi) Collecting paho-mqtt


Downloading
https://files.pythonhosted.org/packages/2a/5f/cf14b8f9f8ed1891cda893a2a7d1d6fa23de2
a9fb4832f05cef02b79d01f/paho-mqtt-1.3.1.tar.gz (80kB)
100% |████████████████████████████████| 81kB 501kB/s
Building wheels for collected packages: paho-mqtt
Running setup.py bdist_wheel for paho-mqtt ... done
Stored in directory:
/root/.cache/pip/wheels/38/ca/67/86c7e4acc659ce5ab74cbb8cc38de50c90ed4f827133e36
994
Successfully built paho-mqtt
Installing collected packages: paho-mqtt
Successfully installed paho-mqtt-1.3.1
To Install AWSIOTPythonSDK, enter given below command

pi@raspberrypi:~/RPI $ pip install AWSIoTPythonSDK

pi@raspberrypi:~/RPI $ sudo apt-get update

Hit http://mirrordirector.raspbian.org jessie InRelease


Ign http://cz.archive.ubuntu.com saucy InRelease
Ign http://cz.archive.ubuntu.com saucy Release.gpg
Ign http://cz.archive.ubuntu.com saucy Release
Hit http://archive.raspberrypi.org jessie InRelease
Hit http://mirrordirector.raspbian.org jessie/main armhf Packages
Hit http://mirrordirector.raspbian.org jessie/contrib armhf Packages
Hit http://mirrordirector.raspbian.org jessie/non-free armhf Packages
Hit http://archive.raspberrypi.org jessie/main armhf Packages
Hit http://mirrordirector.raspbian.org jessie/rpi armhf Packages
Hit http://archive.raspberrypi.org jessie/ui armhf Packages
Err http://cz.archive.ubuntu.com saucy/main armhf Packages
404 Not Found [IP: 217.31.202.63 80]
Err http://cz.archive.ubuntu.com saucy/universe armhf Packages
404 Not Found [IP: 217.31.202.63 80]
Ign http://cz.archive.ubuntu.com saucy/main Translation-en_GB
Ign http://cz.archive.ubuntu.com saucy/main Translation-en
Ign http://cz.archive.ubuntu.com saucy/universe Translation-en_GB
Ign http://cz.archive.ubuntu.com saucy/universe Translation-en
Ign http://archive.raspberrypi.org jessie/main Translation-en_GB
Ign http://archive.raspberrypi.org jessie/main Translation-en
Ign http://archive.raspberrypi.org jessie/ui Translation-en_GB
Ign http://archive.raspberrypi.org jessie/ui Translation-en
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en
Ign http://mirrordirector.raspbian.org jessie/main Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/main Translation-en
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en
W: Failed to fetch http://cz.archive.ubuntu.com/ubuntu/dists/saucy/main/binary-armhf/
Packages 404 Not Found [IP: 217.31.202.63 80]
W: Failed to fetch http://cz.archive.ubuntu.com/ubuntu/dists/saucy/universe/binary-
armhf/Packages 404 Not Found [IP: 217.31.202.63 80]
E: Some index files failed to download. They have been ignored, or old ones used
instead.

pi@raspberrypi:~/RPI $

pi@raspberrypi:~/RPI $ pip install AWSIoTPythonSDK

pi@raspberrypi:~/RPI $ sudo pip install AWSIoTPythonSDK


Collecting AWSIoTPythonSDK
Downloading
https://files.pythonhosted.org/packages/e4/af/2cc14c4c9a0e1397f0f6c571277f997936f88
c2e2c6a3a388640a200f829/AWSIoTPythonSDK-1.3.1.tar.gz (70kB)
100% |████████████████████████████████| 71kB 386kB/s
Building wheels for collected packages: AWSIoTPythonSDK
Running setup.py bdist_wheel for AWSIoTPythonSDK ... done
Stored in directory:
/root/.cache/pip/wheels/bb/15/55/cb48293eb01350e3474c8cd830fd0efd1f0e1c1874deb3a
40e
Successfully built AWSIoTPythonSDK
Installing collected packages: AWSIoTPythonSDK
Successfully installed AWSIoTPythonSDK-1.3.1
You are using pip version 9.0.1, however version 10.0.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command

pi@raspberrypi:~/RPI $ sudo ./start.sh

Running pub/sub sample application...


2018-04-18 14:52:02,048 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO -
Configuring MQTT operation time out: 5.000000 sec
2018-04-18 14:52:02,049 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO -
Performing sync connect...
2018-04-18 14:52:02,049 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO -
Performing async connect...
2018-04-18 14:52:08,368 - AWSIoTPythonSDK.core.protocol.internal.workers -
DEBUG - Dispatching [connack] event
2018-04-18 14:52:11,013 - AWSIoTPythonSDK.core.protocol.internal.workers -
DEBUG - Dispatching [message] event
Received a new message:
{"message": "Hello World!", "sequence": 0}
from topic:
sdk/test/Python
Click on Manage- Things.

Click on SecureCertificates Create Certificates

Create a certificate
pi@raspberrypi:~/RPI $ vim aws_iot_sub.py
-bash: vim: command not found
pi@raspberrypi:~/RPI $ nano aws_iot_sub.py
pi@raspberrypi:~/RPI $
pi@raspberrypi:~/RPI $
pi@raspberrypi:~/RPI $
pi@raspberrypi:~/RPI $

pi@raspberrypi:~/RPI $ python aws


aws-iot-device-sdk-python/ aws_iot_pub.py aws_iot_sub.py

pi@raspberrypi:~/RPI $ python aws_iot_pub.py


Traceback (most recent call last):
File "aws_iot_pub.py", line 69, in <module>
mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath,
cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None) # pass parameters
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 694, in tls_set
context.load_cert_chain(certfile, keyfile)
IOError: [Errno 2] No such file or directory
pi@raspberrypi:~/RPI $ nano aws_iot_pub.py

pi@raspberrypi:~/RPI $ nano aws_iot_sub.py


pi@raspberrypi:~/RPI $
pi@raspberrypi:~/RPI $
pi@raspberrypi:~/RPI $
pi@raspberrypi:~/RPI $
pi@raspberrypi:~/RPI $ python aws_iot_pub.py
Connected to AWS
Connection returned result: 0
msg sent: temperature 21.13
msg sent: temperature 22.09
msg sent: temperature 24.77
msg sent: temperature 23.30
msg sent: temperature 23.21
msg sent: temperature 24.48
msg sent: temperature 21.17
msg sent: temperature 24.98
msg sent: temperature 20.59
msg sent: temperature 21.69
msg sent: temperature 23.44
msg sent: temperature 21.51
msg sent: temperature 21.97
File "aws_iot_pub.py", line 76, in <module>
sleep(5)
KeyboardInterrupt
pi@raspberrypi:~/RPI $ nano aws_iot_pub.py
pi@

Result:
Thus the AWS IoT Components have been integrated with EC2 .

You might also like