Package: modbus4mqtt
Modbus4MQTThttps://github.com/tjhowse/modbus4mqtthttps://pypi.org/project/modbus4mqtt/This is a gateway that translates between modbus TCP/IP and MQTT.The mapping of modbus registers to MQTT topics is in a simple YAML file.The most up-to-date docs will always be on Github.Similar softwareThere is already good software out there that can do what Modbus4MQTT does, but none
that I could find that has this functionality as its focus. Do one thing and do it well.
Modbus4mqtt is designed to fit in as a component of other systems, rather than trying to
be a complete solution.SolariotPVStats(Fork)InstallationPython module$pip3install--usermodbus4mqtt
$modbus4mqtt--helpDocker containerAlternatively you can run Modbus4MQTT in a Docker container. ADockerfileexample is provided.$dockerpulltjhowse/modbus4mqtt:latest
$dockerrunmodbus4mqtt--helpWhen launching inside the docker container you will either need to use one of the built-in YAMLs like/modbus4mqtt/modbus4mqtt/Sungrow_SH5k_20.yaml, or map your custom YAML into the container in a volume.YAML definitionLook at theSungrow SH5k-20configuration YAML for a working example.Modbus device settingsip:192.168.1.89port:502update_rate:5address_offset:0variant:sungrowscan_batching:100word_order:highlowField nameRequiredDefaultDescriptionipRequiredN/AThe IP address of the modbus device to be polled. Presently only modbus TCP/IP is supported.portOptional502The port on the modbus device to connect to.update_rateOptional5The number of seconds between polls of the modbus device.address_offsetOptional0This offset is applied to every register address to accommodate different Modbus addressing systems. In many Modbus devices the first register is enumerated as 1, other times 0. See section 4.4 of the Modbus spec.variantOptionalN/AAllows variants of the ModbusTcpClient library to be used. Setting this to 'sungrow' enables support of SungrowModbusTcpClient. This library transparently decrypts the modbus comms with sungrow SH inverters running newer firmware versions.scan_batchingOptional100Must be between 1 and 100 inclusive. Modbus read operations are more efficient in bigger batches of contiguous registers, but different devices have different limits on the size of the batched reads. This setting can also be helpful when building a modbus register map for an uncharted device. In some modbus devices a single invalid register in a read range will fail the entire read operation. By settingscan_batchingto1each register will be scanned individually. This will be very inefficient and should not be used in production as it will saturate the link with many read operations.word_orderOptional'highlow'Must be eitherhighloworlowhigh. This determines how multi-word values are interpreted.highlowmeans a 32-bit number at address 1 will have its high two bytes stored in register 1, and its low two bytes stored in register 2. The default is typically correct, as modbus has a big-endian memory structure, but this is not universal.Register settingsregisters:-pub_topic:"forced_charge/mode"set_topic:"forced_charge/mode/set"retain:truepub_only_on_change:falsetable:'holding'address:13140value_map:enabled:170disabled:85-pub_topic:"forced_charge/period_1/start_hours"set_topic:"forced_charge/period_1/start_hours/set"pub_only_on_change:truetable:'holding'address:13142-pub_topic:"voltage_in_mv"address:13000scale:1000-pub_topic:"first_bit_of_second_byte"address:13001mask:0x0010-pub_topic:"load_control/optimized/end_time"address:13013json_key:hours-pub_topic:"load_control/optimized/end_time"address:13014json_key:minutes-pub_topic:"external_temperature"address:13015type:int16-pub_topic:"minutes_online"address:13016type:uint32This section of the YAML lists all the modbus registers that you consider interesting.Field nameRequiredDefaultDescriptionaddressRequiredN/AThe decimal address of the register to read from the device, starting at 0. Many modbus devices enumerate registers beginning at 1, so beware.pub_topicOptionalN/AThis is the topic to which the value of this register will be published.set_topicOptionalN/AValues published to this topic will be written to the Modbus device. Cannot yet be combined with json_key. Seehttps://github.com/tjhowse/modbus4mqtt/issues/23for details.retainOptionalfalseControls whether the value of this register will be published with the retain bit set.pub_only_on_changeOptionaltrueControls whether this register will only be published if its value changed from the previous poll.tableOptionalholdingThe Modbus table to read from the device. Must be 'holding' or 'input'.value_mapOptionalN/AA series of human-readable and raw values for the setting. This will be used to translate between human-readable values via MQTT to raw values via Modbus. If a value_map is set for a register the interface will reject raw values sent via MQTT. If value_map is not set the interface will try to set the Modbus register to that value. Note that the scale is applied after the value is read from Modbus and before it is written to Modbus.scaleOptional1After reading a value from the Modbus register it will be multiplied by this scalar before being published to MQTT. Values published on this register'sset_topicwill be divided by this scalar before being written to Modbus.maskOptional0xFFFFThis is a 16-bit number that can be used to select a part of a Modbus register to be referenced by this register. For example a mask of0xFF00will map to the most significant byte of the 16-bit Modbus register ataddress. A mask of0x0001will reference only the least significant bit of this register.json_keyOptionalN/AThe value of this register will be published to its pub_topic in JSON format. E.G.{ key: value }Registers with a json_key specified can share a pub_topic. All registers with shared pub_topics must have a json_key specified. In this way, multiple registers can be published to the same topic in a single JSON message. If any of the registers that share a pub_topic have the retain field set that will affect the published JSON message. Conflicting retain settings are invalid. The keys will be alphabetically sorted.typeOptionaluint16The type of the value stored at the modbus address provided. Only uint16 (unsigned 16-bit integer), int16 (signed 16-bit integer), uint32, int32, uint64 and int64 are currently supported.