Send Measures using REST
Send measure or data to the Device Connectivity Cloud using REST. A program called cURL is used as a sample REST client simulating an IoT device and sends measurement data to the IoT Gateway.
Prerequisites
You need to create a device model for Device Connectivity using the Device Connectivity API. For more information, please refer Create a Device Model using API.
Procedure
- Create a Capability
In the following, a capability is created. A capability can be reused since it can be assigned to multiple sensor types. Each capability can have one or many properties.
- On the SAP IoT Fiori Launchpad, search for the Ingestion app group and select the Device Connectivity app.
The Device Connectivity API Reference opens and you can see the categories overview.
- In the Capabilities category, choose the POST /capabilities request.
- Choose Try it out.
- Copy the JSON string and replace {CapabilityName} with your capability name, replace {PropertyName} with property name, replace {DataTypeName} with the data type for the property, and replace {UnitOfMeasureName} with the unit of the measure.
{ "name": "{CapabilityName}", "properties": [ { "name" : "{PropertyName}", "dataType" : "{DataTypeName}", "unitOfMeasure" : "{UnitOfMeasureName}" } ] } - Paste the modified JSON string in the Example Value field for the POST request.
{ "name": "MyData", "properties": [ { "name" : "temperature", "dataType" : "float", "unitOfMeasure" : "Celsius" }, { "name" : "humidity", "dataType" : "float", "unitOfMeasure" : "g/kg" } ] } - Choose Execute.
- Scroll to the Server response body and Code. If successful, the response code is 200 and the Response body contains all information of the created capability.
- Note down the id of the capability.
- On the SAP IoT Fiori Launchpad, search for the Ingestion app group and select the Device Connectivity app.
- Create a Sensor Type
In the following, a sensor type is created. The previously created capability is assigned to the sensor type.
- Navigate to the SensorTypes category.
- In the SensorTypes category, choose the POST /sensorTypes request.
- Choose Try it out.
- Copy the JSON string and replace {SensorTypeName} with your sensor type name and replace {CapabilityId} with the id of the capability noted down before.
{ "capabilities": [ { "id": "{CapabilityId}", "type": "{type}" } ], "name": "{SensorTypeName}" } - Paste the JSON string in the Example Value field for POST request.
- Choose Execute.
- Scroll to the Server response body and Code. If successful, the response code is 200 and the Response body contains all information of the created sensor type.
- Note down the id of the sensor type.
- Create a REST Device
In the following, a device entity is created. The device entity does not have any sensors attached, yet. The device entity is assigned to one specific gateway.
- Navigate to the Gateways category.
- In the Gateways category, choose the GET /gateways request.
- Choose Try it out.
- Choose Execute.
- Scroll to the response body and note down the id of the gateway with the name REST Gateway (for example, 2).
- Navigate to the Devices category.
- In the Devices category, choose the POST /devices request.
- Choose Try it out.
- Copy the JSON string and replace {gatewayId} with the id you noted down previously and replace {deviceName} with your device name.
{ "gatewayId": "{gatewayId}", "name": "{deviceName}" }When the above json is posted to the API, a sensor will be created along with the device.
- Create a Sensor
In the following a sensor is created. The sensor is assigned to the previously created device and is a kind of the previously created sensor type.
- Navigate to the Sensors category.
- In the Sensors category, choose the POST /sensors request.
- Choose Try it out.
- Copy the JSON string and replace {DeviceId} with the id of the device noted down before, replace {SensorName} with your sensor name, and replace {SensorTypeId} with the id of the sensor type noted down before.
{ "deviceId": "{DeviceId}", "name": "{SensorName}", "sensorTypeId": "{SensorTypeId}" } - Paste the JSON string in the Example Value field for POST request.
- Choose Execute.
- Scroll to the Server response body and Code. If successful, the response code is 200 and the Response body contains all information of the created sensor.
- Note down the id and the alternateId of the sensor.
- Generate the Device Certificate
- Navigate to the Devices category.
- In the Devices category, choose GET /devices/{deviceId}/authentications/clientCertificate/pem.
- Choose Try it out.
- Enter the {deviceId} as Unique identifier of a device.
- Choose Execute.
- Scroll to the Server response body and Code. If successful, the response code is 200 and the Response body contains all information of the generated certificate.
- Click on the Download button available on bottom-right side of Response body to download the file in JSON format.
- Note down the secret key before leaving the page, as it cannot be restored at a later point in time.
- Rename *-device_certificate.pem to client.pem or *-device_certificate.p12 to client.p12.
- Publish Data With cURL Using REST
Prerequisites:
-
You have installed the REST client (cURL). A description of how to install the cURL client can be found in the tutorial Install cURL.
-
You have installed OpenSSL. A description of how to install OpenSSL can be found in the tutorial Install OpenSSL.
-
You have created the device model in step 1-4.
-
You have generated a certificate for your REST device and written down the secret key.
Open the terminal (macOS) or command line tool CMD (Windows) and change the directory to the path of the extracted REST device certificate.
Send data using cURL (only for cURL with OpenSSL on Windows or for cURL without SecureTransport using LibreSSL on macOS).
- Enter and send the message string.
curl -v -k -E client.pem:<SECRET_KEY> -H "Content-Type:application/json" -d "<ENCODED_JSON_MESSAGE>" <REST_ENDPOINT>
Two formats are allowed:
-
One with the measures specified as an array of arrays.
curl -v -k -E client.pem:jJ3fF7dD0rR2rR3wW0eE2tT -H "Content-Type:application/json" -d "{ \"capabilityAlternateId\": \"1234\", \"sensorAlternateId\": \"4321\", \"measures\": [[\"25\"]] }" <REST_ENDPOINT> -
Another one, specifying measures as an array of JSON objects where the name of each property defined in the capability is the key.
curl -v -k -E client.pem:jJ3fF7dD0rR2rR3wW0eE2tT -H "Content-Type:application/json" -d "{ \"capabilityAlternateId\": \"1234\", \"sensorAlternateId\": \"4321\", \"measures\": [{\"temperature\": \"25\"}] }" <REST_ENDPOINT>
-
- Check the response code in the terminal. The message must contain the following:
* upload completely sent off: <xx> out of <xx> bytes
< HTTP/1.1 202 Accepted
See for details of how to inspect data received by a device.
Send data using cURL (only for cURL with SecureTransport on macOS).
- Enter and send the message string.
curl -v -k -E client.p12:<SECRET_KEY> -H "Content-Type:application/json" -d "<ENCODED_JSON_MESSAGE>" <REST_ENDPOINT>
Two formats are allowed:
-
One with the measures specified as an array of arrays.
curl -v -k -E client.p12:jJ3fF7dD0rR2rR3wW0eE2tT -H "Content-Type:application/json" -d "{ \"capabilityAlternateId\": \"1234\", \"sensorAlternateId\": \"4321\", \"measures\": [[\"25\"]] }" <REST_ENDPOINT> -
Another one, specifying measures as an array of JSON objects where the name of each property defined in the capability is the key.
curl -v -k -E client.p12:jJ3fF7dD0rR2rR3wW0eE2tT -H "Content-Type:application/json" -d "{ \"capabilityAlternateId\": \"1234\", \"sensorAlternateId\": \"4321\", \"measures\": [{\"temperature\": \"25\"}] }" <REST_ENDPOINT>
-
- Check the response code in the terminal. The message must contain the following:
* upload completely sent off: <xx> out of <xx> bytes
< HTTP/1.1 202 Accepted
See for details of how to inspect data received by a device.
Send data to IoT Edge Platform REST with the secure transport using cURL
- Enter and send the message string.
curl -v -k -E client.pem:<SECRET_KEY> -H "Content-Type:application/json" -d "<ENCODED_JSON_MESSAGE>" https://<IOT_GATEWAY_IP>:8699/measures/< DEVICE_ALTERNATE_ID>
Sample Code - Measures can be ingested into the IoT Edge Platform REST by performing a POST call on the URL:https://<IOT_GATEWAY_IP>:8699/measures/< DEVICE_ALTERNATE_ID>. The DEVICE_ALTERNATE_ID represents the alternate ID of the device that generated the measures.
- Commands can be retrieved from the IoT Edge Platform REST by performing a GET call on the following URL:https://<IOT_GATEWAY_IP>:8699/commands/< DEVICE_ALTERNATE_ID>
-