X

Best Practices from Oracle Development's A‑Team

How Send a message to Oracle IoT Cloud Service

Derek Kam
Consulting Solutions Architect

In this blog, I will show you how to send a message to an IoT server from a device using the REST API. The message will be in JSON payload. 

To send a message to an Oracle IoT platform, you need a message token. This document describes how to generate a message token: https://docs.oracle.com/en/cloud/paas/iot-cloud/iotrq/op-iot-api-v2-oauth2-token-post.html. Please take note that the scope has to be an empty scope for the message toke. Below is an example of the message access token payload:

{
   "expires_in":3600000,
   "token_type":"Bearer",
   "access_token":"6074b571c671fe3cd548a8e668042187"
}

The access token will be used as the authorization token to send the message to the Oracle IoT platform.  You also require the device id, the id can be obtained from the IoT Platform->Devices->Management, and the device must be activated.

If you have a device model setup in your Oracle IoT platform with system attributes, for example:

You can send a message to the Oracle IoT platform using the model URN (e.g. urn:com:oracle:iot:cw:gps) + “:attributes” as the format, and the data will be the name of the attributes, see highlighted below:

{
    "eventTime": 1603753354,
    "destination": "",
    "source": "D2599C78-E2AD-4A8F-BE2C-AE274C4E68F0",
    "sender": "D2599C78-E2AD-4A8F-BE2C-AE274C4E68F0",
    "clientId": "0A5E5178-9FCF-4DC7-815A-46E86B473CA51",
    "payload": {
        "format": "urn:com:oracle:iot:cw:gps:attributes",
        "data": {
            "ora_uncertainty": 0,
            "ora_latitude": -33.717035699999997,
            "ora_altitude": 0,
            "ora_longitude": 151.0443836
        }
    },
    "properties": {},
    "type": "DATA",
    "priority": "LOW",
    "reliability": "BEST_EFFORT"
}

If you have custom data or alerts defined, then the format should be the URN of the custom data or alert. The data will be the field  you defined for this custom data, for example: 

{
   "clientId":"c839d086-bb23-4cd3-8405-130f5fc92b5b",
   "source":"0-QMCA",
   "destination":"",
   "priority":"LOW",
   "reliability":"BEST_EFFORT",
   "eventTime":1453427902124,
   "sender":"",
   "type":"DATA",
   "properties":{

   },
   "payload":{
      "format":"urn:oracle:iot:device:powermeter",
      "data":{
         "instantPowerConsumption":700
      }
   }
}

Now you are ready to send the message using the IoT REST API ( https://{{IoT-host}}/iot/api/v2/messages) with the above payload, message access token, and the X-EndpointId in the header. (note: X-EndpointId = Device ID) using HTTP POST method.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha