Introduction: Dragonboard and AWS IoT - Python
This instructable will guide you to connect with Dragonboard 410c on AWS IoT using the programming language Python.
At the end of this article, you will be able to connect on AWS IoT and publish/subscribe in any topic.
Step 1: Install AWS IoT Python SDK
Starting our project we need to install the AWS IoT SDK for Python.
- Python 2.7+ or 3.3+ is installed.
- OpenSSL version 1.0.1+ (TLS version 1.2) compiled with the Python executable for X.509 certificate-based mutual authentication.
To check OpenSSL version, use the following command at Python Interpreter.
Install from pip
pip install AWSIoTPythonSDK
Build from source
git clone https://github.com/aws/aws-iot-device-sdk-python.git
python setup.py install
Step 2: Setup AWS IoT
We need to create a project inside AWS IoT, access here to do it.
- Click "create a thing" and choose a name for the project.
Now, we need to able our "thing" to publish/receive a message.
- Click in our "thing" and then click on "Connect a Device".
- After that, we can select the language that we're using to develop our application. Select Python and then click on "Generate Certificate and Policy".
We'll need to generate files and also it's necessary to download the root-CA.crt file.
Transfer this files into your project file. You can create a folder, for example, "certs" and add the certificate files into it.
Step 3: Creating the AWS Client
Import the AWS IoT module to your code.
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
Create five constants with the endpoint, rootCA path, certificate path, private key path, client id.C
You can choose an endpoint here, is appropriate find the best host with the smallest latency. Then follow the code to create your AWS client and connect.
#Initialize the AWS client
myAWSIoTMQTTClient = AWSIoTMQTTClient(clientID)
myAWSIoTMQTTClient.configureEndpoint(endpoint, 8883) myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
#connect to AWS
Step 4: Subscribe and Publish to New Topics
After the client was created, we may subscribe and publish to topics.
For subscribe to a topic, we will need three parameters, the first is the topic name, followed by quality of service and the function called when a new message is received in the subscribed topic.
#QoS - Could be 0 or 1.
myAWSIoTMQTTClient.subscribe(topic, QoS, callback)
The callback function must have three arguments, client, user data, and message.
def callback(client, userdata, message):
print "Information received:"
print "From topic:"
And to publish messages we have to call publish function from our client, we have to take three arguments, the topic to be published, the message and the quality of service.
#QoS - Could be 0 or 1.
myAWSIoTMQTTClient.publish(topic, payload, QoS)