Using Python to Communicate with AWS IoT Core

   Go back to the Task List

  « 4. Develop Code to Exchange Message    6. IoT to Device Communication »

5. Device to IoT Communication

The device and AWS IoT Core use publish-subscribe based communication model using topics. In this task, you configure AWS IoT Core to subscribe to a topic general/inbound and then will use python code in Cloud9 to send the message to the same topic. AWS IoT Core will receive message due to subscription. AWS IoT Core provides MQTT client to test such message exchanges and you are going to use it.

  1. On the AWS IoT Core console, click on Test menu in the left. Enter general/inbound for the Subscription topic and click on the Subscribe to topic button.

    Inbound

  2. It moves to the subscriptions page where you can see all the messages sent to the topic. Keep the subscriptions page open in the browser.

    Inbound

  3. While keeping the subscriptions page in the browser, open AWS Cloud9 environment dojoenvironment in another browser tab. You will use publish.py code to send MQTT message to the general/inbound topic. Run the command python publish.py in the environment bash console to execute publish.py file.

    Inbound

  4. The code will run and publish MQTT message Sample data from the device to the general/inbound topic.

    Inbound

  5. Go back to the AWS IoT subscriptions page in the browser; you will see the message has been received by AWS IoT core.

    Inbound

  6. AWS IoT can then use AWS IoT Rule to route this message to destinations such as Kinesis, Lambda etc. for the processing and storage. You saw device to IoT Core communication. In next task, you will see IoT core to device communication.