ESPNow to Homeassistant/MQTT Gateway

by yanc in Circuits > Microcontrollers

117 Views, 0 Favorites, 0 Comments

ESPNow to Homeassistant/MQTT Gateway

111.jpg

This project implements an ESP32-based ESPNow gateway, and its special feature is:

  1. It uses only a single ESP32 module, but connects to a wired network using either the IP101 or LAN8742.
  2. It receives ESPNow broadcast packets and can parse sensor data using the BTHome protocol.
  3. 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.

Supplies

gateway41.jpg
a4-1 (1).jpg
esp-flasher-rev6-2.png

The supplies for this project are listed below.

  1. AB BLE Gateway v4 - A gateway based on ESP32, it connect Ethernet with IP101 or LAN8742 chip.
  2. A4 button - A WiFi button based on ESP32 C3
  3. 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

截屏2025-02-10 16.25.33.png

ESPNow button


  1. The button encodes the data into the BTHome binary format when button is clicked.
  2. It sends the BTHome payload to ESPNow gateway
  3. Button go to sleep for power saving

ESPNow gateway


  1. The gateway receives the BTHome payload.
  2. 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.
  3. The gateway publishes the decoded data to an MQTT topic (e.g., sensor/temperature).


Homeassistant


  1. Home Assistant subscribes to the MQTT topic.
  2. It receives the decoded data and uses MQTT Discovery to create entities


Key Technologies


  1. BTHome:
  2. A binary format for encoding sensor data, commonly used in Bluetooth Low Energy (BLE) devices.
  3. Compact and efficient for transmitting multiple sensor values.
  4. ESP-NOW:
  5. A lightweight, peer-to-peer communication protocol for ESP devices.
  6. MQTT:
  7. A lightweight messaging protocol for IoT devices.
  8. MQTT Discovery:
  9. A feature in Home Assistant that automatically creates entities based on MQTT topics and payloads.

Compile the Source Code

截屏2025-02-11 10.16.36.png

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

pio run

See the terminal output if successful

Linking .pio/build/esp/firmware.elf
Retrieving maximum program size .pio/build/esp/firmware.elf
Checking size .pio/build/esp/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM: [== ] 15.2% (used 49788 bytes from 327680 bytes)
Flash: [======== ] 80.5% (used 1055329 bytes from 1310720 bytes)
Building .pio/build/esp/firmware.bin
esptool.py v4.7.5
Creating esp32 image...
Merged 27 ELF sections
Successfully created esp32 image.
============================================ [SUCCESS] Took 9.36 seconds ============================================


Compile the ESPNow button source code


Please clone the espnow-button GitHub repository

Compile the source code with command line

pio run

See the terminal output if successful

Linking .pio/build/c3/firmware.elf
Retrieving maximum program size .pio/build/c3/firmware.elf
Checking size .pio/build/c3/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM: [= ] 10.6% (used 34620 bytes from 327680 bytes)
Flash: [======= ] 72.9% (used 955594 bytes from 1310720 bytes)
Building .pio/build/c3/firmware.bin
esptool.py v4.7.5
Creating esp32c3 image...
Merged 2 ELF sections
Successfully created esp32c3 image.
============================================ [SUCCESS] Took 6.51 seconds ============================================

Environment Status Duration
------------- -------- ------------
c3 SUCCESS 00:00:06.509

Flash the Firmware

gw4-layout.png

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

pio run -t upload


Flash firmware to the ESPNow button


A4 button has a USB-C header for flash firmware. Here's the step to flash firmware

  1. Open the button case to get the PCBA
  2. Unplug the battery
  3. Connect the button's USB-C to your laptop
  4. Run the command line to flash firmware
pio run -t upload


Testing


  1. Connect the gateway with 5V/2A power adapter
  2. 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.