How Send a message to Oracle IoT Cloud Service

December 4, 2020 | 2 minute read
Derek Kam
Consulting Solutions Architect
Text Size 100%:

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/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.

Derek Kam

Consulting Solutions Architect

Derek Kam is a Consulting Solutions Architect in the A-Team at Oracle Corporation. He works closely with customers and partners, worldwide, providing guidance on architecture, best practices, troubleshooting and how best to use Oracle Cloud Services and products to solve customer business needs. Derek is a multi-skilled IT professional with more than 26 years of experience, possessing a wide range of experience and expertise in the Oracle Fusion Middleware and Oracle Clouds (PaaS and IaaS) technical and architecture design, development, software testing and quality assurance, and project management. Prior to joining Oracle in 2012, Derek has worked in consulting, financial and retail industries.


Previous Post

Loading Data into Oracle Fusion SaaS - Serverless Style!

Angelo Santagata | 12 min read

Next Post


A small terraform trick to make your OCI terraforming life a bit easier

Christian Weeks | 4 min read