# Communication Protocols

The device provides two protocol links: MQTT/MQTTS and Modbus RTU. MQTT handles platform connectivity, property/event reporting, and command delivery; Modbus RTU acts as a master that polls RS485 slaves and maps the results to local peripheral data.

## Page Guide

- **What This Page Covers**: Configuration entries for MQTT/MQTTS and Modbus RTU, topic types, rule integration, and the troubleshooting order.
- **Use Cases**: For users who need to connect the device to an IoT platform, or poll Modbus slaves over RS485 and link them with local rules.
- **Prerequisites**: The device network is online; when using MQTTS, enable `mqtts` in "Feature Configuration" first.
- **Acceptance Points**: `/api/mqtt/status` shows `connected`, Modbus poll success/failure counters behave as expected, and the platform receives reports and can deliver commands.

```mermaid
flowchart LR
  A[Web Config] --> B[ProtocolManager]
  B --> C["Network Adapter\nWiFi / W5500 / 4G"]
  C --> D[MQTT/MQTTS]
  B --> E[UART/RS485]
  E --> F[Modbus RTU Master]
  F --> G[Register Mapping / Peripheral Rules]
  D --> H[Platform Reports and Commands]
  G --> H
```

## Configuration Entries

| Protocol | Page | API |
| --- | --- | --- |
| MQTT/MQTTS | Communication Protocols → MQTT | `/api/protocol/mqtt/config`, `/api/mqtt/status` |
| Modbus RTU | Communication Protocols → Modbus RTU | `/api/protocol/modbus-rtu/config`, `/api/modbus/*` |
| [Seniverse Weather and Legacy Resource Migration](https://www.fastbee.cn/doc/en/device/v2/protocols/remote-resources.md) | Device Screen → Seniverse Weather component | `/api/screen-panel/weather` |

Confirm the network is online first, then save the protocol configuration. After saving, check the status and device logs; do not judge connection success solely by "saved".

![MQTT Configuration](https://www.fastbee.cn/doc/assets/protocol-mqtt-config.d4iGQpef.png)

## MQTT/MQTTS

Supported:

* Plain MQTT (commonly port `1883`) and TLS-encrypted MQTTS (commonly port `8883`).
* Custom Client ID, username/password, Keep Alive, and last-will messages.
* Auto-reconnect, event reporting, property/monitoring data reporting, and platform command subscription.
* WiFi, W5500, and EC801E-CN 4G network transports.

Configuration steps:

1. Confirm the device network is online and enable the connection on the MQTT page; when using MQTTS, also enable `mqtts` in "Feature Configuration".
2. Fill in the server, port, Client ID, and credentials on the MQTT page.
3. Select the required publish/subscribe topics, save, and connect.
4. Confirm `connected` in `/api/mqtt/status` or the dashboard, then verify platform messages.

`/api/mqtt/test` saves a restore record first, then temporarily applies the parameters for a connection-only test; business reporting and subscriptions are isolated, and restoration is attempted afterward. If restoration is pending, keep the evidence and verify the status; a successful handshake does not mean the platform received business messages.

### Topic Configuration

Topics are determined by the publish/subscribe lists on the page; common types include:

| Type | Purpose |
| --- | --- |
| `property` | Sensor and peripheral property reporting |
| `info` | Device information/configuration messages |
| `event` | Button, fault, alarm, and device events |
| `monitor` | Status monitoring |
| `ntp` | Time synchronization |
| HTTP upgrade | Implemented remote upgrade control and results, subject to compile and partition capabilities |

Do not hardcode topic prefixes in integration programs; read the actual configuration or use platform-assigned topics.

## Modbus RTU

The current firmware provides Modbus **master** capability by default:

* UART TX/RX and optional DE pin configuration.
* Multiple slave addresses, function codes, register ranges, and data-type mapping.
* Scheduled polling, adjacent-register read merging, failure retries, and timeout statistics.
* Coil/register writes, and integration through Peripheral Execution rules.

Configuration steps:

1. Wire the RS485 transceiver; confirm A/B, GND, termination resistors, and DE/RE direction control.
2. Select the UART, baud rate, parity, and stop bits on the Modbus page.
3. Add slave devices with their addresses and register mappings.
4. After saving, check the poll success/failure counters before creating rules or MQTT reports.

The Modbus slave response feature is disabled by default (`FASTBEE_MODBUS_SLAVE_ENABLE=0`); do not configure this device as a slave following slave-device documentation.

## Rule Integration

Modbus reads are handled by the existing scheduler and bus arbitration; currently valid mapped data can be used by independent event rules. Do not treat an output immediately following a collection action as threshold gating.

```text
RS485 slave -> poll task -> register mapping -> local sensor cache
                                               |
                                      rule conditions/actions
                                               |
                                      GPIO, display, or MQTT
```

A rule supports up to 3 triggers and 4 actions; for detailed configuration see [Peripheral Execution](https://www.fastbee.cn/doc/en/device/v2/periph-exec.md).

## Troubleshooting

| Symptom | Check First |
| --- | --- |
| MQTT not connected | DNS, server/port, credentials, Client ID, TLS feature, and system time |
| MQTT reconnects frequently | WiFi signal, Keep Alive, broker logs, and device memory |
| All Modbus requests time out | A/B reversed, baud rate/parity, slave address, DE pin, and common ground |
| Individual registers abnormal | Function code, register base, byte order, data type, and scaling factor |
| Rule takes no action | `modbus` feature, poll task, target peripheral, and rule enabled state |

For interfaces, status fields, and error codes, see the [API Reference](https://www.fastbee.cn/doc/en/device/v2/dev/api-reference.md); for network transports, see [Network Configuration](https://www.fastbee.cn/doc/en/device/v2/system/network-config.md).

The current PubSubClient publish actually uses QoS 0; a publish configuration of `qos=1` does not provide QoS 1 delivery guarantees. Subscribe and last-will QoS are handled separately. The legacy remote channel is retired: the related old APIs return 410, and rules bound to the retired source are suspended as a whole. See each topic page for detailed boundaries.

## FAQ

### Does "saved" after saving the protocol configuration mean the connection succeeded?

No. Saving only writes the configuration; the connection counts as successful only after `connected` is confirmed in `/api/mqtt/status` or the dashboard, and platform message exchange is verified.

### Can I configure this device as a Modbus slave?

No. The Modbus slave response feature is disabled by default (`FASTBEE_MODBUS_SLAVE_ENABLE=0`); the current firmware provides master capability only. Do not configure this device as a slave following slave-device documentation.

### Does a publish configuration of `qos=1` provide QoS 1 delivery guarantees?

No. The current PubSubClient publish actually uses QoS 0; subscribe and last-will QoS are handled separately. Do not take the publish configuration as evidence of delivery guarantees.

### Can I hardcode topic prefixes in integration programs?

No. Topics are determined by the publish/subscribe lists on the page; integration programs should read the actual configuration or use platform-assigned topics.

## Related Documentation

* [API Reference](https://www.fastbee.cn/doc/en/device/v2/dev/api-reference.md)
* [Network Configuration](https://www.fastbee.cn/doc/en/device/v2/system/network-config.md)
* [Seniverse Weather and Legacy Resource Migration](https://www.fastbee.cn/doc/en/device/v2/protocols/remote-resources.md)
* [Peripheral Execution](https://www.fastbee.cn/doc/en/device/v2/periph-exec.md)
