Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

SIMPLE AWS IoT CONNECTION

System Implementation:
A connection between a pseudo-sensor (implemented as a python class) and the AWS
infrastructure is established. The following steps were followed:
• A thing was created in the IoT Core framework.
• Paho MQTT was used to connect the data server (Temperature and humidity data
from pseudo-sensor) to the AWS IoT core
• Data was received at the MQTT Test Client interface under specific topics.
• Data from the client was sent to 2 queues – one containing temperature data and the
other humidity, by defining rules.
• The data from the queues were processed and presented on the command line using
boto3 and AWS CLI.

Implementation/Assumption Notes:
1. Problem:
While preparing the python server to send the pseudo-server data to AWS IoT, I
encountered an issue with on_connect prompt. The on_connect did not change the status
of connflag.
Solution:
I resolved the issue by correct the port number (8883) through which AWS will
communicate. It was not open in the Windows Firewall.

2. The MQTT Test Client on the AWS IoT Console reset every time it was closed.

3. The rule that instructed the IoT thing to send data to the SQS required a process of
granting access to AWS which was a bit tedious. It had to be carried out in the IAM
portal rather than the regular root key console.
CODE
send.py – used to send data to the IoT Thing, and its counterpart rec.py
Note: While sending data from pseudo-sensor to the Thing, the topic argument of the
publish command is changed to the respective topics declared on the MQTT Client on
AWS.
import paho.mqtt.client as paho
import os
import socket
import ssl
from time import sleep
from random import uniform
from psuedoSensor import PseudoSensor

connflag = False

def on_connect(client, userdata, flags, rc):


global connflag
connflag = True
print("Connection returned result: " + str(rc) )

def on_message(client, userdata, msg):


print(msg.topic+" "+str(msg.payload))

mqttc = paho.Client()
mqttc.on_connect = on_connect
mqttc.on_message = on_message
#mqttc.on_log = on_log

awshost = "data.iot.ap-south-1.amazonaws.com"
awsport = 8883
clientId = "Project"
thingName = "Project"
caPath = "aws-iot-rootCA.crt"
certPath = "cert.pem"
keyPath = "privkey.pem"

mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath,


cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None)

mqttc.connect(awshost, awsport, keepalive=60)

mqttc.loop_start()

while 1==1:
sleep(0.5)
if connflag == True:
ps = PseudoSensor()
h,t = ps.generate_values()
mqttc.publish("Temperature", t, qos=1)
print("msg sent: temperature " + "%.2f" % t )
mqttc.publish("Humidity", h, qos=1)
print("msg sent: humidity " + "%.2f" % h )
else:
print("waiting for connection...")
rec.py – counter part of send.py which subscribes and shows all the data that send.py
has sent
import paho.mqtt.client as paho
import os
import socket
import ssl

def on_connect(client, userdata, flags, rc):


print("Connection returned result: " + str(rc) )
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("#" , 1 )

def on_message(client, userdata, msg):


print("topic: "+msg.topic)
print("payload: "+str(msg.payload))

mqttc = paho.Client()
mqttc.on_connect = on_connect
mqttc.on_message = on_message
#mqttc.on_log = on_log

awshost = "data.iot.ap-south-1.amazonaws.com"
awsport = 8883
clientId = "Project"
thingName = "Project"
caPath = "aws-iot-rootCA.crt"
certPath = "cert.pem"
keyPath = "privkey.pem"

mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath,


cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None)

mqttc.connect(awshost, awsport, keepalive=60)

mqttc.loop_forever()
psuedoSensor.py – generates the temperature and humidity data to be sent to the Thing
and eventually to the SQS queue
import random

class PseudoSensor:

h_range = [0, 20, 20, 40, 40, 60, 60, 80, 80, 90, 70, 70, 50, 50, 30,
30, 10, 10]

t_range = [-20, -10, 0, 10, 30, 50, 70, 80, 90, 80, 60, 40, 20, 10, 0,
-10]

h_range_index = 0

t_range_index = 0

humVal = 0

tempVal = 0

def __init__(self):

self.humVal = self.h_range[self.h_range_index]

self.tempVal = self.t_range[self.t_range_index]

def generate_values(self):

self.humVal = self.h_range[self.h_range_index] + random.uniform(0,


10);

self.tempVal = self.t_range[self.t_range_index] + random.uniform(0,


10);

self.h_range_index += 1

if self.h_range_index > len(self.h_range) - 1:

self.h_range_index = 0

self.t_range_index += 1

if self.t_range_index > len(self.t_range) - 1:

self.t_range_index = 0

return self.humVal, self.tempVal


boto.py – processes and retrieves data from 2 SQS queues and prints the data
# Get the service resource
import boto3
sqs = boto3.resource('sqs')

# Get the queue. This returns an SQS.Queue instance


queue1 = sqs.get_queue_by_name(QueueName='humid')

for message in queue1.receive_messages():


print('Humidity data: ',message.body)
queue2 = sqs.get_queue_by_name(QueueName='send_msg')

for message in queue2.receive_messages():


print('Temperature data: ',message.body)
RESULTS
The data server tracing its connection to AWS:
Result ‘0’ means the connection was successful
MQTT Test Client receiving data from the server:
Two queues – humid and send_msg receiving data from the MQTT test client:
Printing data from the queues on the terminal using boto3 SQS:

You might also like