This guide will help you to get familiar with CAN connector configuration for ThingsBoard IoT Gateway.
Use general configuration to enable this connector.
We will describe connector configuration file below.
The root part of the CAN connector configuration provides basic information to connect/reconnect to a CAN bus and the list of device configurations.
Parameter | Default value | Description |
---|---|---|
name | CAN Connector | Name of connector. |
interface | socketcan | Type of CAN interface. |
channel | vcan0 | Channel name of CAN interface. |
backend | Interface specific configuration. | |
reconnect | true | Whether to reconnect after bus error while sending/receiving CAN messages. |
reconnectPeriod | 30.0 | Period in seconds between reconnect attempts. Floating point means more precise time than seconds. |
reconnectCount | Number of reconnect attempts after bus error. If not specified means infinite number of attempts. | |
devices | List of devices. |
The list of supported CAN interfaces one can find in the documentation of the Python CAN library.
This section is optional and it provides configuration for specific CAN interface. Each option has default value. To get available list of options see the documentation for specific interface type.
For example, the SocketCAN interface supports the following configuration options:
"backend": {
"receive_own_messages": true,
"fd": true
}
This means that transmitted messages should also be received and CAN-FD frames should be supported. By default these options are disabled.
This section provides array of configurations for devices, which connected through the CAN bus.
Parameter | Default value | Description |
---|---|---|
name | Name of device. | |
type | can | Type of device. |
sendDataOnlyOnChange | false | Sending only if data changed from last check, if not specified data will send after each received CAN message. |
strictEval | true | Restricted mode of Python eval() API. |
enableUnknownRpc | false | Allow processing RPC commands not listed in serverSideRpc subsection. |
overrideRpcConfig | false | Allow overriding RPC command configuration (all or some of options) by data received from server. |
converters | Custom converters. | |
attributes | List of device attributes. | |
timeseries | List of time series keys. | |
attributeUpdates | List of shared attributes to be subscribed for. | |
serverSideRpc | List of RPC commands. |
If enableUnknownRpc is set to true, overrideRpcConfig is forcibly set to true as well.
Note, despite attributes, timeseries, attributeUpdates and serverSideRpc are optional subsections, at least one of them must be set to get in use the device configuration.
CAN connector is provided with built-in uplink/downlink data converters. One can specify custom converter for both uplink and downlink data or for one of them.
Parameter | Default value | Description |
---|---|---|
uplink | Python class of uplink converter. | |
downlink | Python class of downlink converter. |
As input data the uplink converter gets CAN payload (array of bytes) and list of configurations from “attributes” and “timeseries” subsection to know which bytes to get and how they need to be interpreted.
As output data the uplink converter returns dictionary with attribute and telemetry lists. Each element of this list is key/value pair, where key is attribute name or time series key and the value is what it is.
{
"attributes": [{"isDriverDoorOpened": "true"}],
"telemetry": [{"rpm":100},{"milliage": 300000}]
}
As input data the downlink converter gets the value (or values in case of RPC) and configuration (from “attributeUpdates” or “serverSideRpc” subsections) that describes how to convert the value (or values) to CAN payload.
As output data the downlink converter returns CAN payload (array of bytes) for the further sending.
This subsection provides the list of configuration each of them describes which bytes to get from CAN payload (array of bytes) and how to convert these bytes to a Thingsboard attribute or a time series key.
Parameter | Default value | Description |
---|---|---|
key | Name of attribute or time series key. | |
nodeId | CAN node (arbitration) id. | |
value | Value conversion configuration. | |
expression | Python eval() expression to modify value in some way. | |
command | Command conversion configuration. | |
polling | Polling configuration. | |
isExtendedId | false | If True means extended CAN node (arbitration) id. |
isFd | false | If True means using CAN FD mode. |
bitrateSwitch | false | Only for CAN FD mode If True means a higher bitrate is used for the data transmission. |
The option command is an additional but non-neccessary level of abstraction what lets one to interpret the one part of CAN message differently based on the another part of this CAN message.
For example, the controller of an irrigation system can inform about different parameters of the system. Communication is done via CAN bus.
The first byte of a CAN payload is command. If the value of a command is 2, it means that following bytes is temperature.
If value of command is 4, data is a humidity level.
The option command supports the following formats:
command: "<start>:<length>:[byteorder]:[value]"
"command": {
"start": <start_value>,
"length": <length_value>,
"byteorder": <byteorder_value>,
"value": <value>
}
where:
Example:
"command": "0:2:little:12345"
The option value describes how many bytes to get from CAN payload and what primitive type to be converted to.
The option value supports the following formats:
"value": "<start>:<length>:[byteorder]:<type>:[encoding|signed]"
"value": {
"start": <start_value>,
"length": <length_value>,
"byteorder": <byteorder_value>,
"type": <type_value>,
"encoding": <encoding_value>,
"signed": <signed_value>
}
where
Examples:
"value": "2:1:int"
"value": "0:7:double"
"value": "0:4:little:float"
"value": "4:2:little:int:signed",
"expression": "value / 4"
The option expression is evaluated via Python eval() API. There are available the following variables in the eval() context:
Note, by default Python eval() API is working in some kind of a restricted mode by denying the explicit access to __builtins__ API, to disable the restricted mode set the option strictEval to False.
If a polling configuration is not specified the CAN connector receive only that data what CAN node decides to send on its own.
On demand sending activates after CAN node receives specific data which is specified in the polling configuration. Depending on the type of polling the CAN connector can send that data once or periodically.
Parameter | Default value | Description |
---|---|---|
type | always | Type always means sending CAN message periodically, once means single time sending. |
period | 1.0 | Period of polling in seconds. Floating point means more precise time than seconds. |
dataInHex | CAN message payload in the hexadecimal format. |
This subsection provides the list of configurations to subscribe for changes of Thingsboard shared attributes.
Parameter | Default value | Description |
---|---|---|
attribute | Name of shared attribute. | |
nodeId | CAN node (arbitration) id. | |
isExtendedId | false | If True means extended CAN node (arbitration) id. |
isFd | false | If True means using CAN FD mode. |
bitrateSwitch | false | Only for CAN FD mode If True means a higher bitrate is used for the data transmission. |
dataLength | 1 | Only for integer values Number of bytes to pack integer value. |
dataByteorder | big | Only for integer and float values Order of bytes to pack numeric value. |
dataSigned | false | Only for int/long types indicates whether integer is signed value or not. |
dataExpression | Python eval() expression to modify attribute value in some way before packing it to array of bytes. | |
dataEncoding | ascii | Only for string values Encoding of string packing. |
dataBefore | Hexadecimal string of bytes that are preceded value bytes. | |
dataAfter | Hexadecimal string of bytes that are followed by value bytes. |
The steps of processing an attribute update are the following:
This subsection provides the list of configurations to process RPC commands from a ThingsBoard server to a device.
Parameter | Default value | Description |
---|---|---|
method | Name of RPC command. | |
response | false | If true, response will be sent to ThingsBoard. |
nodeId | CAN node (arbitration) id. | |
isExtendedId | false | If True means extended CAN node (arbitration) id. |
isFd | false | If True means using CAN FD mode. |
bitrateSwitch | false | Only for CAN FD mode If True means a higher bitrate is used for the data transmission. |
dataLength | 1 | Only for integer values Number of bytes to pack integer value. |
dataByteorder | big | Only for integer and float values Order of bytes to pack numeric value. |
dataSigned | false | Only for int/long types indicates whether integer is signed value or not. |
dataExpression | Python eval() expression to modify attribute value in some way before packing it to array of bytes. | |
dataEncoding | ascii | Only for string values Encoding of string packing. |
dataBefore | Hexadecimal string of bytes that are preceded value bytes. | |
dataAfter | Hexadecimal string of bytes that are followed by value bytes. | |
dataInHex | Only for RPC without parameters Hexadecimal string of bytes that are sent to CAN node. |
The CAN connector supports RPC commands without and with parameters (see params JSON object).
The without parameters RPC type requires only dataInHex to be set (another data* options are not used). The value of dataInHex is sent as payload of a CAN message each time when the RPC command is processed.
The with parameters RPC type is based on all data* options except dataInHex. The steps of processing are the same as for attribute updates except:
{
"device": "Car",
"data": {
"method": "setLightLevel",
"params": {
"value": 70
}
}
}
{
"device": "Car",
"data": {
"id": 1,
"method": "setSpeed",
"params": {
"userSpeed": 150,
"maxAllowedSpeed": 100
}
}
}
The configuration of such RPC command is set in the way whether user exceeds the limit the speed of a car is forcibly set to this limit:
"serverSideRpc": [
{
"method": "setSpeed",
"nodeId": 16,
"dataBefore": "09",
"dataAfter": "aabb",
"dataExpression": "userSpeed if maxAllowedSpeed > userSpeed else maxAllowedSpeed"
}
]
So after processing the setSpeed RPC command CAN payload is following:
[ 0x09, 0x64, 0xAA, 0xBB ]
, where 0x64 is 100 mph because user exceeds the limit.
Explore guides related to main ThingsBoard features: