Tracking using AWS IoT and MQTT with Amazon Location Service
MQTT
AWS IoT Core
Note
Devices may know their own position, for example via built-in GPS. AWS IoT also has support for third party device location tracking. For more information, see AWS IoT Core Device Location in the AWS IoT Core Developer Guide.
The following walkthrough describes tracking using AWS IoT Core rules. You can also send the device information to your own AWS Lambda function, if you need to process it before sending to Amazon Location. For more details about using Lambda to process your device locations, see Using AWS Lambda with MQTT.
Topics
Prerequisite
Before you can begin tracking, you must complete the following prerequisites:
-
Create a tracker resource that you will send the device location data to.
-
Create an IAM role for granting AWS IoT Core access to your tracker.
When following those steps, use the following policy to give access to your tracker:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "WriteDevicePosition", "Effect": "Allow", "Action": "geo:BatchUpdateDevicePosition", "Resource": "arn:aws:geo:*:*:tracker/*" } ] }
Create an AWS IoT Core rule
Next, create an AWS IoT Core rule to forward your devices' positional telemetry to Amazon Location Service. For more information about creating rules, see the following topics in the AWS IoT Core Developer Guide:
-
Creating an AWS IoT rule for information about creating a new rule.
-
Location action for information specific to creating a rule for publishing to Amazon Location
Test your AWS IoT Core rule in the console
If no devices are currently publishing telemetry that includes location, you can test your rule using the AWS IoT Core console. The console has a test client where you can publish a sample message to verify the results of the solution.
-
Sign in to the AWS IoT Core console at https://console.aws.amazon.com/iot/
. -
In the left navigation, expand Test, and choose MQTT test client.
-
Under Publish to a topic, set the Topic name to
iot/topic
(or the name of the topic that you set up in your AWS IoT Core rule, if different), and provide the following for the Message payload.{ "payload": { "deviceid": "thing123", "timestamp":
1604940328
, "location": { "lat": 49.2819, "long": -123.1187 }, "accuracy": { "Horizontal": 20.5 }, "positionProperties": { "field1": "value1", "field2": "value2" } } } -
Choose Publish to topic to send the test message.
-
To validate that the message was received by Amazon Location Service, use the following AWS CLI command. If you modified it during setup, replace the tracker name with the one that you used.
aws location batch-get-device-position --tracker-name
MyTracker
--device-ids thing123