Amazon AWS IOT can be connected with using MQTT protocol without using SDK. Here I have connected the Amazon AWS IOT with MQTT protocol using python language. This article is divided into two sections as below:
- Amazon AWS IOT configuration
- Python Configuration
Go to AWS IoT console and go to security
then certificates
:
Press Create button located right hand top side. It will travels through "Create A certificate
” screen, select create certificate option:
Download all 4 certifications and keys:
A certificate for the thing: downloaded the file xxxxxxxxxx-certificate.pem.crt.
A public key: downloaded the file xxxxxxxxx-public.pem.key
.
A private key: downloaded the file xxxxxxxxxxxx-private.pem.key
.
A root CA for AWS IoT: downloaded the file and renamed to root-CA.crt
.
Go to Security and select option “Create
”. Select a name for the policy:
In Add Statements > Actions, select whatever actions you would like to support or Add:
I have selected iot.*
to support all kind of requests. In Resource ARN, just add “*
”. Then Press “Create
”. A new Policy will be created.
Go to Registry, Select things. Press Create Button from Top right. Give a name for the thing and press “Create Thing” button. After creating the thing, it will look as below:
Go to Security > Certificate and open the certificate which we have created. Select Policy sub menu and press Actions button located in top right side of security tab. Select “Attach Policy” and select the created Policy. Then Select “Attach Thing” and select the created Thing
Python ConfigurationPython Code for Connecting AWS IOT with MQTT:Before starting the python coding, install OpenSSL light library for windows from below link: http://slproweb.com/products/Win32OpenSSL.html
Install the Mqtt client with PIP:
pip install paho-mqtt
Import required libraries:
Create a call back function to show the connection status:
Create Another call back function to show the subscribe status:
Create another call back function to show the subscribed message:
Create a client for Mqtt
and assign the created call backs:
Configure network encryption and authentication options with downloaded certificates from AWS IOT:
Connect the Mqtt
client to Amazon AWS IoT, the default port 8883 is used:
Then subscribe a topic from AWS IOT (after connection, I have added 5 second delay to avoid race conditions – I have read somewhere this delay should be added):
Then I have created a payload to send to Amazon AWS (My thing name is “WindowsPC”:
Then start the mqtt
client and also publish your payload for every 0.5 seconds:
Then start your client with python (File name: Mqtt_Demo.py
):
Go to Amazon AWS and open the thing and select “Activity”
, you will find the activity keep updating as below:
If you need to send information to windows MQTT client, go to test in AWS IOT main menu:
In Publish submenu at bottom, copy your subscribed topic from your Python source and paste on the text box and press “Publish to topic”. You will see the message located below the text box:
Your command prompt will show the text as:
Source: https://github.com/Saas-Auto/IOT_Projects/tree/master/AmazonAWS_IOT_Python/Source
Comments