On a TRB246, I want to give some Modbus data a special JSON format and I think Lua is the way to go. I’ve downloaded the sample script and I see that “data“ is an object probably containing the Modbus data, maybe a list of values… I don’t know.
local json = require("luci.jsonc")
function handle_format_request(data)
return json.stringify(data)
end
Is there a document where this is explained? I’d also like to add some other data to the JSON object, such as Server name and Request name. Are these fields also part of “data“?
On Teltonika routers, the handle_format_request(data) function is part of the Modbus Data to Server feature. The Lua script is only a formatter: it receives the Modbus values already collected by the router, and you return whatever JSON structure you want.
When we select JSON as the format type, we are allowed to add multiple fields to the JSON object, including Modbus Server name and Request name. This information tells us about the origin of the information (Modbus slave), type of data, etc.
Problem solved. I simply used the default Lua script returning the whole JSON data object and I’m now able to see all the available fields, including timestamp, server name, request name, server id, etc. Everything we need is in the data object.