t6 Features > Sockets connection

Sockets connection

A socket programming interface to connect physical Objects with t6 IoT platform and using a bi-directional communication.

Tagged on #feature, #sockets, #preprocessor, #Objects, Sockets connection

How to use Sockets with t6 IoT?

Sockets is an early stage t6 feature made to connect easily Arduino Objects with t6. The idea is to have a minimal code-base on the Arduino device as generic as possible and interface that Object using simple Socket connectivity.

t6 implement a Socket server. Any Client can connect to it using its own t6 credentials (a Basic Authorization header made from base64 encoded key:secret).

The Client must then claim the Object using the specific command:

{"command": "claimObject", "object_id": "aaaaaaaa-aaaa-4bbb-8888-aaaaaaaaaaaa"}

Note: The claimed Object must be owned by the User Account from the Basic Authorization.

Multiple commands can be sent to Socket from any authenticated Client:

Using Sockets with preprocessor and Rule Engine

Of course Sockets on t6 IoT can be triggered from a Decision Rule during Datapoint creation.

Here is a sample Rule that will trigger the command to be sent to Arduino socket connected devices.

E.g.:

{
    "name": "Rule for Web Sockets on t6 IoT",
    "rule": {
        "conditions": {
            "all": [
            ]
        },
        "event": {
            "type": "sockets",
            "params": {
                "socketPayload": {
                    "arduinoCommand": "digitalWrite",
                    "pin": 2,
                    "value": "{value}"
                }
            }
        },
        "priority": 1
    },
    "active": true
}

socketPayload node must contains an arduinoCommand attribute to specify to command on the Arduino Object. arduinoCommand is one of the following :

Once the Object received the WebSocket message, it will be executed from as a command. Object require to use the sample arduino code.

The datapoint should stick to the regular styntax. E.g: please note the value attribute containing a variable. It will be transformed with the t6 preprocessor dring the ingestion process.

[
    {
        "value": "1\n",
        "flow_id": "{{$randomUUID}}",
        "text": "",
        "save": false,
        "unit": "",
        "mqtt_topic": "",
        "publish": true,
        "object_id": "aaaaaaaa-aaaa-4bbb-8888-aaaaaaaaaaaa",
        "datatype_id": "e7dbdc23-5fa8-4083-b3ec-bb99c08a2a35"
    }
]

Note: Instead of sending the socketPayload on the text attribute, t6 allows to have a separate Rule containing that socketPayload as parameter.

Socket messages to single or several Arduino devices

This is possible to send the same message simultaneously to multiple Arduino Objects or to specify a single Object as target message. In order to send a command, you’d need to use object_ids from the same t6 User Account and claim them to Socket using the above procedure.

On the Socket payload, the command unicast will send the message to a single Object using the attribute object_id.

E.g:

{
    "command": "unicast",
    "object_id": "aaaaaaaa-aaaa-4bbb-8888-aaaaaaaaaaaa",
    "payload": {
        "arduinoCommand": "digitalWrite",
        "pin": "2",
        "value": "0"
    }
}

Obviously, the command broadcast (that does not require any object_id) will send the message to any Object connected to Socket and claimed from the same User Account. The arduinoCommand digitalWrite will be received on all of these devices simultaneously.

E.g:

{
    "command": "broadcast",
    "payload": {
        "arduinoCommand": "digitalWrite",
        "pin": "2",
        "value": "0"
    }
}

The Arduino example code is available at https://github.com/mathcoll/t6iot/blob/master/examples/nodeMCU-websockets-client/nodeMCU-websockets-client.ino

Tagged on #feature, #sockets, #preprocessor, #Objects,