SIMPLE AWS IoT CONNECTION
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
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.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
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.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.h_range_index += 1
self.h_range_index = 0
self.t_range_index += 1
self.t_range_index = 0