Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Modbus TCP <-> MQTT glue. YAML configuration. Robust.

License

NotificationsYou must be signed in to change notification settings

tjhowse/modbus4mqtt

Repository files navigation

https://github.com/tjhowse/modbus4mqtt

https://pypi.org/project/modbus4mqtt/

codecov

This is a gateway that translates between modbus 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 software

There is already good software out there that can do what Modbus4MQTT does, but nonethat 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 tobe a complete solution.

Installation

Python module

$ pip3 install --user modbus4mqtt$ modbus4mqtt --help

Docker container

Alternatively you can run Modbus4MQTT in a Docker container. ADockerfile example is provided.

$ docker pull tjhowse/modbus4mqtt:latest$ docker run modbus4mqtt --help

When 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 definition

Look at theSungrow SH5k-20 configuration YAML for a working example.

Modbus device settings

ip:192.168.1.89port:502update_rate:5address_offset:0variant:sungrowscan_batching:100word_order:highlow
Field nameRequiredDefaultDescription
ipRequiredN/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.
device_addressOptional1The modbus device address ("unit") of the target device
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.
variantOptional'tcp'Allows modbus variants to be specified. See below list for supported variants.
write_modeOptional'single'Which modbus write function code to usesingle for06 ormulti for16
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_batching to1 each 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 eitherhighlow orlowhigh. This determines how multi-word values are interpreted.highlow means 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.

Modbus variants

The variant is split into two: The connection variant and the framer variant using the format<framer>-over-<connection> or just<connection>.For examplertu-over-tcp orascii-over-tls. The framer is optional allowing to simply specifytcp, which makes it use the default modbus-TCP framer.Supported framer variants are:ascii,binary,rtu andsocket.The following connection variants are supported:tcp,udp,tls,sungrow, with the latter one transparently decrypting traffic from sungrow SH inverters running newer firmware versions.

Register settings

registers:  -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:uint32

This section of the YAML lists all the modbus registers that you consider interesting.

Field nameRequiredDefaultDescription
addressRequiredN/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. See#23 for 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_topic will 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 of0xFF00 will map to the most significant byte of the 16-bit Modbus register ataddress. A mask of0x0001 will 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.

About

Modbus TCP <-> MQTT glue. YAML configuration. Robust.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp