elpinjo / CumulocityClient

Arduino library for connecting your WiFi enabled Arduino to Cumulocity IoT platform over MQTT
3 stars 4 forks source link

Operations #5

Closed josaban closed 1 year ago

josaban commented 1 year ago

Please, can you give us example how you wrote operation to be executed?

elpinjo commented 1 year ago

Good Idea. I will add an example.

elpinjo commented 1 year ago

I just checked the code. There already is an example called ControlLed. As you can see in the example we set the Relay operation:

c8yClient.setSupportedOperations("c8y_Relay,c8y_Command,c8y_Restart");

we registered the callback routine that processes the commands coming from Cumulocity to this device:

c8yClient.setCallback(switchLed);

And the switchLed function will turn on the built-in led or turn it off when the relay state is changed.

To test this out one can create a dashboard for the device and add the relay widget on the dashboard to switch the built-in led on or off.

I hope this helps.

elpinjo commented 1 year ago

Hi Josip,

One thing to know is that the datamodel of Cumulocity is very flexible. There are a few predefined operations like c8y_Restart and c8y_Relay. And example of the c8y_Relay command is as follows:

{
    "description": "Turn on Led",
    "c8y_Relay": {
        "relayState": "CLOSED"
    }
}

So to execute an operation upon an alarm go to smart rules and create a new smartrule to create an operation upon an alarm:

image

Next fill in the fields with the content like so:

image

For custom operations, you need to supply a description and replace the "c8y_Relay" object in the JSON code with anything you like to put there. In the arduino code you can check the content of this to determine what to do and what parameters to use if provided.

Also remind yourself that an operation has 3 statusses and you will have to update them. These are Sent, Pending and completed. For more information checkout the documentation: https://cumulocity.com/guides/concepts/domain-model/#operations

The API documentation also gives quite a good introduction: https://cumulocity.com/guides/device-integration/rest/

And a final pointer to the MQTT SmartRest documentation as this is the way this library interfaces with Cumulocity: https://cumulocity.com/guides/reference/smartrest-two/#quick-reference.

I hope this helps. If you need more information. Try to find me on LinkedIn and we can have a longer conversation.

Kind regards,

Misja Heuveling