Integrate IoT Device with AWS IoT using Python — Part II: command-and-response

Workflow of command-and-response using AWS IoT Jobs

Introduction

Before You Start

git pull origin master
git checkout -b command_and_response
.
├── Pipfile
├── Pipfile.lock
├── config.py
├── credentials
│ └── readme.md
├── main.py
├── pyproject.toml
├── scripts
│ ├── __init__.py
│ ├── script_config.py
│ └── update_lambda_code.py
├── setup.cfg
└── src
├── __init__.py
├── aws
│ ├── __init__.py
│ └── aws_iot_client_wrapper.py
├── child_processes
│ ├── __init__.py
│ └── child_processes.py
├── clients
│ ├── __init__.py
│ ├── remote.py
│ └── upload.py
├── errors
│ ├── __init__.py
│ └── network_connection_error.py
├── lambda_func
│ ├── __init__.py
│ └── remote_control_api
│ ├── __init__.py
│ └── lambda_function.py
├── logger
│ ├── __init__.py
│ ├── logger_config.py
│ └── ouput.py
├── remote_control
│ ├── __init__.py
│ ├── command.py
│ └── process.py
└── vehicle_detector
├── __init__.py
└── detect_vehicle.py

What does “command-and-response” mean?

Workflow of AWS IoT Jobs for command-and-response

Create A Job Document And Save It on S3

{
"cmd": "greeting"
}

Set up AWS IoT for The Remote Client

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:Subscribe",
"Resource": "arn:aws:iot:[region]:[aws_account]:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/jobs/*"
},
{
"Effect": "Allow",
"Action": "iot:Receive",
"Resource": "arn:aws:iot:[region]:[aws_account]:topic/$aws/things/*"
},
{
"Effect": "Allow",
"Action": "iot:Publish",
"Resource": "arn:aws:iot:[region]:[aws_account]:topic/$aws/things/${iot:Connection.Thing.ThingName}/jobs/*"
}
]
}

Set up The Remote Client on The Source Code

src/clients/remote.py

src/remote_control/command.py

src/remote_control/process.py

Update configuration

# AWS IoT config
sensor_name=vehicle_detector_1
endpoint=[iot_thing_https_endpoint]
port=8883
root_ca=AmazonRootCA1.pem
upload_private_key=[credential_id]-private.pem.key
upload_cert_file=[credential_id]-certificate.pem.crt
remote_private_key=[another_credential_id]-private.pem.key
remote_cert_file=[another_credential_id]-certificate.pem.crt
upload_topic=vehicle_detector/test/raw
# Main program config
total_iterations=1000
debug=False

Test run

Command-and-Response Live Demo

2020-08-05 19:55:24,456 - src.clients.remote - INFO - Recieved command "greeting", executing now...
2020-08-05 19:55:24,456 - src.clients.remote - INFO - Execution result: Hello from vehicle_detector_1
2020-08-05 19:55:24,457 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync publish...
2020-08-05 19:55:24,543 - src.clients.remote - INFO - Job status (4, 'SUCCEEDED') update SUCCESS for cmd "greeting"

Summary

Hi, I am from the Earth. And you?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store