Skip to content

romlisrl/Esp32H2GasCounter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

117 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Buy Me a Coffee ESP-IDF Zigbee

ESP32H2 Gas Counter with Zigbee2MQTT

ESP32H2-based pulse counter that measures pulses from utility meters with magnetic impulse output and sends them to Zigbee2MQTT.

Inspired by ZigbeeGasCounter.


πŸš€ Quick Start

  1. Flash the firmware onto your ESP32-H2 board.
  2. Copy the external converter to Zigbee2MQTT.
    πŸ‘‰ See External Converter
  3. Restart Zigbee2MQTT completely (full restart required).
  4. Enable Zigbee pairing (permit join)
  5. Power on the board or press the Boot button to send the first data packet.

⚠️ Power Notes

  • The vBat pin operates at VDD levels (typically 3.3V).
  • The pin vBat is NOT 5V tolerant.
  • To reduce power consumption, the power LED may be removed (unsoldered).
  • Use a voltage regulator or voltage divider if needed.
  • Can also be powered from USB (3.3V internally regulated).

✨ Features

Zigbee2MQTT

  • Counts pulses from a meter using a reed sensor connected to a GPIO and GND pin.
  • Stores pulse count in non-volatile memory (NVS).
  • Sends data to Zigbee2MQTT:
    • On a configurable timer
    • When accumulated pulses reach a threshold
    • Triggers immediate transmission when Boot button is pressed
  • Supports deep sleep for low power.
  • LED for pulse indication.

πŸ”§ Hardware

ESP32-H2 DevKitM-1 Pinout


πŸ“¦ Software Requirements


πŸ”Œ Wiring Example

ESP32H2 Pin Connection
GPIO10 Reed sensor signal
GND Reed sensor GND
VBat(Vcc) +VDD (+3.3V)
GND Battery '-'

ESP32-H2 DevKitM-1 Wiring


βš™οΈ Configuration

Parameter Description
PULSE_PIN GPIO pin connected to the reed sensor
MAIN_BTN GPIO used for the button
MUST_SYNC_MINIMUM_TIME Interval between automatic transmissions (default: 60 min)
COUNTER_REPORT_DIFF Number of pulses to trigger immediate transmission (default 10)
INITIAL_COUNTER_VALUE Initial counter value (e.g., 2134 = 21.34 mΒ³)
FEATURE_MEASURE_BATTERY_LEVEL Enable or disable battery voltage monitoring
ADC_CHANNEL_2 GPIO pin used to measure voltage via a resistive divider
RATED_BATTERY_VOLTAGE Nominal battery voltage (as specified by the manufacturer)
MAX_BATTERY_VOLTAGE Maximum battery voltage
MIN_BATTERY_VOLTAGE Minimum battery voltage
WARN_BATTERY_VOLTAGE Battery voltage threshold for warning
ADC_MAX_VALUE Maximum ADC value (on GPIO pin used to measure voltage)

πŸš€ How It Works

  1. ESP32H2 counts pulses from the meter.
  2. Pulses are stored in NVS flash to survive reboots or deep sleep.
  3. When timer expires or pulse threshold is reached data is sent to Zigbee2MQTT.
  4. Device enters deep sleep to save power until next event (pulse or timer).

🏠 Home Assistant Configuration

To correctly interpret the counter values as gas consumption, create a Helper in Home Assistant Settings β†’ Devices & services β†’ Helpers β†’ Create helper β†’ Template β†’ Sensor with the following parameters.

5720.17 - is the initial reading of your utility meter.
sensor.0xf4ce3662f44a0e45_gas_count - your sensor entity in Home Assistant.

πŸ“Š Template

Name: Gas meter
State: {{ (states('sensor.0xf4ce3662f44a0e45_gas_count') | float(0) * 0.01 + 5720.17) | round(2) }}
Device class: Gas
State class: Total increasing
Unit of measurement: mΒ³
Availability template: {{ has_value('sensor.0xf4ce3662f44a0e45_gas_count') }}

πŸ“Ÿ Example Logs

GAS_COUNTER: Counter loaded value=160
GAS_COUNTER: Setup deep sleep
GAS_COUNTER: Wake up from PULSE.
GAS_COUNTER: Checking if Zigbee radio should be enabled
GAS_COUNTER: Counter stored value=161
GAS_COUNTER: Configuring wake-up methods
GAS_COUNTER: Enabling wake-up timer , 162s

⚑ Installation

⚠️ Don't forget to create an external converter in Zigbee2MQTT first!

1️⃣ Using ESP-IDF (recommended)

2️⃣ Using esptool.py (firmware_batmon.bin/firmware_wo_batmon.bin)

  • Connect the board to a UART port (e.g., COM10 on Windows)

With battery monitor:

esptool.py --chip esp32h2 --port COM10 write_flash 0x0 firmware_batmon.bin  

Without battery monitor:

esptool.py --chip esp32h2 --port COM10 write_flash 0x0 firmware_wo_batmon.bin  

3️⃣ Using ESPHome Web (firmware_batmon.bin/firmware_wo_batmon.bin)

  • Connect the board via USB/UART
  • Select the firmware file:
    • firmware_batmon.bin (with battery monitor)
    • firmware_wo_batmon.bin (without battery monitor)
  • Flash it directly from the browser

πŸ› οΈ To Be Done

The following tasks are planned or pending implementation:

  • [Ρ…] Implement battery voltage measurement via voltage divider on GPIO3 β€” include ADC reading and calibration.
  • [Ρ…] Add configuration for voltage divider parameters (scaling factor, low-battery thresholds).

πŸ“ Notes

  • Make sure the Zigbee coordinator is running and permit join is enabled
  • After modifying the external converter, restart Zigbee2MQTT completely (do not use Settings β†’ Tools β†’ Restart Zigbee2MQTT)