Want to query modbus sensor via mqtt

I am using a vibration sensor which is connected through modbus rtu to lorawan end node and end node is in turn connected with the lorawan gateway.so can i send modbus commands from node red platform to query the sensor values.?

This image shows data received from sensor when giving modbus command on gateways network server page.
modbus_cmd:01030001000D.

Node-red supports MQTT

Node-red can communicate to modbus devices by contrib node.

Some modbus contrib nodes allow you to dynamically request read address.

Putting that together, the answer is yes, you can query modbus via MQTT.

1 Like

Hello Steve its really nice to meet u after long time,
sensor is sending in 8 bytes
01-dev id
03-function code(holding registers)
1a
00006cea-operational status
42bfd4de-total acceleration
3eac8b33-x axis velocity
3ea752963-y axis velocity
3ea9eda2-z axis velocity
41d9febd-temperature
428c0159-noise,
Steve which are those nodes which dynamically read modbus registers?


operational status-95.915

modbus-getter and/or modbus-flex-getter nodes in the node-red-contrib-modbus package

Steve but how would the flow be wired with mqtt node so that it sends commands to gateway which in turn sends the commands to sensor.can u create some dummy flow which explains my case.?

(1) MQTT in --> (2) [something that converts the MQTT payload to a flex getter request object] --> (3)
modbus getter --> (4) [something that converts result into sensible values suitable for MQTT] --> (5) MQTT out

TBH, you clearly havent tried so I am not going to "create some dummy flow" for you when you havent tried.

  • figure out how you expect the MQTT in payload to look
  • figure out how to convert your MQTT in payload into a getter request
    • (TIP use a function node, debug nodes, read the extensive help and examples the modbus nodes provide)
  • figure out how to read modbus addresses using the getter nodes
  • figure out how you expect the MQTT out result payload to look

When you get stuck, I will certainly help.

1 Like

-This is how we send downlink command to query modbus sensor.


[{"id":"d7118925.a9f4d8","type":"function","z":"9e668262.26725","name":"","func":"msg.payload =\n{\n    'fc':3,\n    'unitid':1,\n    'address':40001,\n    'quantity':20\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":240,"y":320,"wires":[["f7cf2fb2.5f03"]]}]

[{"id":"d7118925.a9f4d8","type":"function","z":"9e668262.26725","name":"","func":"msg.payload =\n{\n    'fc':3,\n    'unitid':1,\n    'address':40001,\n    'quantity':20\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":240,"y":320,"wires":[["f7cf2fb2.5f03"]]}]

Steve how would the message structure be like?as we have to include device_eui, type(hex), Payload(modbus cmd), port for sending a downlink command.And I am not sure why we are using mqtt input node as we can directly do it by an inject node with mqtt out node by making data-type as JSON.Am i missing something?.

If you can do this by inject node, then send the EXACT same payload via MQTT

Alternatively, if you only send part of the value via MQTT, send that to a function or template node or change node and build the correct object so that it 100% matches the object you have working in the inject.

Steve according to you But what is the definition of correct object in this case.what all parameters should be taken into consideration while sending the payload to lorawan gateway?

I have no idea. I dont know what you are trying to achieve.

your question was...
"Want to query modbus via MQTT"

So i said...

that still remains true.



Maybe if you start again. Provide clear information of what you need.

Steve i think you already know what i am asking , basically earlier we used to send modbus commands form gateways web interface to query the sensor values. Now we want to send downlink commands(scheduler in hmi terms) to query the senosr values every 3 seconds from node red platform.for which u said to use the mqtt input node and flex getter request object.but how to use these nodes in a way that u were describing.Steve you said something about converting mqtt payload to flex getter request object which i don't know so i ask your support.

No, not really. Plus I dont know where you are up to in this journey.

Lets take this one step at a time.

  • Can you perform the modbus read using flex getter?

    • If so, what do you send to the modbus node to make it do work (show me the debug value)
    • how is the flex getter setup (show me)
  • What value do you have in MQTT and what is that value to be used for?

    • as a dynamic modbus address?
    • as a dynamic station id?

steve i am receving this when i send this command in gateways interface page

so now we have to add mqtt input node.
(upload://ievo0GNpSfoy4pFOHuKiK1PVFGL.jpeg)

You don't answer questions.

I can't help.

I don't have enough information or the equivalent equipment to help.

Modbus Configuration
slaveid-1,baudrate-9600,parity-none stopbit-1 of sensor. type i selected as tcp. what should i set the ip(ip of gateway)?should i place function node in between mqtt in and flex getter node?
Steve please reply fast need your support and i am answering all your questions.


[{"id":"caee3e0c.98f17","type":"modbus-flex-getter","z":"97c98aeb.127198","name":"","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"4412ae43.cdf648","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":390,"y":140,"wires":[[],[]]},{"id":"4412ae43.cdf648","type":"modbus-client","name":"modbusclient","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.1.9","tcpPort":"502","tcpType":"DEFAULT","serialPort":"COM3","serialType":"RTU-BUFFERD","serialBaudrate":"19200","serialDatabits":"8","serialStopbits":"1","serialParity":"even","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":false,"reconnectTimeout":"2000","parallelUnitIdsAllowed":false}]

If you read the built in help it shows you what you need to send to the flex getter...

Questions...

  • What Address and how many elements do you want to read from the modbus device?

  • What comes out of the MQTT node (show me)

    • what part of the MQTT payload do you need to use for querying the modbus device?




steve thats what i am saying nothing is coming out of mqtt input node.I am sending commands to read 13 registers from gateways embedded network server,not node red.

You hadn't made that clear.

Why is nothing coming out of MQTT? What sends to that topic?

Now i am totally lost.

Where are you "sending commands" to?
using what method of communication?