Amazon IoT Jobs library demo
Important
This is an archived version of the FreeRTOS User Guide for use with FreeRTOS release 202012.00. For the latest version of this document, see the FreeRTOS User Guide.
Introduction
The Amazon IoT Jobs library demo shows you how to connect to the Amazon IoT Jobs service through an
MQTT connection, retrieve a job from Amazon IoT, and process it on a device. The Amazon IoT Jobs demo project uses the
FreeRTOS Windows port
Note
To set up and run the FreeRTOS demos, follow the steps in Getting Started with FreeRTOS.
Source code organization
The demo code is in the jobs_demo.c file and can be found on the
GitHub directory.freertos/demos/jobs_for_aws/
Configure the Amazon IoT MQTT broker connection
In this
demo,
you use an MQTT connection to the
Amazon IoT
MQTT broker. This connection is configured in the same way as in the MQTT mutual authentication demo
Functionality
The demo shows the workflow used to receive jobs from Amazon IoT and process them on a device. The demo is interactive
and requires you to create jobs by using either the Amazon IoT console or the Amazon Command Line Interface (Amazon CLI). For more
information about creating a job, see
create-job in the
Amazon CLI Command Reference. The demo requires the job document to have an action
key set to print to print a message to the console.
See the following format for this job document.
{ "action": "print", "message": "ADD_MESSAGE_HERE" }
You can use the Amazon CLI to create a job as in the following example command.
aws iot create-job \ --job-id t12 \ --targets arn:aws-cn:iot:region:123456789012:thing/device1 \ --document '{"action":"print","message":"hello world!"}'
The demo also uses a job document that has the action key set to publish to republish
the message to a topic. See the following format for this job document.
{ "action": "publish", "message": "ADD_MESSAGE_HERE", "topic": "topic/name/here" }
The demo loops until it receives a job document with the action key set to exit to
exit the demo. The format for the job document is as follows.
{ "action: "exit" }
Entry point of the Jobs demo
The source code for the Jobs demo entry point function can be found on
GitHub
-
Establish an MQTT connection using the helper functions in
mqtt_demo_helpers.c. -
Subscribe to the MQTT topic for the
NextJobExecutionChangedAPI, using helper functions inmqtt_demo_helpers.c. The topic string is assembled earlier, using macros defined by the Amazon IoT Jobs library. -
Publish to the MQTT topic for the
StartNextPendingJobExecutionAPI, using helper functions inmqtt_demo_helpers.c. The topic string is assembled earlier, using macros defined by the Amazon IoT Jobs library. -
Repeatedly call
MQTT_ProcessLoopto receive incoming messages which are handed toprvEventCallbackfor processing. -
After the demo receives the exit action, unsubscribe from the MQTT topic and disconnect, using the helper functions in the
mqtt_demo_helpers.cfile.
Callback for received MQTT messages
The
prvEventCallbackJobs_MatchTopic from the Amazon IoT Jobs library to classify the
incoming MQTT message. If the message type corresponds to a new job, prvNextJobHandler() is called.
The
prvNextJobHandlerprvSendUpdateForJob function.
Send an update for a running job
The function
prvSendUpdateForJob()Jobs_Update() from the Jobs library to populate the
topic string used in the MQTT publish operation that immediately follows.