Dragonboard and AWS IoT - Python

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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Install AWS IoT Python SDK

Starting our project we need to install the AWS IoT SDK for Python.

Minimum requirements

  • 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.
import ssl
ssl.OPENSSL_VERSION

Install from pip

pip install AWSIoTPythonSDK

Build from source

git clone https://github.com/aws/aws-iot-device-sdk-python.git
cd aws-iot-device-sdk-python
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
myAWSIoTMQTTClient.connect()

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 message.payload
print "From topic:"
print message.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)

Be the First to Share

    Recommendations

    • Backyard Contest

      Backyard Contest
    • Silly Hats Speed Challenge

      Silly Hats Speed Challenge
    • Finish It Already Speed Challenge

      Finish It Already Speed Challenge

    5 Discussions

    0
    Join TechH
    Join TechH

    1 year ago

    Hi dear, I want to use esp32 with aws IoT greengrass. Can you guide me, how to achive it ?

    0
    joaopmsouza
    joaopmsouza

    3 years ago

    Helpful tutorial, congratulations!

    0
    Swansong
    Swansong

    3 years ago

    Thanks for sharing :)