Modbus client to HTTP

I’m reading sensor data via RS485 with a TRB256 gateway via Modbus Serial Client. I need to send these to an HTTP API server in JSON.

The wiki documentation is not quite clear (to me at least) - tried several variations of custom JSON and LUA scripts but without much success.

How to pass variables to the JSON output from the Modbus serial client? I tried custom JSON templates with %% variables but nothing seems to work.

Hello,

Could you clarify what variables you are referring to? Could you also share screenshots of the custom templates you have already tried?


In the meantime, here’s a Lua script example that might be useful and demonstrates how to format Modbus responses into JSON:

-- Available variables:
-- data_sender.section - config section name
-- data_sender.version - LUA plugin version
-- env - data to be formatted
--
-- String value must be returned

local json = require("luci.jsonc")

function handle_format_request(data)
    local result = {}

    result.meterID = data[1]["server_name"]
    result.values = {}

    for _, modbus_response in pairs(data) do
        table.insert(result.values, {
            modbus_response["date"],
            tonumber(modbus_response["data"]),
        })
    end

    return json.stringify(result)
end

This script will produce output similar to:

[
  {
    "values": [
      ["11/10/2024 08:56:27", 6505],
      ["11/10/2024 08:56:27", 6505]
    ],
    "meterID": "data"
  }
]

Best regards,

This topic was automatically closed after 60 days. New replies are no longer allowed.