DEV Community 👩‍💻👨‍💻

Cover image for t6 IoT Sockets connection
Mathieu Lory
Mathieu Lory

Posted on • Originally published at internetcollaboratif.info

t6 IoT Sockets connection

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

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:

  • broadcast: to cast a message to any connected Object from your user account.
  • unicast: to cast a message to a specif Object you own.
  • claimObject: to Claim the id of a specific Object.
  • getObject: to get the id of an Object claimed to server.
  • getUser: to get the user_id of an Object claimed to server.
  • getUA: to get the user-agent of an Object. help: to get an help message with all of the commands.

Using Sockets with preprocessor and Rule Engine

Of course Sockets on t6 IoT can be triggered from a Decision Rule during Datapoint creation. The prerequisite is the Object to use Arduino with the sample code

The datapoint should stick to the following styntax including a payload on the text node Payload node need to contains an arduinoCommand to specify to command on the Arduino Object. arduinoCommand is one of the following :

  • analogWrite
  • digitalWrite
  • analogRead
  • digitalRead
  • getPinMode
  • setPinMode

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": {
"command": "unicast",
"object_id": "aaaaaaaa-aaaa-4bbb-8888-aaaaaaaaaaaa",
"payload": {
"arduinoCommand": "digitalWrite",
"pin": "2",
"value": "{value}"
}
},
"save": false,
"unit": "",
"mqtt_topic": "",
"publish": true,
"object_id": "aaaaaaaa-aaaa-4bbb-8888-aaaaaaaaaaaa",
"datatype_id": "e7dbdc23-5fa8-4083-b3ec-bb99c08a2a35"
}
]

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

Top comments (0)

Regex for lazy developers

regex for lazy devs

You know who you are. Sorry for the callout 😆