./can2mqtt.py -h to see a list of command line parameters.
Default is to read configuration from local directory from file
config.json and log to stdout with level INFO.
Connection to CAN bus is configured in
canbus section of json config file.
Connection to MQTT broker is configured in
Translation of CAN messages to MQTT is configured in
Translation of MQTT messages to CAN is configured in
Cennection to MQTT broker is made using paho-mqtt package.
You may set
client_id (default is
host (default is
port (default is
Packing and Unpacking of CAN message
CAN messages are packed and unpacked using bitstruct package.
Due to a problem with endianness, packing and unpacking is now done using the struct package.
This is a little bit less flexible; it doesn't allow packing single bits into one byte and odd 24 bit or 56 bit integers are not supported, but being able to unpack CANopen data which come in little endian was more importatnt for me.
See here for valid syntax of
pack_template of transmitter resp.
unpack_template of receiver templates.
Each value from the template is later referenced by a variable name given in
Formating of topic and payload string
Multiple topic and payload strings can be formed and sent to the broker from one CAN message via one or more receiver.
payload_template may be strings or arrays of strings. Each string is formated using Format Specification Mini-Language.
Each replacement field has to explicitly reference a variable name from
Additionally to the variable names defined in
payload_template may reference the timestamp and CAN id of the received message by the names
The CAN timestamp is also provided under the name
datetime format which may be converted to string with e.g. the following pattern:
Parsing of incoming MQTT messages
Each transmitter handles MQTT messages received due to its
Received topics and payloads are parsed using parse package.
Again explicit field names have to be used.
var_names gives the order of the parsed named values to be used to pack the CAN message using the
The CAN id is either a decimal integer, a hex formatted integer string or a single variable name from the set of parsed named values.
Unpacked and parsed values named by variable names in the
var_names parameter may be transformed using a python function defined in the
Each such function must expect one scalar parameter and return one scalar parameter.
To use a translation the variable name in
var_names has to be followed by the key word "via" and then the name of the transformation function.
For logging python logging is used.
The special section
canopen allows to configure the bridge to send some CANopen specific messages.
sync_interval is specified a sync message is sent every interval seconds. If
sync_count is specified the sync message has one data byte that cyclicly counts from 0 to the given count.
auto_start is true the program listens to CANopen NMT bootup and heartbeat messages and sends a start command to every node that indicates it is not in operational mode.