Brush up modbus doc (#3294)

* Brush up modbus doc

Fixed punctuation and terminology, added some explanation.

* Update modbus_controller.rst

* Update modbus_controller.rst

* Update modbus_controller.rst

fix SeeAlso

* Update modbus_controller.rst

* Update modbus_controller.rst
This commit is contained in:
H. Árkosi Róbert 2023-10-27 07:03:27 +02:00 committed by GitHub
parent 75d6d8c2b6
commit a62d7ab193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,28 +2,31 @@ Modbus Controller
================= =================
.. seo:: .. seo::
:description: Instructions for setting up the Modbus Controller component. :description: Instructions for setting up the ModBUS Controller component.
:image: modbus.png :image: modbus.png
The ``modbus_controller`` component creates a RS485 connection to control a modbus device The ``modbus_controller`` component creates a RS485 connection to control a ModBUS slave device, letting your ESPHome node to act as a ModBUS master.
You can access the coils and registers from your slave ModBUS device as sensors, switches or various other ESPHome components and present them to your favorite Home Automation system.
.. figure:: /images/modbus.png .. figure:: /images/modbus.png
:align: center :align: center
:width: 25% :width: 25%
The ``modbus_controller`` component uses the modbus component The ``modbus_controller`` component relies on the :doc:`/components/modbus`.
Hardware setup Hardware setup
-------------- --------------
A RS 485 module connected to an ESP32, for example: You need an RS485 transceiver module:
.. figure:: /images/rs485.jpg .. figure:: /images/rs485.jpg
See `How is this RS485 Module Working? <https://electronics.stackexchange.com/questions/244425/how-is-this-rs485-module-working>`__ on stackexchange for more details See `How is this RS485 module working? <https://electronics.stackexchange.com/questions/244425/how-is-this-rs485-module-working>`__ on stackexchange for more details.
The controller connects to the UART of the MCU. For ESP32 GPIO PIN 16 to TXD PIN 17 to RXD are the default ports but any other pins can be used as well. 3.3V to VCC and GND to GND. The transceiver connects to the UART of the MCU. For ESP32, pin ``16`` to ``TXD`` and pin ``17`` to ``RXD`` are the default ones but any other pins can be used as well. ``3.3V`` to ``VCC`` and naturally ``GND`` to ``GND``.
On the bus side, you need 120 Ohm termination resistors at the ends of the bus cable as per ModBUS standard. Some transceivers have this already solderes onboard, and some slave devices may have them preinstalled with a jumper or a dip switch.
.. note:: .. note::
@ -31,7 +34,7 @@ The controller connects to the UART of the MCU. For ESP32 GPIO PIN 16 to TXD PI
For hardware serial only a limited set of pins can be used. Either ``tx_pin: GPIO1`` and ``rx_pin: GPIO3`` or ``tx_pin: GPIO15`` and ``rx_pin: GPIO13``. For hardware serial only a limited set of pins can be used. Either ``tx_pin: GPIO1`` and ``rx_pin: GPIO3`` or ``tx_pin: GPIO15`` and ``rx_pin: GPIO13``.
The disadvantage of using the hardware uart is that you can't use serial logging because the serial logs would be sent to the modbus device and cause errors. The disadvantage of using the hardware uart is that you can't use serial logging because the serial logs would be sent to the ModBUS device and cause errors.
Serial logging can be disabled by setting ``baud_rate: 0``. Serial logging can be disabled by setting ``baud_rate: 0``.
@ -48,29 +51,27 @@ The controller connects to the UART of the MCU. For ESP32 GPIO PIN 16 to TXD PI
Configuration variables: Configuration variables:
------------------------ ------------------------
- **modbus_id** (*Optional*, :ref:`config-id`): Manually specify the ID of the modbus hub. - **modbus_id** (*Optional*, :ref:`config-id`): Manually specify the ID of the ``modbus`` hub.
- **address** (**Required**, :ref:`config-id`): The modbus address of the device - **address** (**Required**, :ref:`config-id`): The ModBUS address of the slave device
Specify the modbus device address of the.
- **command_throttle** (*Optional*, :ref:`config-time`): minimum time in between 2 requests to the device. Default is 0ms - **command_throttle** (*Optional*, :ref:`config-time`): minimum time in between 2 requests to the device. Default is ``0ms``.
Because some modbus devices limit the rate of requests the interval between sending requests to the device can be modified. Some ModBUS slave devices limit the rate of requests from the master, the interval between sending requests can be altered.
- **update_interval** (*Optional*, :ref:`config-time`): The interval that the sensors should be checked. - **update_interval** (*Optional*, :ref:`config-time`): The interval that the sensors should be checked.
Defaults to 60 seconds. Defaults to 60 seconds.
- **offline_skip_updates** (*Optional*, integer): When a controller doesn't respond to a command, it is - **offline_skip_updates** (*Optional*, integer): When a slave doesn't respond to a command, it is
marked as offline, you can specify how many updates will be skipped while it is offline. If using a bus with multiple marked as offline, you can specify how many updates will be skipped while it is offline. If using a bus with multiple
controllers, this avoids waiting for timeouts allowing to read other controllers in the same bus. When the controller slaves, this avoids waiting for timeouts allowing to read other slaves in the same bus. When the slave
responds to a command, it'll be marked online again. responds to a command, it'll be marked online again.
Example Example
------- -------
The following code create a modbus_controller hub talking to a modbus device at address 1 with 115200 bps The following code creates a ``modbus_controller`` hub talking to a ModBUS device at address ``1`` with ``115200`` bps
ModBUS sensors can be directly defined (inline) under the ``modbus_controller`` hub or as standalone components
Modbus sensors can be directly defined (inline) under the modbus_controller hub or as standalone components
Technically there is no difference between the "inline" and the standard definitions approach. Technically there is no difference between the "inline" and the standard definitions approach.
.. code-block:: yaml .. code-block:: yaml
@ -88,8 +89,7 @@ Technically there is no difference between the "inline" and the standard definit
modbus_controller: modbus_controller:
- id: epever - id: epever
## the Modbus device addr address: 0x1 ## address of the ModBUS slave device on the bus
address: 0x1
modbus_id: modbus1 modbus_id: modbus1
setup_priority: -10 setup_priority: -10
@ -100,7 +100,7 @@ Technically there is no difference between the "inline" and the standard definit
id: rtc_clock id: rtc_clock
internal: true internal: true
register_type: holding register_type: holding
address: 0x9013 address: 0x9013 ## address of the register inside the ModBUS slave device
register_count: 3 register_count: 3
raw_encode: HEXBYTES raw_encode: HEXBYTES
response_size: 6 response_size: 6
@ -208,7 +208,6 @@ To gather some of these bits as binary sensors in ESPHome, use ``bitmask``:
Protocol decoding example Protocol decoding example
------------------------- -------------------------
@ -298,9 +297,8 @@ Protocol decoding example
accuracy_decimals: 0 accuracy_decimals: 0
To minimize the required transactions all registers with the same base address are read in one request. To minimize the required transactions all registers with the same base address are read in one request.
The response is mapped to the sensor based on register_count and offset in bytes. The response is mapped to the sensor based on ``register_count`` and offset in bytes. For example:
**Request** **Request**
@ -331,7 +329,7 @@ The response is mapped to the sensor based on register_count and offset in bytes
+--------+------------+--------------------+--------------------------------------------+ +--------+------------+--------------------+--------------------------------------------+
| offset | data | value (type) | description | | offset | data | value (type) | description |
+========+============+====================+============================================+ +========+============+====================+============================================+
| H | 0x1 (01) | | device address | | H | 0x1 (01) | | device address |
+--------+------------+--------------------+--------------------------------------------+ +--------+------------+--------------------+--------------------------------------------+
| H | 0x4 (04) | | function code | | H | 0x4 (04) | | function code |
+--------+------------+--------------------+--------------------------------------------+ +--------+------------+--------------------+--------------------------------------------+
@ -379,161 +377,157 @@ The response is mapped to the sensor based on register_count and offset in bytes
+--------+------------+--------------------+--------------------------------------------+ +--------+------------+--------------------+--------------------------------------------+
.. note::
Note Write support is only implemented for switches and selects.
---- However the C++ code provides the required API to write to a ModBUS device.
Write support is only implemented for switches and selects. These methods can be called from a lambda.
However the C++ code provides the required API to write to a modbus device.
These methods can be called from a lambda. Here is an example how to set config values to for an EPEVER Trace AN controller.
The code synchronizes the localtime of MCU to the epever controller
The time is set by writing 12 bytes to register 0x9013.
Then battery charge settings are sent.
Here is an example how to set config values to for an EPEVER Trace AN controller. .. code-block:: yaml
The code synchronizes the localtime of MCU to the epever controller
The time is set by writing 12 bytes to register 0x9013.
Then battery charge settings are sent.
esphome:
on_boot:
## configure controller settings at setup
## make sure priority is lower than setup_priority of modbus_controller
priority: -100
then:
- lambda: |-
// get local time and sync to controller
time_t now = ::time(nullptr);
struct tm *time_info = ::localtime(&now);
int seconds = time_info->tm_sec;
int minutes = time_info->tm_min;
int hour = time_info->tm_hour;
int day = time_info->tm_mday;
int month = time_info->tm_mon + 1;
int year = time_info->tm_year % 100;
esphome::modbus_controller::ModbusController *controller = id(epever);
// if there is no internet connection localtime returns year 70
if (year != 70) {
// create the payload
std::vector<uint16_t> rtc_data = {uint16_t((minutes << 8) | seconds), uint16_t((day << 8) | hour),
uint16_t((year << 8) | month)};
// Create a ModBUS command item with the time information as the payload
esphome::modbus_controller::ModbusCommandItem set_rtc_command =
esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command(controller, 0x9013, 3, rtc_data);
// Submit the command to the send queue
epever->queue_command(set_rtc_command);
ESP_LOGI("ModbusLambda", "EPSOLAR RTC set to %02d:%02d:%02d %02d.%02d.%04d", hour, minutes, seconds, day, month,
year + 2000);
}
// Battery settings
// Note: these values are examples only and apply my AGM Battery
std::vector<uint16_t> battery_settings1 = {
0, // 9000 Battery Type 0 = User
0x0073, // 9001 Battery Cap 0x55 == 115AH
0x012C, // 9002 Temp compensation -3V /°C/2V
0x05DC, // 9003 0x5DC == 1500 Over Voltage Disconnect Voltage 15,0
0x058C, // 9004 0x58C == 1480 Charging Limit Voltage 14,8
0x058C, // 9005 Over Voltage Reconnect Voltage 14,8
0x05BF, // 9006 Equalize Charging Voltage 14,6
0x05BE, // 9007 Boost Charging Voltage 14,7
0x0550, // 9008 Float Charging Voltage 13,6
0x0528, // 9009 Boost Reconnect Charging Voltage 13,2
0x04C4, // 900A Low Voltage Reconnect Voltage 12,2
0x04B0, // 900B Under Voltage Warning Reconnect Voltage 12,0
0x04BA, // 900c Under Volt. Warning Volt 12,1
0x04BA, // 900d Low Volt. Disconnect Volt. 11.8
0x04BA // 900E Discharging Limit Voltage 11.8
};
.. code-block:: yaml // Boost and equalization periods
std::vector<uint16_t> battery_settings2 = {
0x0000, // 906B Equalize Duration (min.) 0
0x0075 // 906C Boost Duration (aka absorb) 117 mins
};
esphome::modbus_controller::ModbusCommandItem set_battery1_command =
esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command(controller, 0x9000, battery_settings1.size() ,
battery_settings1);
esphome: esphome::modbus_controller::ModbusCommandItem set_battery2_command =
on_boot: esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command(controller, 0x906B, battery_settings3.size(),
## configure controller settings at setup battery_settings2);
## make sure priority is lower than setup_priority of modbus_controller delay(200) ;
priority: -100 controller->queue_command(set_battery1_command);
then: delay(200) ;
- lambda: |- controller->queue_command(set_battery2_command);
// get local time and sync to controller ESP_LOGI("ModbusLambda", "EPSOLAR Battery set");
time_t now = ::time(nullptr);
struct tm *time_info = ::localtime(&now);
int seconds = time_info->tm_sec;
int minutes = time_info->tm_min;
int hour = time_info->tm_hour;
int day = time_info->tm_mday;
int month = time_info->tm_mon + 1;
int year = time_info->tm_year % 100;
esphome::modbus_controller::ModbusController *controller = id(epever);
// if there is no internet connection localtime returns year 70
if (year != 70) {
// create the payload
std::vector<uint16_t> rtc_data = {uint16_t((minutes << 8) | seconds), uint16_t((day << 8) | hour),
uint16_t((year << 8) | month)};
// Create a modbus command item with the time information as the payload
esphome::modbus_controller::ModbusCommandItem set_rtc_command =
esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command(controller, 0x9013, 3, rtc_data);
// Submit the command to the send queue
epever->queue_command(set_rtc_command);
ESP_LOGI("ModbusLambda", "EPSOLAR RTC set to %02d:%02d:%02d %02d.%02d.%04d", hour, minutes, seconds, day, month,
year + 2000);
}
// Battery settings
// Note: these values are examples only and apply my AGM Battery
std::vector<uint16_t> battery_settings1 = {
0, // 9000 Battery Type 0 = User
0x0073, // 9001 Battery Cap 0x55 == 115AH
0x012C, // 9002 Temp compensation -3V /°C/2V
0x05DC, // 9003 0x5DC == 1500 Over Voltage Disconnect Voltage 15,0
0x058C, // 9004 0x58C == 1480 Charging Limit Voltage 14,8
0x058C, // 9005 Over Voltage Reconnect Voltage 14,8
0x05BF, // 9006 Equalize Charging Voltage 14,6
0x05BE, // 9007 Boost Charging Voltage 14,7
0x0550, // 9008 Float Charging Voltage 13,6
0x0528, // 9009 Boost Reconnect Charging Voltage 13,2
0x04C4, // 900A Low Voltage Reconnect Voltage 12,2
0x04B0, // 900B Under Voltage Warning Reconnect Voltage 12,0
0x04BA, // 900c Under Volt. Warning Volt 12,1
0x04BA, // 900d Low Volt. Disconnect Volt. 11.8
0x04BA // 900E Discharging Limit Voltage 11.8
};
// Boost and equalization periods uart:
std::vector<uint16_t> battery_settings2 = { id: mod_bus
0x0000, // 906B Equalize Duration (min.) 0 tx_pin: 19
0x0075 // 906C Boost Duration (aka absorb) 117 mins rx_pin: 18
}; baud_rate: 115200
esphome::modbus_controller::ModbusCommandItem set_battery1_command = stop_bits: 1
esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command(controller, 0x9000, battery_settings1.size() ,
battery_settings1);
esphome::modbus_controller::ModbusCommandItem set_battery2_command = modbus:
esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command(controller, 0x906B, battery_settings3.size(), #flow_control_pin: 23
battery_settings2); send_wait_time: 200ms
delay(200) ; id: mod_bus_epever
controller->queue_command(set_battery1_command);
delay(200) ;
controller->queue_command(set_battery2_command);
ESP_LOGI("ModbusLambda", "EPSOLAR Battery set");
uart: modbus_controller:
id: mod_bus - id: epever
tx_pin: 19 ## the Modbus device addr
rx_pin: 18 address: 0x1
baud_rate: 115200 modbus_id: mod_bus_epever
stop_bits: 1 command_throttle: 0ms
setup_priority: -10
update_interval: ${updates}
modbus: sensor:
#flow_control_pin: 23 - platform: modbus_controller
send_wait_time: 200ms modbus_controller_id: epever
id: mod_bus_epever id: array_rated_voltage
name: "array_rated_voltage"
modbus_controller: address: 0x3000
- id: epever unit_of_measurement: "V"
## the Modbus device addr register_type: read
address: 0x1 value_type: U_WORD
modbus_id: mod_bus_epever accuracy_decimals: 1
command_throttle: 0ms filters:
setup_priority: -10 - multiply: 0.01
update_interval: ${updates}
sensor:
- platform: modbus_controller
modbus_controller_id: epever
id: array_rated_voltage
name: "array_rated_voltage"
address: 0x3000
unit_of_measurement: "V"
register_type: read
value_type: U_WORD
accuracy_decimals: 1
filters:
- multiply: 0.01
- platform: modbus_controller
modbus_controller_id: epever
id: array_rated_current
name: "array_rated_current"
address: 0x3001
unit_of_measurement: "A"
register_type: read
value_type: U_WORD
accuracy_decimals: 2
filters:
- multiply: 0.01
- platform: modbus_controller
modbus_controller_id: epever
id: array_rated_power
name: "array_rated_power"
address: 0x3002
unit_of_measurement: "W"
register_type: read
value_type: U_DWORD_R
accuracy_decimals: 1
filters:
- multiply: 0.01
- platform: modbus_controller
modbus_controller_id: epever
id: array_rated_current
name: "array_rated_current"
address: 0x3001
unit_of_measurement: "A"
register_type: read
value_type: U_WORD
accuracy_decimals: 2
filters:
- multiply: 0.01
- platform: modbus_controller
modbus_controller_id: epever
id: array_rated_power
name: "array_rated_power"
address: 0x3002
unit_of_measurement: "W"
register_type: read
value_type: U_DWORD_R
accuracy_decimals: 1
filters:
- multiply: 0.01
See Also See Also
-------- --------
- :doc:`/components/sensor/modbus_controller` - :doc:`/components/modbus`
- :doc:`/components/binary_sensor/modbus_controller` - :doc:`/components/binary_sensor/modbus_controller`
- :doc:`/components/text_sensor/modbus_controller` - :doc:`/components/text_sensor/modbus_controller`
- :doc:`/components/switch/modbus_controller` - :doc:`/components/switch/modbus_controller`
- :doc:`/components/number/modbus_controller` - :doc:`/components/number/modbus_controller`
- :doc:`/components/output/modbus_controller` - :doc:`/components/output/modbus_controller`
- `ModBUS RTU Protocol Description <https://www.modbustools.com/modbus.html>`__
- `EPEVER MPPT Solar Charge Controller (Tracer-AN Series) <https://devices.esphome.io/devices/epever_mptt_tracer_an>`__ - `EPEVER MPPT Solar Charge Controller (Tracer-AN Series) <https://devices.esphome.io/devices/epever_mptt_tracer_an>`__
- `Modbus RTU Protocol Description <https://www.modbustools.com/modbus.html>`__ - `Genvex, Nibe, Alpha-Innotec heat recovery ventilation <https://devices.esphome.io/devices/Genvex-Nibe-AlphaInnotec-heat-recovery-ventilation>`__
- :ghedit:`Edit` - :ghedit:`Edit`