Technical Articles
Send/Receive Command with REST on SAP IoT services for SAP BTP for the Cloud Foundry Environment
With SAP IoT services for SAP BTP for the Cloud Foundry Environment, you can receive sensor information from devices, but you can also send “commands” to devices.
The word “command” may be misleading, but what is actually sent is a string of characters. Therefore, the device side needs logic to control the actual device side from the received string.
For more information about this function, please refer to the explanation of using MQTT as a gateway.
To briefly explain the case of MQTT, when you send a command in a specified format to a specified URL (which includes the destination device ID) provided by SAP IoT services, it is published as a Topic on the MQTT Broker, and the Subscriber can receive it. The subscriber (device) will take some action based on the content of the received topic.
Set up

Send Command
Open


Note that the Device ID, Sensor ID, and Capability ID used here are not “Alternate ID” values.

Please use the GUID value here. Enter each parameter and click the Execute button.

Receiving commands via REST
As in the case of receiving via MQTT, the device’s certificate is required. Please download the certificate of the device in advance. This time, I am using cURL as a client.
Since I am using a Mac, I have downloaded certificate in pem format.

The URL to get the command is
Note we should will use Alternate id here.To use cURL, follow the instructions in the manual and execute the following command.
% curl -v -k -E ’<certificate.pem>:<secret key>’ https://<HOST_NAME>/iot/gateway/rest/commands/<DEVICE_ALTERNATE_ID> |
If there are no mistakes, you should get a response like the following.
% curl -v -k -E ’<certificate.pem>:<secret key>’ https://<HOST_NAME>/iot/gateway/rest/commands/<DEVICE_ALTERNATE_ID> * Trying 3.121.203.15:443… |
If you look at the BODY section at the bottom, you will see the contents of the command that was just sent.
This will be returned as JSON, so the device side will parse this and execute the logic to take some action. In the case of this example, it would be necessary to write the logic to adjust the speed of the motor, etc. to 50, and then execute the process of turning on the buzzer and LED blablabla.
POINT
When using REST, the point is that the device needs to know if a command has been issued or not. In other words, there needs to be logic that calls the REST API from the device periodically to see if any commands have been issued.
Others
For example, first issue a command with the following parameters
Next, issue the following command to the same device.
After this, if you check with REST to see if the command has been issued, the following will be returned.
—-snip—–
* Connection #0 to host <HOST_NAME> left intact [{“sensorAlternateId”:”10005″,”capabilityAlternateId”:”10000″,”command”:{“Speed”:50.0,”Buzzer”:true,”LED”:true}},{“sensorAlternateId”:”10005″,”capabilityAlternateId”:”10000″,”command”:{“Speed”:0.0,”Buzzer”:false,”LED”:false}}]% |
The old commands are sent along with the new commands, which are sent as an array in JSON.
As far as I have tested, this JSON array seems to be arranged in the order of command issuance. However, I couldn’t find any clear documentation, so if order of commands is important, or if there is a requirement to execute only the latest command, it may be better to add the date and time of command issuance or a number to the command to be sent so that it can be identified.
Incidentally, if the command has not been issued, the following will be returned.
* Connection #0 to host <HOSTNAME> left intact [] |
Thus, an empty JSON is returned. On the device side, please take these into account when controlling the command.
Summary
SAP IoT services for SAP BTP for the Cloud Foundry Environment uses the above method to handle Command via REST. Using commands, device control is also possible from the cloud side, so please try this.
Hi Naoto Sakai,
Thank you for blog.Is it possible to use Postman to send the message using REST Gateway to the Device ID. I tried to import the curl command as a raw text and uploaded the .pem certificate under the certificate tab in the setting in Postman, but received the unauthorized message "Unauthorized for device alternate ID 'E_4000060', certificate CN ''.
Thanks & Regards,
Omkar.
HI ,
I have never used that feature of Postman; does it run successfully without error when you run the curl command? If so, then the problem is on Postman's side.
Regards,
Naoto
Hi Naoto Sakai,
thank you for your response. Could you please provide insights on generation of Certificates ? We tried to follow the step 5 as per the SAP Help documentation(Send Data with REST | SAP Help Portal) provided but unable to download the certifcate file *-device_certificate.pem.
Regards,
Omkar G.
Hi Omkar ,
Can't download ? You got an any error ?
If so, please create an incident.
I have been getting an error message on generating a certificate. It was resolved by submitting an Incident.
Regards,
Naoto
Hi Naoto,
Apology for delay in response. I don't receive any error message but *-device_certificate.pem file is not getting downloaded in the local machine to rename it. Only receive the JSON response.
Regards,
Omkar.