Lua script to receive MQTT or HTTP payloads and send it to serial port rs232 (not modbus)

Hi, I need to implement this scenario:

  1. TRB245 subscribe to MQTT topic or make HTTP requests to get an String.
    2)The String must be to send to serial port (rs232. Not modbus) where a serial device is connected.
  2. The TRB245 will receive a response from Serial device and this response should be publish in an MQTT topics or send via HTTP request to a server.

Would be great if the TRBxxx support MQTT to Serial communication like it do for modbus.

Serial over IP is not an option for this case. I need to know how to make this using Lua script. I tried to see some Lua examples but the documentation is poor.
Thanks!

Hello there,

Writing custom Lua scripts is unfortunately out of our scope here, so we’re not able to help you out much. However, recently, I’ve had a user that had a very similar issue to yours, and they have shared their script, which might help you get started: RUTX11 Modbus MQTT - #9 by jsampaio

Regards,
M.

Hi, thanks for your reply. The script shared doesn´t not match my needs. I could not find the final solution yet but I shared a lua script (develop in progress) to:

  1. Receive an string making an HTTP request to an external Server.
  2. Open the serial port in TRB and send the string received (in fact the string is a command in HEXA
  3. Wait for Serial port reply
  4. prepare all the data as a table to return it to “Data to Server” and be publish via MQTT.
    The part of this script that doesn’t work yet is the one related to the serial port. I’m still working with the help of what I found on the forum and chatgpt. The only part of this script that actually

The Script

local http = require(“socket.http”)

function handle_data_request()
local command = “XAAAA”
local response = “NO Response”
local serialport = “/dev/ttyS0”
local http_url = “URL Server”

-- HTTP get to receive command
local body, status = http.request(http_url)
if status == 200 and body ~= nil and body ~= "" then
    command = body
end

-- Serial port setup
os.execute("stty -F " .. puerto .. " 9600 cs8 -cstopb -parenb -ixon -ixoff -crtscts")

-- Send command through serial port
local f = io.open(serialport, "r+b")
if f then
	f:write(command .. "\r\n")
	f:flush()
	os.execute("sleep 1")
	local r = f:read(64)
	if r then
		response = r
	end
	f:close()
end

-- Return datas as a table for Teltonika Data to Server
return {
	version = data_sender.version,
	time = os.time(),
	date = os.date(),
	command_http = command,
	serial_response = response
}

end

function plugin_init()
return {
{ name = “version”, help = “plugin version”, type = “STRING” },
{ name = “time”, help = “Unix Time”, type = “INT” },
{ name = “date”, help = “Date”, type = “STRING” },
{ name = “command_http”, help = “Command received from HTTP”, type = “STRING” },
{ name = “serial_response”, help = “Serial port reply”, type = “STRING” }
}
end

1 Like

Thank you very much for being so kind and sharing the script, in case anyone needs something similar to this, it will help us out and we’ll be able to share this with them.

Thank you once again,
M.

Hi Matas, the community support is fundamental to solve this kind of issues.
Would be great if Teltonika developers add Serial as an Input in “Data to Server” to solve this requirements without the need to develop a script.
+1 to Dev Ops requirements to include this feature soon.

Hello,

I can definitely register this as a suggestion for the R&D, however, it is necessary to have more requests from more users for this specific feature for it to be implemented faster, or to be implemented at all.

Regards,
M.