ESPNow to Homeassistant/MQTT Gateway
by yanc in Circuits > Microcontrollers
117 Views, 0 Favorites, 0 Comments
ESPNow to Homeassistant/MQTT Gateway

This project implements an ESP32-based ESPNow gateway, and its special feature is:
- It uses only a single ESP32 module, but connects to a wired network using either the IP101 or LAN8742.
- It receives ESPNow broadcast packets and can parse sensor data using the BTHome protocol.
- It automatically registers a component with HomeAssistant and updates the sensor data.
For demo the whole system, I also created an Wireless button. The button is powered with lithium battery and can work with months without charge power.
Downloads
Supplies

.jpg)

The supplies for this project are listed below.
- AB BLE Gateway v4 - A gateway based on ESP32, it connect Ethernet with IP101 or LAN8742 chip.
- A4 button - A WiFi button based on ESP32 C3
- ESP flasher rev6
Note: We will erase the default firmware for these boards and flash our own firmware. So the ESP flasher is required. It's a USB to UART converter that based on CH340.
How It Works

ESPNow button
- The button encodes the data into the BTHome binary format when button is clicked.
- It sends the BTHome payload to ESPNow gateway
- Button go to sleep for power saving
ESPNow gateway
- The gateway receives the BTHome payload.
- It decodes the payload back into human-readable value. The source code supports button type only. It's possible expand the source to parse more sensor type datas.
- The gateway publishes the decoded data to an MQTT topic (e.g., sensor/temperature).
Homeassistant
- Home Assistant subscribes to the MQTT topic.
- It receives the decoded data and uses MQTT Discovery to create entities
Key Technologies
- BTHome:
- A binary format for encoding sensor data, commonly used in Bluetooth Low Energy (BLE) devices.
- Compact and efficient for transmitting multiple sensor values.
- ESP-NOW:
- A lightweight, peer-to-peer communication protocol for ESP devices.
- MQTT:
- A lightweight messaging protocol for IoT devices.
- MQTT Discovery:
- A feature in Home Assistant that automatically creates entities based on MQTT topics and payloads.
Compile the Source Code

Install platformio
The microcontroller program here is developed in Arduino framework using PlatformIO. I prefer PlatformIO Core (CLI). PlatformIO Core provides a rich and documented Command Line Interface that to download library dependences, tool and compile source code.
See the link to install platformio core
Compile the ESPNow gateway source code
Please clone the espnow-gateway GitHub repository
Modify the MQTT_BROKER_PATH and MQTT_BROKER_PORT to match your environment
Compile the source code with command line
See the terminal output if successful
Compile the ESPNow button source code
Please clone the espnow-button GitHub repository
Compile the source code with command line
See the terminal output if successful
Flash the Firmware

Flash firmware to the ESPNow gateway
To flash the compiled firmware to the gateway, you can wire the ESP flasher to the pins P2 which designed for flash the ESP32 module. Run the command line
Flash firmware to the ESPNow button
A4 button has a USB-C header for flash firmware. Here's the step to flash firmware
- Open the button case to get the PCBA
- Unplug the battery
- Connect the button's USB-C to your laptop
- Run the command line to flash firmware
Testing
- Connect the gateway with 5V/2A power adapter
- Wire Ethernet cable to the gateway
Final Thoughts
This system is a versatile and cost-effective solution for creating a call system. By leveraging ESP-NOW for communication, MQTT for data transmission, and Home Assistant for automation, you can build a reliable and customizable call system for various use cases. Whether for elderly care, workplace assistance, or home automation, this setup provides a robust foundation for meeting your needs.
WARNING: Do not use this Call System in isolation in situations where emergency medical assistance might be needed. This system is not meant to replace devices that directly link to emergency medical service providers or 911 emergency dispatchers.