Send command from IoT Core to Device

0

Hi Guys, What is the best way to send some commands/messages from AWS Iot Core to device? Can we just use sdk in our back-end app and message/command to iot core with specified "thing"? Any resource will be helpful, thanks

ozy
asked 8 months ago690 views
3 Answers
2

Hi ozy cozy.

Some general guidance:

In short, yes your devices will have to subscribe to a topic, and your cloud application would publish to that topic.

Applications will generally use the IoT data plane API: https://docs.aws.amazon.com/iot/latest/apireference/API_Operations_AWS_IoT_Data_Plane.html. And especially the Publish action: https://docs.aws.amazon.com/iot/latest/apireference/API_iotdata_Publish.html. Although perhaps UpdateThingShadow if you wish to perform command and control using shadows.

This API is implemented in the regular SDKs. You would not typically use the AWS IoT Device SDK just to publish a message from your cloud application (because your application doesn't typically need to make an MQTT connection to the AWS IoT Core broker if you just want to publish a message). IoT data plane API implementations in some of the regular SDKs:

boto3 example: https://github.com/awslabs/aws-greengrass-labs-certificate-rotator/blob/a780c001232e831c7143904e57fe82f0a766fd26/backend/lambda/create_certificate/create_certificate.py#L163

profile pictureAWS
EXPERT
Greg_B
answered 8 months ago
AWS
EXPERT
reviewed 8 months ago
  • Thanks Greg_B, I am going to check those links. First thing comes to my mind is, these devices are connected to AWS IoT core by certificates. Each of them has their own certificate file. I will have thousands devices. If I skip the Iot Core and send them message directly, I am sure connection/security might be problem. Seems like my back-end (multi-tenant java app) app should use IoT core in the middle.

  • These APIs still publish the message using IoT Core. IoT Core is not skipped. Your application would still need an IAM policy that granted it the ability to use these APIs to publish a message to the broker.

  • Thank you Greg, for the clarification. It is a system with real time dashboard for monitoring and controlling and security is very critical.

  • Hi Greg, While checking the documentation, just saw AWS IOT Jobs, do you think that I can do this with IoT Jobs? Documunetation says "Use AWS IoT Jobs to define a set of remote operations that can be sent to and run on one or more devices connected to AWS IoT"

  • Hi ozy. Jobs are not for near real-time stuff. They're for software updates, configuration changes, certificate rotation types of operations: https://iotatlas.net/en/implementations/aws/software_update/iot_jobs/. A cloud application would normally interface with jobs using the IoT control plane: https://docs.aws.amazon.com/iot/latest/apireference/API_Operations_AWS_IoT.html. And perhaps the jobs data plane: https://docs.aws.amazon.com/iot/latest/apireference/API_Operations_AWS_IoT_Jobs_Data_Plane.html

2

Hi,

in addition to the resources shared by Greg, I also consider the problem of timeliness of the commands.

When sending commands to devices, you might want to cope with the case when the device is not connected when the command is issued. In many cases commands are valid only for a short period after they are issued and should be ignored if received too late.

You can either implement this logic on the client side, adding a validity timestamp to the command message and ignoring messages that have a validity < current time. Or, you can leverage MQTT 5 message expiration and set an expiration value on the message itself at the time of publishing to IoT Core. If the client is disconnected and reconnects after the expiration time, the message will not be delivered to it.

AWS
EXPERT
answered 8 months ago
  • Thank you for this. The devices are industrial controls that has built in mqtt message handling functionality. Control messages definitely need to be verified after sending because they are critical. The scenario you have mentioned is very important.

1

Hi,

Yes, you can use the AWS IoT SDKs to send commands or messages from AWS IoT Core to devices. AWS IoT Core provides SDKs for various programming languages like Python, Java, JavaScript (Node.js), C++, and more. Using these SDKs, you can communicate with the IoT Core service and send messages or commands to your devices.

Here is a great starting point: https://docs.aws.amazon.com/iot/latest/developerguide/sdk-tutorials.html

profile pictureAWS
EXPERT
answered 8 months ago
  • thanks for the link. I am able to connect a device and send message to IoT core. My problem is , how should I send message to device? Do I need to "publish" the message to IoT Core and expect IoT core, to forward that message to my device? In this case device has to subscribe to a "topic", in this case device should have two topics, for sending and receiving. Is this assumption correct? Or can I send message directly to device without topic? I am not able find the clear picture in the documents. Thanks

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions