Skip to content

Publish and Subscribe to Data using AWS IoT and Python3

License

Notifications You must be signed in to change notification settings

anujdutt9/AWS_IoT_Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

AWS_IoT_Python

This repository contains the steps and the code to Access the AWS IoT service using Python3, Paho-MQTT on Raspberry Pi3. Note: To make this code work for Windows, Linux/Ubuntu, just remove the RPi imports and GPIO's and insert the Interface I/O's according to your OS.

Requirements:

a) Python 3.4 or above [https://www.python.org/downloads/] (https://www.python.org/downloads/)

b) Paho-MQTT

"pip install paho-mqtt"	

c) SSL

"sudo apt-get install libssl-dev"

Reference:

AWS IoT Tutorial [http://docs.aws.amazon.com/iot/latest/developerguide/iot-device-sdk-c.html] (http://docs.aws.amazon.com/iot/latest/developerguide/iot-device-sdk-c.html)

Steps for Setup:

  1. Go to Amazon IoT website [https://aws.amazon.com/iot/] (https://aws.amazon.com/iot/)
  2. Follow this link https://aws.amazon.com/iot/. Click on "Get started with AWS IoT" and "Sign In" with your Amazon Account.
  3. Click on Create a Resource.
  4. Choose Create a thing, type a name for the thing, and then choose Create. The thing is a shadow of the Hardware device that you want to connect to the AWS IoT service.
  5. In addition to a confirmation message, the View thing button will be displayed.
  6. Choose View thing to display information about your "thing".
  7. Choose the Connect a device button on the side panel to download a key pair and a certificate generated by AWS IoT.
  8. On the Connect a device page, select the SDK to use, and then choose Generate certificate and policy. Choose Embedded C here.
  9. This will generate an X.509 certificate and key pair; activate the X.509 certificate; and create an AWS IoT policy and attach it to the certificate.
  10. Next you will get to see the Download Links for the certificates and the keys. Download them all.
  11. Choose Confirm & start connecting.
  12. On the next page you will get a sample format of the code. Save that in a text file for the reference.

Download the Code:

You can either clone this repository using git clone:

git clone https://github.com/anujdutt9/AWS_IoT_Python.git

or download it as a ".zip" file.

Running the Code:

The example code in the repository is for Publisher and Subscriber

The Publisher and Subscriber folder contains AWS_IoT_pub.py file that publishes(sends) the data to the AWS IoT service and AWS_IoT_sub.py that subscribes to the service and shows(get) the data from the AWS IoT on the second Terminal. Currently, AWS_IoT_sub.py subscribes to any topic and will show all of the received msgs but we can customize it to get a perticular message.
The publisher code reads the state of the pins of Raspberry Pi3 (change Pin Numbers for other Raspberry Pi Boards) and sends this to the AWS IoT updating the state as its changed. For this, we have connected some LED's to the pins of the Raspberry Pi that represent the state of the pins.
If the LED's glow, it's state is '1' else '0' (You can make changes in the code as you want).

The subscriber code fetches the state of the LED's from AWS IoT and prints it on the second terminal.

To Test the sample Python Programs:

i) In one terminal, run the AWS_IoT_pub.py code using

python3 AWS_IoT_pub.py

ii) In second terminal, run the AWS_IoT_sub.py code using

python3 AWS_IoT_sub.py

Follow the same steps for the Raspberry Pi code.

You can check the sources and modify the topics used by both the programs.