Cloud to Device messages can be sent to a running Softata device that is connected to an IoT Hub .There are commands such as pausing and continuing the telemetry stream. The protocol mimics service messages as sent from a connected app. There is now a simple quite textual protocol for some commands.

Sending Cloud to Device Messages

Apart from specific apps and VS Code, The Azure IoT Explorer can used to send C2D messages to a device. For this activity used Cloud-to-device-message not Direct method.

For a Softata device that is connected to an Azure IoT Hub, it is possible to send C2D messages from the cloud, that are interpreted at the device as commands. Originally, the messages had a raw structure that mimicked the messages being sent to it from a connected desktop app. This is covered in a previous blog post Softata: Many ways to skin a cat#Cloud 2 Device Messages. For example:

240,0,9,0

… is interpreted as:

  • 0xF0: A Sensor command
  • 0 : Ignored but required
  • 9 : Subcommand 9 which is Telemetry Pause
  • 0 : Sensor Linked List Index zero

This assumes that sensor has been instantiated, the device has connected to IoT Hub and the telemetry stream has started, … or paused.

The same command from the connected app over HTML to the service on the device would be the 5 bytes:

4 F0 0 9 0 
cmd:F0 pin:0 param:9 other:0

The 4 here means 4 bytes follow.

The C2D message can also be a a bit more explicit such as:

telemetry 0 pause  0

The C2D messages are received an initially processed in the second core. They are messaged a little and then inserted into the first core’s server HTML receive stream via

  • rp2040.fifo.push of the data from the second core and …
  • rp2040.fifo.pop of the data in the send code when available when idle or having completed a ‘direct’ command from the app.

About Simple C2D Protocol

In the most recent version of Softata, a simplified C2D message protocol has been added. P0 will pause the telemetry stream generated by the sensor whose linked list index is 0. The mechanism bypasses the first core as Telemetry processing is actually implemented in the second core so no need tp pass the command to the first core, and get it back!. Once a C2D message is received, if it is interpreted as one of these simpler commands, then a direct call is made to implement it in the second core. If not so deciphered by this simpler protocol then it is passed to “raw” C2D interpreter.

Simple C2D Protocol

The C2D message is a string. Properties etc are ignored.

  • The first character determines the command, and is case insensitive alpha.
  • The last character (if numeric) is interpreted as the device linked list index.
  • The second last character (if numeric), if required is an additional parameter such as bit.

The characters in between are ignored

  • Pause0 Pause telemetry for sensor 0
    • uase is ignored and can be omitted or embellished
  • Continue0 Continue telemetry for sensor 0.
    • ditto for ontinue.

There are also is a new Relay type (see next blog Softata: Added Actuator Class - Relay)

  • Actuator01: Set the relay of index 1
    • ctuator is ignored and can be omitted or embellished
  • Reset01: Reset the relay of index 1
    • ditto
  • Toggle01: Toggle the relay of index 1
    • ditto

The 0 parameter in these 3 is the bit parameter but for the relay is ignored as there is only one of them for each listed device. It is required for completeness for other actuators that might use it.

The index of 1 would mean that 2 relays have been instantiated, as the first is always index as 0.

Note: Whilst there is great flexibility, avoid spaces in the these C2D messages.

Examples

  • P0 Pause the stream for sensor 0
  • C1 Continue stream for sensor 1
  • Pau0 Pause the stream for sensor 0
  • T00 Toggle relay 0
  • A71 Set relay 1 (7 is ignored for relay)

Clarification: Linked Lists

When Softata is started, a link list is created for each of the device types:

  • Actuators
  • Sensors
  • Displays

When a device type is instantiated after a specific device’s Setup() is called, it is added to its device type’s linked list along with some metadata including its index in the list. When a command is received with a device linked list index, the device instance is retrieved using the index. This is so, post instantiation, polymorphism is used for device types … _The same device type command can be applied to any instance of a device type in code at the calling level that only refers to the device type. For example:

grove_Actuator->ClearBit();

Conclusion

Sensors and Actuators can be controlled once instantiated by Cloud to Device messages. Message protocol has be simplified.


 TopicSubtopic
   
 This Category Links 
Category:Softata Index:Softata
  Next: > Softata
<  Prev:   Softata