Skip to content

Gateway Setup

Sergey Brutsky edited this page Dec 3, 2026 · 2 revisions

Gateway setup with MiHomeSetup

Prepare a gateway for MiHomeLib using the MiHomeSetup console application and the repository's Makefile. check inspects readiness without changing the gateway; setup enables access and prepares the services required by the library.

Before you start

  1. Install the .NET 10 SDK, Git and GNU Make on the computer used for setup. The setup tool requires .NET 10 even when your application uses another supported library target.

  2. Clone the library repository and run the commands from its root:

    git clone https://github.com/sergey-brutsky/mi-home.git
    cd mi-home
    make build-gw-setup
  3. Find the gateway's IP and its 32-character hexadecimal miIO token. Setup does not extract the token. See the token extraction instructions.

  4. Run setup from a computer that can reach the gateway on the local network. Replace every example IP and credential placeholder below with your own values.

Model detection uses authenticated miIO.info when a token is provided. For MQTT gateways, existing passwordless Telnet access can also identify the model without a token. A miIO hello packet alone does not contain the model name.

Xiaomi Gateway 2

Applies to DGNWG02LM / lumi.gateway.v3. This gateway uses its native LAN API, not MQTT or Telnet.

Check the current state:

make gw check 192.168.1.13 token="<MIIO_TOKEN>"

Enable LAN access and verify the result:

make gw setup 192.168.1.13 token="<MIIO_TOKEN>"

Setup preserves an existing 16-character developer key. If the key is empty, it generates a random 16-character key, writes it through miIO and verifies the result. It then runs UDP discovery through the library. The key is not printed or saved locally; MiHomeLib retrieves it from the gateway when needed. An optional key="<16_CHARACTER_DEVELOPER_KEY>" selects a replacement key.

Gateway 2 readiness covers authenticated miIO and UDP discovery; it does not test switching each subdevice. An empty supported-device list can still pass discovery.

Multimode Gateway, Multimode Gateway 2 and Aqara Hub E1

Supported profiles:

These gateways need an accessible MQTT broker on TCP port 1883 and passwordless administrative Telnet on TCP port 23 for device discovery. The token is also needed for the library's miIO commands. Gateway DID, where required by a library constructor, is separate from setup credentials; see the individual gateway API page.

Start with a read-only check:

make gw check 192.168.1.100 token="<MIIO_TOKEN>"

If the services are unavailable, prepare the gateway:

make gw setup 192.168.1.100 token="<MIIO_TOKEN>"

On firmware requiring a separate saved gateway key, use:

make gw setup 192.168.1.100 token="<MIIO_TOKEN>" key="<GATEWAY_KEY>"

The 16-character gateway key is the key saved by the XiaomiGateway3 integration, not the 32-character miIO token. It is required for its system_command activation method. MiHomeSetup does not retrieve this key automatically. You can also pass it to check to validate its format and get setup advice; only setup verifies it by attempting activation.

Setup detects the model, selects the supported activation method for the firmware and opens Telnet if needed. It verifies administrative login, the actual model and CPU architecture, then prepares the MQTT agent and checks readiness. Existing MQTT clients may disconnect. Enabling passwordless administrative Telnet can remove the administrative password; keep this in mind when choosing where the gateway is accessible.

After setup, check again:

make gw check 192.168.1.100 token="<MIIO_TOKEN>"

When Telnet and MQTT are already available, make gw check 192.168.1.100 can detect and check an MQTT gateway without a token. This does not verify its miIO credentials.

Automatic Telnet activation rules

These rules apply when Telnet is closed. If passwordless administrative Telnet is already accessible, setup can proceed without automatic activation.

  • Multimode Gateway: 1.4.6 below build 0043 uses enable_telnet_service; build 0043 and later, 1.4.7, and 1.5.0–1.5.4 use set_ip_info. Versions 1.5.5–1.5.7 use system_command and require a saved gateway key. Version 1.4.6 needs an explicit build number.
  • Multimode Gateway 2 CN/Global: 1.0.3–1.0.6 use set_ip_info; 1.0.7–1.0.8 use system_command and require a saved gateway key.
  • Aqara Hub E1 CN: 4.0.1 uses set_ip_info; 4.0.4 uses system_command and requires a saved gateway key.

Unknown firmware versions stop automatic activation. These are implemented compatibility rules, not a claim that every listed firmware has been physically tested. The legacy 1.4.6 and 1.4.7 families are described as unsupported by the upstream integration.

Agent, Home Assistant and reboot behavior

Setup follows the XiaomiGateway3 integration conventions: pinned openmiio_agent v1.2.1 at /data/openmiio_agent, arguments miio mqtt cache central z3 --zigbee.tcp=8888, and log at /var/log/openmiio.log. A healthy agent with the expected executable and arguments is reused. This is intended to allow subsequent startup by the integration; future integration versions may update the agent.

MiHomeSetup does not add a boot script. The executable and backups remain in /data, but service startup after reboot depends on firmware and the application preparing the gateway. Run check after reboot and setup again if needed. Preservation of normal Mi Home operation across reboot must be checked on the target firmware.

If installation fails after stopping services, setup attempts to restore the previous executable and restart it; without a previous executable it attempts to start the embedded broker. Recovery is best effort. It cannot undo Telnet activation or guarantee recovery after losing administrative access. Keep any backup path printed by the tool. This is not a firmware flashing operation or a guaranteed full rollback.

Arguments and alternatives to Make

Model detection is optional; specify a model explicitly when needed:

make gw setup 192.168.1.100 model=lumi.gateway.mgl03 token="<MIIO_TOKEN>" key="<GATEWAY_KEY>"
make gw check 192.168.1.13 model=lumi.gateway.v3 token="<MIIO_TOKEN>"

Credentials can be read from local files instead of passed inline:

make gw setup 192.168.1.100 token_file="/path/to/gateway.token" key_file="/path/to/gateway.key"
make gw check 192.168.1.13 token_file="/path/to/gateway.token"

Files may have a trailing newline. token and token_file are mutually exclusive, as are key and key_file. A key option requires a token option. Quote values and paths containing shell-special characters or spaces. Run make gw separately, with gw as the first target. host=IP is an alternative to the positional IP.

Without Make, run the same application directly:

dotnet run --project MiHomeSetup -- check --host 192.168.1.13 --token-file ./gateway.token
dotnet run --project MiHomeSetup -- setup --host 192.168.1.100 --token-file ./gateway.token --key-file ./gateway.key
dotnet run --project MiHomeSetup -- --help

Understanding the result

  • READY: the protocol-specific connection and discovery checks passed. MQTT checks include CONNECT and subscription acknowledgements, plus parsing Zigbee and, where supported, BLE/Mesh discovery tables. It does not test every device command, future reports or operation after reboot.
  • NOT READY: use the service failures and suggested next command printed below the result. check does not prepare the gateway automatically.
  • make: ... Error 1: the tool returned failure/not ready. Read the preceding diagnostics; this line itself is not another gateway error.

Exit codes are 0 for success/ready, 1 for failure/not ready, 2 for invalid arguments and 130 for cancellation or the overall timeout.

Troubleshooting and manual preparation

Gateway 2: miIO works, UDP discovery fails. Ensure LAN access is enabled and multicast 224.0.0.50 / UDP port 9898 can pass in both directions. Run the application on the gateway's LAN; routed networks and VPNs require multicast forwarding. Multiple virtual adapters (Hyper-V, VirtualBox, packet-capture software) can affect multicast interface selection on Windows. Check routing and adapter selection rather than treating a successful miIO request as proof of multicast access.

Some Gateway 2 hardware revisions can have closed multicast ports even after enabling LAN access. Setup does not patch firmware or guarantee opening these ports. Historical manual references: development mode and closed-port troubleshooting.

MQTT/Telnet port closed. Check the address and firewall, then run setup with a valid token and the saved gateway key if required. Unsupported activation firmware requires manual Telnet preparation. See the historical Telnet instructions, including their Aqara E1 section, or prepare the gateway using the XiaomiGateway3 Home Assistant integration. Then rerun check. Manual instructions may depend on firmware and are alternatives to automatic preparation.

Model cannot be detected. Supply the correct token or an explicit model=.... An explicit model does not bypass setup's check of the actual device. Unknown or ambiguous models are rejected.

Discovery files cannot be read or parsed. An open MQTT port alone is insufficient. Inspect the discovery failure; setup does not repair damaged device databases.

A stale /tmp/mihome-setup.lock remains. Confirm that no MiHomeSetup operation is still running before removing the directory through Telnet. Other applications do not use this lock; avoid concurrent preparation by Home Assistant and MiHomeSetup.

See also: gateway API pages, migration guide, and the MiHomeSetup implementation notes.

Clone this wiki locally