Tunnel – Reading and writing MODBUS records on an RS232 device using MQTT communication

¿Buscas alguna otra cosa?

Scenario details:

  • A special screen with RS232 serial input is available to communicate with using modbus protocol. It is possible to display characters on the screen by writing in its modbus registries through the RS232 serial port
  • We can control the screen remotely via 4G with an MTX modem with MTX-Tunnel firmware
  • To avoid connectivity problems (SIMs with private IPs) MQTT protocol will be used, so the modem will automatically connect to the configured MQTT broker
  • Once the modem is connected to the broker, the modem will periodically send its status (IP, coverage, etc) to a MQTT topic
  • In order to write and read the modbus registries on the screen, the commands AT ^ MTXTUNNEL = GETMODBUS, … and AT ^ MTXTUNNEL = SETMODBUS will be sent to the modem via MQTT

Solution: MTX-Tunnel firmware + MTX-Java-IoT/MTX-Java-T/MTX-Java-T2

Config.txt configuration file:

Configuration Observations
COMM2_baudrate: 9600
COMM2_bitsperchar: 8
COMM2_autocts: off
COMM2_autorts: off
COMM2_stopbits: 1
COMM2_parity: none
GPRS_apn: movistar.es
GPRS_login: MOVISTAR
GPRS_password: MOVISTAR
GPRS_timeout: 0
MTX_PIN: 0000
MTX_model: 199801421
MTX_mode: none
MTX_urc: off
MTX_ping: 30
MTX_pingIP: 8.8.8.8
MTX_invertedCom: on
MTX_portAux: modbusmaster
SMS_allPhones: on
SMS_ATEnabled: on
SMS_ATResponse: on
MQTT_enabled: on
MQTT_server: tcp://test.mosquitto.org:1883
MQTT_id: [IMEI]
MQTT_attopic1: [IMEI]/AT1
MQTT_atrtopic: [IMEI]/ATR
MQTT_qos: 1
MQTT_keepalive: 300
MQTT_persistent: off
DNS_enabled: on
DNS_mode: mqtt
DNS_mqttTopic: [IMEI]/mqtt
DNS_extended: off
DNS_period: 120
Serial port baud rate
8 bit data
No flow control
No flow control
1 stop bit
No parity
GPRS APN from your network operator
GPRS Login
GPRS Password
MTX-Tunnel is always connected
If SIM card has no PIN security, use 0000 value
MTX-Terminal modem model used
Gategay disabled
URC messages will not be sent
Minutes for connectivity supervision ping
Connectivity supervision IP address
RS232 port used as secondary
Secondary port as modbus master
All phones are authorized
AT commands enabled by SMS
Replies to AT commands enabled by SMS
MQTT service enabled
Broker IP/DNS specified, including identifying port
Identifier
Subscribed to this topic to receive commands
Topic to send replies to commands to
Service quality
Connection keep alive (300 seconds)
Not relevant
Sending status periodically
Sending mode
Topic to send status data to
Won’t send extended information (GPIOs)
Sending status every 120 seconds

Details:

  • Remember what modem will replace the [IMEI] tags with their IMEI (unique identifier)
  • The modem will subscribe to the MQTT topic “[IMEI]/AT1”, so all the AT commands sent to the MQTT topic will be received and executed by the modem
  • The responses to the executed AT commands will be sent to the MQTT topic: “[IMEI]/ATR”
  • Let’s say the screen allows to visualize 10 characters, and the modbus registries on the screen are from @10 to @19. Let’s suppose it is enough to write the ASCII code in those registries so they are visualized. The screen will have the modbus address @7
    If we want to write the word “HELLO” on the screen, which corresponds to the ASCII: 72, 79, 76 and 65, we will need to write those values in the modbus position 10, 11, 12 and 13, so we will send the following command via MQTT to the topic [IMEI]/AT1:
    AT^MTXTUNNEL=SETMODBUS,7;10;72;79;76;65
    This command writes in the modbus device with address @7, from register number @10, the values 72, 79, 76 and 65
  • If we want to read the modbus registries on the screen, we use the AT ^ MTXTUNNEL = GETMODBUS command. If we want to read the same modbus registries from the previous
    section, we send the following AT command via MQTT:
    AT^MTXTUNNEL=GETMODBUS,7;10;3;4
    This command reads the modbus device with address @7, from register number @10, using the modbus 3 command and will read 4 registers. The result of the AT command the modem will send to the “[IMEI]/ATR” topic will be:
    AT^MTXTUNNEL=GETMODBUS,7;10;3;4
    72,79,76,65
    OK