I use Home Assistant to control and monitor devices around my home, I designed a wireless door/window sensor in an earlier blog entry that used very little power and used the ESP-NOW protocol to send data packets. To receive the data I use an ESP07 with an external antenna which feeds the data into the USB port of a Raspberry Pi running HA.
I decided to make a dedicated ESP-NOW To WiFi gateway using two ESP devices linked via a serial connection, this means I can now locate the ESP-NOW receiver closer to the centre of the house rather than being right next to the Raspberry Pi.
This is a first for me, this project has been sponsored by PCBWay, they have provided the PCBs populated with all the SMD components apart from the Xiao ESP32 S3s free of charge.
The quality of the PCBA is good and I would have no problem using them again. For PCBs, PCBA, 3D printing and CNC machining visit WWW.PCBWAY.com
WWW.PCBWay.com### This hardware design is solely done by me without AI assistance but the software is written by Gemini AI based on my instructions ###
All files are available on my Github page www.github.com/gadjetBasic Features
- Two distinct ESP32 S3s linked by the Serial ports
- Each ESP individually programmable by reversing the USB plug
- The selected ESP is indicated by an LED under the ESP
- Optional OLED display for Gateway status
- Status RGB LED indicating connection status
- Optional Touch input (D3 on Wifi ESP) to wake up OLED from sleep (I used Copper tape on the inside of the 2mm thick PLA enclosure)
- Web page hosted by WiFi ESP with status and settings (use IP address from OLED or check router)
Dual XIAO ESP32-S3 ESP-NOW to MQTT Gateway
An industrial-grade, dual-microcontroller bridge between low-power ESP-NOW sensor nodes and an MQTT broker (e.g. Home Assistant, Mosquitto, CloudAMQP). Built around two Seeed Studio XIAO ESP32-S3 modules on a custom PCB.
1. System Architecture
[ Remote ESP-NOW Sensor Nodes ] (Battery powered, deep-sleep, ultra-fast burst)
│ ESP-NOW (2.4 GHz RF - Channel 1 default)
▼
┌────────────────────────────┐
│ Xiao ESP32-S3 (U1) │ - Dedicated ESP-NOW RX/TX
│ ESP-NOW Receiver Node │ - Continuous listening (zero packet loss)
└─────────────┬──────────────┘ - Hardware FIFO frame packaging
│
│ Inter-ESP Hardware UART (D6 & D7 @ 115200 baud)
▼
┌────────────────────────────┐
│ Xiao ESP32-S3 (U10) │ - WiFi Station (Auto-reconnect)
│ WiFi & MQTT Gateway │ - MQTT Client (Publish & Subscribe)
└──────┬──────────────┬──────┘ - Web Dashboard & REST API
│ │ - Common-Anode RGB Status LED (LED1)
│ └────────► Optional I2C OLED Display (SSD1306 on H1)
▼
[ Local MQTT Broker / Home Assistant ]
Why Dual ESP32-S3 Modules?
Standard single-chip ESP-NOW + WiFi gateways suffer from packet drops, channel conflicts, and latency jitter because WiFi station mode and ESP-NOW share the same radio hardware. Using two dedicated Xiao ESP32-S3 modules connected via hardware UART provides:
- Zero Packet Drops: U1 is dedicated to ESP-NOW listening 100% of the time.
- Independent WiFi Channel Management: U10 connects to any router channel without disrupting U1.
- Hardware UART FIFO: Direct hardware serial communication over pins D6 and D7 eliminates SoftwareSerial bit-banging CPU overhead.
2. Reversible USB-C Connector (Flippable Programming)
The custom PCB features an ingenious USB-C circuit:
- USB Data Lines (D+/D-): Only one ESP32-S3 is connected to the USB data lines at a time.
- Flippable Cable: Turning the USB-C plug over switches the USB connection between U10 (WiFi/MQTT) and U1 (ESP-NOW).
- Orientation Indicator LEDs: An onboard
LMV393comparator monitors the USB-C CC1/CC2 lines:- LED3 (WiFi/MQTT): Illuminates when the cable is oriented to program U10.
- LED2 (ESP-NOW): Illuminates when the cable is turned over to program U1.
3. Hardware Pin Mapping
A. U10 (WiFi, MQTT Gateway, Web Server & Display Controller)
| U10 Pin | ESP32-S3 GPIO | Connected Component | Schematic Net | Purpose |
|---|---|---|---|---|
| D0 | GPIO1 | LED1 Pin 1 via R3 (680Ω) | red | RGB LED: Red (Active LOW) |
| D1 | GPIO2 | LED1 Pin 3 via R5 (680Ω) | green | RGB LED: Green (Active LOW) |
| D2 | GPIO3 | LED1 Pin 4 via R4 (680Ω) | blue | RGB LED: Blue (Active LOW) |
| D3 | GPIO4 | Test Pad D3# (Copper Tape Pad) | D3# | Capacitive Touch (TOUCH4): Wakes OLED display for 30s |
| D4 | GPIO5 | Connector H1 Pin 4 + R1 (4.7kΩ pull-up) | SDA | I2C Data (Optional OLED) |
| D5 | GPIO6 | Connector H1 Pin 3 + R2 (4.7kΩ pull-up) | SCL | I2C Clock (Optional OLED) |
| D6 | GPIO43 | U1 Pin 7 (D6) | RX | Hardware UART1 RX from U1 |
| D7 | GPIO44 | U1 Pin 8 (D7) | TX | Hardware UART1 TX to U1 |
| 3V3 | +3.3V | VCC Rail | VCC | +3.3V Power |
| GND | GND | Ground Rail | GND | Common Ground |
| 5V | +5V | USB1 VBUS Rail | +5V | System 5V Input |
B. U1 (Dedicated ESP-NOW Receiver & Forwarder)
| U1 Pin | ESP32-S3 GPIO | Connected Component | Schematic Net | Purpose |
|---|---|---|---|---|
| D0 | GPIO1 | Test Pad D0 | D0 | Breakout / Diagnostic Pin |
| D1 | GPIO2 | Test Pad D1 | D1 | Breakout / Diagnostic Pin |
| D2 | GPIO3 | Test Pad D2 | D2 | Breakout / Diagnostic Pin |
| D3 | GPIO4 | Test Pad D3 | D3 | Breakout / Diagnostic Pin |
| D4 | GPIO5 | Test Pad D4 | D4 | Breakout / Diagnostic Pin |
| D5 | GPIO6 | Test Pad D5 | D5 | Breakout / Diagnostic Pin |
| D6 | GPIO43 | U10 Pin 7 (D6) | TX | Hardware UART1 TX to U10 |
| D7 | GPIO44 | U10 Pin 8 (D7) | RX | Hardware UART1 RX from U10 |
| 5V | +5V | USB1 VBUS Rail | +5V | System 5V Input |
| GND | GND | Ground Rail | GND | Common Ground |
C. RGB Status LED (LED1 - Common Anode, Active LOW)
The RGB LED anodes are connected to VCC (+3.3V). Driving a pin LOW illuminates the color; driving it HIGH turns it off.
| State / Condition | LED Color | Pin States |
|---|---|---|
| Connecting to WiFi | Solid Red | Red(D0): LOW, Green(D1): HIGH, Blue(D2): HIGH |
| WiFi Connected, Connecting to MQTT | Solid Yellow | Red(D0): LOW, Green(D1): LOW, Blue(D2): HIGH |
| WiFi & MQTT Connected (Normal) | Solid Green | Red(D0): HIGH, Green(D1): LOW, Blue(D2): HIGH |
| ESP-NOW Packet Received & Forwarded | Brief Blue Pulse | Blue(D2): LOW for ~70ms on packet arrival |
| Outbound Message Sent via ESP-NOW | Brief Cyan Pulse | Green(D1) + Blue(D2): LOW for ~80ms on transmit |
4. Optional I2C Display (Connector H1)
Connector H1 is pinned out for standard 4-pin I2C OLED modules (e.g. SSD1306 128x64 or 128x32):
- Pin 1:
GND - Pin 2:
VCC(+3.3V) - Pin 3:
SCL(U10 Pin D5) - Pin 4:
SDA(U10 Pin D4)
[!NOTE] The I2C display is optional and auto-detected. At boot, U10 scans the I2C bus for addresses
0x3Cand0x3D. If no display is detected, the gateway continues executing normally without delay.
OLED Power-Save & Capacitive Touch Wake-Up (Pin D3)
To prevent OLED burn-in and save power:
- Auto-Off after 30 Seconds: When enabled, the OLED turns on at power-up and automatically enters hardware sleep (
SSD1306_DISPLAYOFF) 30 seconds after startup or last wake. - Web & API Toggle: The 30-second timeout can be enabled or disabled at any time via the 30s Screen Timeout slide switch on the Web Dashboard or the
/api/oled_timeoutAPI endpoint (persisted in Flash NVS). - Touch-to-Wake on D3 (
GPIO4/TOUCH4): Connecting test pad D3# on U10 to a piece of copper tape on the inside of the 2mm 3D-printed PLA cover enables capacitive touch wake-up. Touching the cover over the copper pad wakes the display for 30 seconds (or resets the 30-second timer if already awake). - Auto-Calibration: At startup, U10 automatically calibrates the untouched baseline capacitance of the copper pad through the enclosure and tracks slow ambient temperature/humidity drift.
5. Project Folder Structure
ESP32 S3 Gateway/
├── Gateway_WiFi_MQTT/
│ └── Gateway_WiFi_MQTT.ino <-- Flash to U10 (WiFi + MQTT + Web Server + LED + OLED)
├── Gateway_ESPNOW_Receiver/
│ └── Gateway_ESPNOW_Receiver.ino <-- Flash to U1 (ESP-NOW Receiver & Forwarder)
├── ESPNOW_Sender_Example/
│ └── ESPNOW_Sender_Example.ino <-- Test sketch for remote sensor nodes
├── Schematic/
│ ├── SCH_Schematic1_2026-10-30.pdf
│ └── schematic_highres.png
├── PCB/
│ ├── Top.png
│ └── Bottom.png
└── README.md <-- Documentation
6. Setup & Flashing Instructions
Required Libraries
Install the following via the Arduino IDE Library Manager (Sketch -> Include Library -> Manage Libraries...):
- PubSubClient by Nick O'Leary
- Adafruit SSD1306 by Adafruit (Required only if using OLED)
- Adafruit GFX Library by Adafruit (Required only if using OLED)
Board Settings in Arduino IDE
For both modules, select:
- Board:
XIAO_ESP32S3(under ESP32 Arduino) - USB CDC On Boot:
Enabled - Flash Size:
8MB(or default) - Upload Speed:
921600
Step 1: Flash U1 (ESP-NOW Receiver)
- Plug the USB-C cable into the gateway board.
- Verify that LED2 ("ESP-NOW") is illuminated. (If LED3 is illuminated instead, flip the USB-C plug over).
- Open
Gateway_ESPNOW_Receiver/Gateway_ESPNOW_Receiver.inoin Arduino IDE. - Select the COM port corresponding to the board.
- Click Upload.
- Open Serial Monitor at 115200 baud to confirm U1 startup and note its MAC address.
Step 2: Flash U10 (WiFi & MQTT Gateway)
- Flip the USB-C cable connector over.
- Verify that LED3 ("WiFi/MQTT") is now illuminated.
- Open
Gateway_WiFi_MQTT/Gateway_WiFi_MQTT.inoin Arduino IDE. - Select the new COM port in the IDE.
- Click Upload.
- Open Serial Monitor at 115200 baud.
- Observe the RGB LED:
- Solid Red: Connecting to WiFi.
- Solid Yellow: WiFi connected, connecting to MQTT.
- Solid Green: Fully connected and operating.
7. Web Dashboard & Configuration
Accessing the Web Dashboard
Open your browser and navigate to the gateway's IP address:
http://<U10_IP_ADDRESS>/
Screenshot
Features:
- Live Badges: Real-time connection status for WiFi (including IP, RSSI, and connected AP BSSID), MQTT Broker, Inter-ESP U1 Link (including last ESP-NOW RSSI), and OLED display.
- 1-Hour WiFi RSSI Trend Graph: Embedded sparkline on the WiFi card plotting up to 60 1-minute RSSI samples (
-90to-30dBm) with a-65dBm roaming threshold reference line. - Automatic Mesh AP Roaming: If WiFi RSSI drops below
-65dBm, U10 periodically scans for a stronger BSSID on the same SSID (>= 10dB improvement) and reconnects to it. - OLED Timeout Slide Switch: Toggle the 30-second OLED auto-sleep timeout directly from the OLED status card.
- Rolling Message Table: Auto-updating table of received ESP-NOW sensor packets with timestamps, MAC addresses, RSSI, and decoded payloads.
- Test Transmitter: Send test unicast or broadcast ESP-NOW messages directly from your browser.
REST API Endpoints
GET /api/status: Returns live JSON status,wifi_bssid,wifi_rssi_history,u1_last_rssi,oled_timeout_enabled, touch calibration values, and recent packets.GET/POST /api/oled_timeout?enabled=true|false: Reads or updates the NVS-persisted OLED 30-second screen timeout setting.POST /api/send: Dispatches a unicast or broadcast ESP-NOW payload via U1.
Supported ESP-NOW Payloads (U1 Receiver)
In addition to UTF-8 JSON/text payloads, Gateway_ESPNOW_Receiver.ino automatically decodes binary struct_message packets (int id, char state[7], int vBatt — 16 bytes) and forwards them as JSON ({"id":...,"state":"...","vBatt":...}).
Reconfiguring Settings
Navigate to:
http://<U10_IP_ADDRESS>/config
From here you can adjust WiFi SSID/Password, MQTT broker host, port, credentials, and base topic prefix. All settings are persistently stored in the ESP32-S3 Flash NVS.
8. MQTT Topics & Payloads
| Direction | Topic | Payload Example | Description |
|---|---|---|---|
| Gateway -> MQTT | <prefix>/rx/<CLEAN_MAC> | {"temp":22.4,"humidity":55.1} | Incoming ESP-NOW packet from sensor node |
| Gateway -> MQTT | <prefix>/status | online / offline | Gateway LWT availability topic |
| MQTT -> Gateway | <prefix>/tx | {"cmd":"restart"} | Broadcast packet sent to all ESP-NOW nodes |
| MQTT -> Gateway | <prefix>/send/<TARGET_MAC> | {"valve":"open"} | Unicast packet dispatched to specific node |


No comments:
Post a Comment