2mqtt
                                
                                 2mqtt copied to clipboard
                                
                                    2mqtt copied to clipboard
                            
                            
                            
                        MQTT Bridge
2mqtt
2mqtt is MQTT bridge. You can convert the serial, ethernet to MQTT
Supported Providers
- MySensors
- serialto- MQTT
- ethernetto- MQTT
 
- raw- sends the serial,ethernet messages to mqtt as is- serialto- MQTT
- ethernetto- MQTT
- httpto- MQTT
 
Download
Container images
Docker Run
docker run --detach --name 2mqtt \
    --volume $PWD/config.yaml:/app/config.yaml \
    --device /dev/ttyUSB0:/dev/ttyUSB0 \
    --env  TZ="Asia/Kolkata" \
    --restart unless-stopped \
    docker.io/mycontroller/2mqtt:1.4
Download Executables
- Released versions
- Pre Release - mainbranch executables
Configuration
You can have more than one adapter configurations
Provider options: mysensors_v2 and raw
logger:
  mode: development         # logger mode: development, production
  encoding: console         # encoding options: console, json
  level: info               # log levels: debug, info, warn, error, fatal
  enable_stacktrace: false  # enable or disable error stack trace
adapters:   # you can have more than one adapter
  - name: adapter1          # name of the adapter
    enabled: false          # enable or disable the adapter, default disabled
    reconnect_delay: 20s    # reconnect automatically, if there is a failure on the connection
    provider: mysensors_v2  # provider type, options: mysensors_v2, raw
    source: # source is the device, to be converted to MQTT, based on the type, configurations will be different
      type: serial              # source device type: serial
      port: /dev/ttyUSB0        # serial port
      baud_rate: 115200         # serial baud rate
      transmit_pre_delay: 10ms  # waits and sends a message, to avoid collision on the source network
    mqtt: # mqtt broker details
      broker: tcp://192.168.10.21:1883  # broker url: supports tcp, mqtt, tls, mqtts
      insecure: false                   # enable/disable insecure on tls connection
      username:                         # username of the broker
      password:                         # password of the broker
      subscribe: in_rfm69/#             # subscribe a topic, should include `#` at the end, your controller to serial port(source)
      publish: out_rfm69                # publish on this topic, can add many topics with comma, serial to your controller
      qos: 0                            # qos number: 0, 1, 2
      transmit_pre_delay: 0s
      reconnect_delay: 5s
      connection_timeout: 30s           # mqtt connection timeout (default 30 seconds)
Source device configuration
Based on the source type the configurations will be different.
Serial
source:
  type: serial              # source device type
  port: /dev/ttyUSB0        # serial port
  baud_rate: 115200         # serial baud rate
  transmit_pre_delay: 10ms  # waits and sends a message, to avoid collision on the source network
  message_splitter: # message splitter byte, default '10'
Ethernet
source:
  type: ethernet                    # source device type
  server: tcp://192.168.10.21:5003  # ethernet server address with port
  transmit_pre_delay: 10ms          # waits and sends a message, to avoid collision on the source network
  message_splitter: # message splitter byte, default '10'
HTTP
source:
  type: http                      # source device type
  listen_address: "0.0.0.0:8080"  # listening address and port
  is_auth_enabled: true           # enable/disable basic authentication
  username: hello                 # username of basic authentication
  password: hello123              # password of basic authentication
for http source, on mqtt the payload (json string) will be as follows,
{
  "method":"POST",
  "remoteAddress":"192.168.0.1:57112",
  "host":"my-secret-host.com:8080",
  "path":"/hello",
  "body":"say hello",
  "queryParameters":{
    "q1": ["v1"],
    "q2": ["v1", "v2"]
  },
  "headers":{
    "Accept-Encoding":["gzip"],
    "Cache-Control":["no-cache"],
  },
  "timestamp":"2022-05-27T08:01:55.806281887+05:30"
}
Special note on message_splitter
NOTE: Applicable for serial and ethernet devices
- message_splitteris a reference char to understand the end of message on serial and ethernet device read
- This special char will be included while writing to the device.
- supports only one char, should be supplied in byte, ie:0to255, extended ASCII chars
Quick references
For complete details refer the extended ASCII table
- 0- Null char
- 3- End of Text
- 4- End of Transmission
- 8- Back Space
- 10- Line Feed
- 13- Carriage Return
Script support
In raw provider we can add script to support custom specification. If we leave the script part empty, works without formatting.
2mqtt support limited JavaScript support along with goja
Configuration file with raw provider and a script support
logger:
  mode: development         # logger mode: development, production
  encoding: console         # encoding options: console, json
  level: info               # log levels: debug, info, warn, error, fatal
  enable_stacktrace: false  # enable or disable error stack trace
adapters:   # you can have more than one adapter
  - name: adapter1          # name of the adapter
    enabled: false          # enable or disable the adapter, default disabled
    reconnect_delay: 20s    # reconnect automatically, if there is a failure on the connection
    provider: raw  # provider type, options: mysensors_v2, raw
    source: # source is the device, to be converted to MQTT, based on the type, configurations will be different
      type: serial              # source device type: serial
      port: /dev/ttyUSB0        # serial port
      baud_rate: 115200         # serial baud rate
      transmit_pre_delay: 10ms  # waits and sends a message, to avoid collision on the source network
    mqtt: # mqtt broker details
      broker: tcp://192.168.10.21:1883  # broker url: supports tcp, mqtt, tls, mqtts
      insecure: false                   # enable/disable insecure on tls connection
      username:                         # username of the broker
      password:                         # password of the broker
      subscribe: receive_data/#         # subscribe a topic, should include `#` at the end, your controller to serial port(source)
      publish: publish_data             # publish on this topic, can add many topics with comma, serial to your controller
      qos: 0                            # qos number: 0, 1, 2
      transmit_pre_delay: 0s
      reconnect_delay: 5s
      connection_timeout: 30s           # mqtt connection timeout (default 30 seconds)
    formatter_script: # script used to perform custom formatting
      to_mqtt: |
        // your multiline javascript
        // to perform formatting
        // read examples for more details
      to_source: |
        // your multiline javascript
        // to perform formatting
        // read examples for more details
to_mqtt
data received from source device and posts to mqtt. can be serialPort, http, ethernet. for details refer source device options section.
you will receive the following variables on your script
- raw_data- data received from- sourcedevice
once the format done, at the end of script, you have to submit the result in two ways 
in both way, your response should be assigned into result variable WITHOUT let, var or const.
- if you do not want to change the mqttparameters, liketopic,QoS, etc, just assign the response intoresult. example:result="hello"
- if you need to change mqttparameters dynamically, assign key/value intoresultvariable. supported keys,
- data- your response data should passed on the mqtt publish
- mqtt_topic- if you want to change the topic dynamically. NOTE: this topic will be appended(suffix) along with global topic (- adapters[].mqtt.publish)
- mqtt_qos- you can modify the QoS
- ignore- if you think, no need to proceed further with this data and do not want to send it to- mqttadd- ignore: true. This message will be dropped.
examples:
simple string return
// want to append "_modified" at the end of raw data
data=raw_data + "_modified"
// return
result=data
return as object
// want to add mqtt topic dynamically
// assume the raw_data is "hello;mqtt/secret/topic"
const dataArray = raw_data.split(";")
// return
result = {
  data: dataArray[0], // ie: hello
  mqtt_topic: dataArray[1], // ie: mqtt/secret/topic
}
ignore the data. do not proceed further
// assume the raw_data is "ignore_me"
// ignoreMe is "true", if the message is "ignore_me"
const ignoreMe = raw_data == "ignore_me"
// return
result = {
  data: raw_data,
  ignore: ignoreMe, // is true. this message will not send to mqtt
}
to_source
data received from mqtt passed to formatter script and posts to source device.
you will receive the following variables on your script
- raw_data- data received from mqtt subscription
- mqtt_topic- topic of the received message
- mqtt_qos- qos of the received message
once the format done, at the end of script, you have to submit the result in two ways 
in both way, your response should be assigned into result variable WITHOUT let, var or const.
- just assign the into result. example:result="hello"
- assign key/value into resultvariable. supported keys,
- data- your response data should passed to- sourcedevice
- ignore- if you think, no need to proceed further with this data and do not want to send it to- sourcedevice add- ignore: true. This message will be dropped.
examples
simple string return
// want to include mqtt topic in the message
// assume: 
//    raw_data is "hello"
//    mqtt_topic is "mqtt/secret/topic"
const finalData = raw_data + ";" + mqtt_topic
// return
result = finalData // ie: hello;mqtt/secret/topic
return as object
// want to include mqtt topic in the message
// assume: 
//    raw_data is "hello"
//    mqtt_topic is "mqtt/secret/topic"
const finalData = raw_data + ";" + mqtt_topic
// return
result = {
  data: finalData, // ie: hello;mqtt/secret/topic
}
ignore the data. do not proceed further
// assume the raw_data is "ignore_me"
// ignoreMe is "true", if the message is "ignore_me"
const ignoreMe = raw_data == "ignore_me"
// return
result = {
  data: raw_data,
  ignore: ignoreMe, // is true. this message will not send to mqtt
}