Automating household processes starts small, and most often the first step is installing a smart switch or outlet. Most of these devices are based on a simple yet powerful component— WiFi relayThis device allows you to remotely control your load, whether it's lighting, a pump, or a heater, using your home's wireless network.
Setting up such equipment opens up scenarios that previously seemed impossible: turning on lights with a clap, controlling heating via a smartphone from another country, or automatically turning off appliances when no one is present. However, for the magic to work, the initial configuration must be performed correctly.
In this guide, we will walk you through the entire process from unpacking the module to integrating it into the system. Home AssistantYou'll learn about firmware differences, the nuances of connecting GPIO pins, and ways to bypass the limitations of manufacturers' proprietary software.
Selecting the Right Relay Module
The IoT device market is overflowing with options, but for a serious setup, it's important to choose the right hardware. The most popular chips are ESP8266 And ESP32The first is cheaper and easier to learn, the second has more computing power and Bluetooth support.
When purchasing, pay attention to the type of relay used. 10-amp modules are suitable for lighting control, but for high-power consumers like boilers or convectors, a higher power rating is required. 16 Amps and more. Cheap Chinese boards often have inflated specifications, so it's best to buy devices with a reserve.
⚠️ Caution: There is a risk of electric shock when working with 220V mains voltage. Only connect wires to the relay terminals when the mains voltage is completely de-energized!
It's also worth considering the device's form factor. It could be a ready-made smart plug, a built-in relay for a wall box, or a DIN rail module for an electrical panel. Development boards, such as NodeMCU or Wemos D1 Mini, to which any external relay can be soldered.
Device Firmware: Tasmota vs. ESPHome
The factory firmware of most Chinese relays (Tuya, Smart Life) operates via the cloud and often depends on servers in China, which creates delays and privacy issues. Firmware replacement is a key step. The leaders here are Tasmota And ESPHome.
Tasmota is renowned for its web interface and a huge number of ready-made templates. If you want to quickly get a working device with minimal code, this is your choice. ESPHome, on the other hand, is designed for deep integration with Home Assistant and requires YAML configuration, providing flexibility in customizing the logic.
The flashing process often requires using OTA (Over The Air) mode or a physical connection via a UART converter. There's a utility for Tasmota. tuya-convert, which allows you to flash your device over the air if it has not yet been updated by the manufacturer to a new version of security.
What to do if OTA doesn't work?
If wireless flashing isn't possible, you'll need to open the relay housing. Locate the TX, RX, GND, and VCC (3.3V) contacts. Connect them to the USB-UART adapter, hold down the GPIO0 button while applying power, and upload the binary file via a browser or terminal.
Tools and preparation for work
Before you begin setup, you need to prepare your workstation and software. You'll need a stable 2.4 GHz WiFi network, as most IoT devices don't support 5 GHz.
For diagnostics and initial configuration of IP addresses, a network scanner will be useful, for example, Advanced IP Scanner or mobile app FingAlso, install a terminal for working with MQTT if you plan to use this technology, for example, MQTT Explorer.
☑️ Preparing for firmware
It's important to have a pair of PCB tweezers or insulated screwdrivers on hand if you need to disassemble the device. Keep safety in mind: static electricity can damage chips, so it's recommended to use an anti-static mat or wrist strap.
Basic network setup and MQTT
After successfully flashing the firmware, the device creates its own access point. Once connected, you'll be taken to the web configuration interface. First, you'll need to specify your home network settings: SSID and password.
For integration into smart home systems, the protocol is critical MQTTIt allows devices to exchange messages through a broker. The relay settings require entering the broker address (often the IP address of the Home Assistant server or Raspberry Pi), port (default 1883), and credentials.
| Parameter | Default value | Recommended value | Description |
|---|---|---|---|
| Port | 1883 | 1883 | Standard port for unencrypted MQTT |
| Topic | tasmota/DEVICE | home/relay_kitchen | Unique topic for the device |
| QoS | 0 | 1 | Message delivery guarantee (At least once) |
| Retain | False | True | Save the last state on the broker |
Setting up topics is the art of organizing namespaces. Use a hierarchical structure, for example: dom/livingroom/lightto easily navigate the data flow, especially if you have dozens of devices.
GPIO Configuration and Load Control
The most delicate part of setup is assigning the correct pins. In the Tasmota firmware, this is done through the menu. Configuration > Configure ModuleYou need to know which physical pin on the ESP chip is connected to the relay coil and possibly the status LED.
For example, for the popular Sonoff Basic module, the relay is typically assigned to GPIO12 and the button to GPIO0. If these assignments are reversed, the device may behave erratically: the LED may light but the relay won't click, or vice versa.
⚠️ Note: Some GPIO pins (e.g., GPIO6-11 on the ESP8266) are reserved for flash memory and cannot be used for external connections. Using GPIO15 (D8) can also cause boot issues.
For complex scenarios, such as blind control (requiring two relays and a delay between activations) or a pulse relay, the firmware includes specialized module types. Don't use the standard "Generic" type if a specialized template is available.
Home Assistant integration and automation
After setting up MQTT, the device appears almost instantly in Home Assistant if auto-discovery (Home Assistant Discovery) is enabled. You'll see an entity like switch.relay_kitchen, which can be controlled with_dashboard.
The system's power is revealed through automation. You can create a scenario where the relay turns on at sunset if someone is in the room (motion sensor), and turns off after 15 minutes. The logic can be as complex as you like.
Advanced users can integrate with ESPHome, where the configuration is stored in a YAML file. This allows for versioning of settings and quick restoration when replacing hardware. The configuration code appears as a set of nested parameters.
switch:- platform: gpio
pin: GPIO12
name:"Kitchen Light"
id: kitchen_light
restore_mode: RESTORE_DEFAULT_OFF
Diagnostics and safety
During operation, problems with loss of connection (WiFi disconnects) may occur. This is often due to a weak signal at the location where the relay is installed. Use WiFi Signal Strength in the firmware interface to assess the signal level; a value below -75 dBm is considered unstable.
Another common issue is a "hanging" Wi-Fi module. This can be resolved by configuring the Watchdog Timer or performing a software restart if the connection to the MQTT broker is lost for more than 2 minutes. Also, monitor the case temperature: cheap relays can become hot under load.
Security shouldn't be an afterthought. Change default passwords for accessing the device's web interface. Disable unnecessary services, such as Telnet, unless you're using them for debugging right now.
How to update firmware without losing settings?
In Tasmota, use the "Upgrade" -> "By URL" function, specifying a direct link to the binary. Settings stored in the file system are usually preserved, but it's best to copy the configuration file (config.ini) beforehand.
Do I need special equipment for flashing?
In most cases, a WiFi connection and a computer are sufficient. A physical UART connection is only required if the device is blocked from OTA updates or if you purchased a bare ESP board without the Tuya firmware.
Will the relay burn out if I connect a load higher than the nominal value?
Yes, this is very likely. The relay contacts could weld together, and the housing itself could melt. Always leave a 20-30% current reserve. If the relay is rated for 10A, do not connect a load greater than 2 kW.
Can I use WiFi relay with Alexa or Google Home?
Yes, but not directly. You'll need a "bridge," such as a Home Assistant plugin or an MQTT server that transmits commands to the voice assistant cloud. Direct integration is only possible with the manufacturer's original software.
What to do if the relay clicks spontaneously?
Check the device logs. The cause could be contact bounce, interference in long wires to the button, or a software bug. Software debounce filtering can be enabled in the firmware.