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.
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"
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)
- Go to Amazon IoT website [https://aws.amazon.com/iot/] (https://aws.amazon.com/iot/)
- Follow this link https://aws.amazon.com/iot/. Click on "Get started with AWS IoT" and "Sign In" with your Amazon Account.
- Click on Create a Resource.
- 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.
- In addition to a confirmation message, the View thing button will be displayed.
- Choose View thing to display information about your "thing".
- Choose the Connect a device button on the side panel to download a key pair and a certificate generated by AWS IoT.
- On the Connect a device page, select the SDK to use, and then choose Generate certificate and policy. Choose Embedded C here.
- 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.
- Next you will get to see the Download Links for the certificates and the keys. Download them all.
- Choose Confirm & start connecting.
- On the next page you will get a sample format of the code. Save that in a text file for the reference.
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.
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.
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.