# ESPHome 4.2" BWR 4-Zone Climate Display

[![ESPHome](https://img.shields.io/badge/ESPHome-2026.2+-blue.svg)](https://esphome.io)
[![Home Assistant](https://img.shields.io/badge/Home%20Assistant-Compatible-41BDF5.svg)](https://www.home-assistant.io)
[![Hardware](https://img.shields.io/badge/Hardware-M5Stack%20Stamp--C5%20%7C%20WeAct%204.2%22%20BWR-orange.svg)](#hardware--wiring)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

An ultra-low-power, battery-powered 4-zone environmental monitoring display built using **ESPHome**, an **M5Stack Stamp-C5 (ESP32-C5)** or **ESP32-C3**, and a **WeAct 4.2" 400×300 3-Color (Black, White, Red) E-Paper Display (SSD1683)**. 

Sensor states are streamed over the native Home Assistant API, featuring dual-scale temperature readouts, relative humidity, battery level telemetry, deep sleep power cycling (15-minute intervals), high-side P-FET power cut to achieve near-zero quiescent draw, and an OTA recovery safeguard.

---

## Preview

<p align="center">
  <img src="IMG_7318.jpeg" alt="4-Zone E-Paper Climate Display" width="700">
</p>

---

## Features

- **4-Zone Quadrant Grid**: High-contrast, clean 4-quadrant layout with double-line borders and solid header bars.
- **Dual-Scale Readouts**: Displays large bold primary temperature in **°F**, converted secondary temperature in **°C**, and **% Relative Humidity** on the same card.
- **Battery Powered with Deep Sleep**: Wakes every 15 minutes, connects to Wi-Fi & Home Assistant, updates display readings, and returns to deep sleep (~13 µA standby).
- **High-Side Power Cut (0 µA Display Quiescent)**: Drives a P-Channel MOSFET high-side switch on pin **G1** to completely de-energize the display module during sleep, preventing parasitic leakage.
- **Battery Telemetry & Percentage**: Monitored via an ADC voltage divider on pin **G2** and rendered directly in the status bar (`Bat: XX% (X.XXV)`).
- **OTA Rescue Safeguard**: Holding the onboard **BOOT** button (**G28**) at boot aborts deep sleep and keeps the device awake indefinitely for firmware flashing.
- **Status Bar Telemetry**: Shows live clock timestamp (`Updated: HH:MM`), battery percentage/voltage, and Wi-Fi signal strength (`WiFi: -XX dBm`).
- **Custom SSD1683 Driver**: Includes a dedicated ESPHome driver component for the WeAct 4.2" BWR SSD1683 panel with proper OTP waveform loading (`0xF7`), red plane memory addressing (`0x26`), and a 30s watchdog-safe refresh loop.

---

## Hardware & Wiring

### Components
1. **M5Stack Stamp-C5** (ESP32-C5 RISC-V, Wi-Fi 6, integrated battery charger) or **ESP32-C3**.
2. **WeAct Studio 4.2" Black/White/Red E-Paper Display Module** (400×300 resolution, SSD1683 controller).
3. **P-Channel MOSFET** (e.g. DMG2305UX, AO3401A, or SI2301) for high-side display power switching.
4. **Resistors**:
   - $100\text{ k}\Omega$ pull-up resistor (from 3.3V to P-MOSFET Gate).
   - $2\times 100\text{ k}\Omega$ precision resistors (voltage divider from LiPo battery (+) to GND, tapped at G2).
5. **3.7V LiPo Battery** (e.g., 2000 mAh).

---

### Pinout Map (M5Stack Stamp-C5)

Pins **G5 through G10** are dedicated contiguously to the e-paper display, while **G1** and **G2** handle power control and battery sensing:

| Signal / Pin | Stamp-C5 Pad | Function | Description / Circuit |
| :--- | :---: | :--- | :--- |
| **P-FET Gate** | **G1** | Power Switch | Drives P-MOSFET Gate (LOW = ON, HIGH/Floating = Power Cut) |
| **BAT ADC** | **G2** | Battery Monitor | Midpoint of 100kΩ / 100kΩ divider from LiPo (+) |
| **BUSY** | **G5** | Busy Status | Active HIGH busy signal from SSD1683 |
| **RES / RST** | **G6** | Hardware Reset | Active LOW reset to SSD1683 |
| **DC** | **G7** | Data / Command | Control line (LOW = Command, HIGH = Data) |
| **CS** | **G8** | Chip Select | Active LOW SPI Chip Select |
| **SDC / CLK** | **G9** | SPI SCK | SPI Serial Clock |
| **SDA / DIN** | **G10** | SPI MOSI | SPI Master Out Slave In |
| **BOOT Button**| **G28** | Safe Mode | Hold on power-up to disable deep sleep for OTA |

---

### High-Side Power Cut Circuit (Why NOT Low-Side?)

> [!CAUTION]
> **Never use a low-side N-FET (switching GND) on an SPI display.**
> When GND is disconnected, the display will parasitically draw ground through the SPI data and clock lines via the SSD1683's internal ESD protection diodes, preventing power-down and causing phantom battery drain.

```
       Stamp-C5 3V3
            │
            ├──────────────┐
            │              │
          [100k]           S (Source)
         Pull-up    ┌──[ P-MOSFET ]
            │       │      D (Drain)
  G1 ───────┴───────G      │
 (LOW = ON)                └───► Display VCC
```

---

### Battery Voltage Divider (G2)

```
  LiPo (+) ───[ 100kΩ ]───┬───[ 100kΩ ]─── GND
                          │
                          └───► Stamp-C5 G2 (ADC)
```
*(A 100k / 100k divider halves the 3.0V – 4.2V battery voltage to 1.5V – 2.1V, well within the ESP32 ADC range with 12dB attenuation).*

---

## Custom Driver Component

The repository includes a customized `components/waveshare_epaper` external component. Standard ESPHome lacks built-in support for the WeAct 4.2" BWR panel running on the SSD1683 controller:
- **Red RAM Addressing**: Writes black/white plane data to register `0x24` and red plane data to register `0x26`.
- **OTP Waveform**: Drives display refresh using master activation `0x22 -> 0xF7` and `0x20`.
- **Watchdog-Safe Refresh**: Extends `idle_timeout_()` to 30,000 ms with FreeRTOS watchdog feeding (`App.feed_wdt()`), preventing watchdog resets during the 15-second refresh cycle.
- **Deep Sleep Command**: Automatically issues SSD1683 deep sleep (`0x10, 0x01`) once the panel finishes refresh.

The driver is loaded automatically via `external_components:` in the YAML config.

---

## Software & Configuration

### Prerequisites
- [ESPHome](https://esphome.io/) (version 2024.x or later) installed in your environment.
- Home Assistant instance running on your local network.
- Ubuntu fonts (`Ubuntu-B.ttf`, `Ubuntu-M.ttf`, `Ubuntu-R.ttf`) bundled directly in `fonts/`.

### Getting Started

1. **Clone this repository**:
   ```bash
   git clone https://github.com/iboguslavsky/esphome-epaper-climate-display.git
   cd esphome-epaper-climate-display
   ```

2. **Configure Credentials**:
   Copy the example secrets file and edit it with your Wi-Fi credentials:
   ```bash
   cp secrets.yaml.example secrets.yaml
   nano secrets.yaml
   ```

3. **Customize Sensor Entity IDs**:
   Open `epaper_quad_temp.yaml` and update the `substitutions:` block with your own Home Assistant sensor entity IDs.

4. **Compile and Flash**:
   Connect your board via USB and run:
   ```bash
   esphome run epaper_quad_temp.yaml --device /dev/ttyACM0
   ```

5. **OTA Firmware Updates**:
   To update firmware wirelessly:
   - Hold the **BOOT** button (**G28**) while powering on or resetting the board.
   - The device will detect the button press, log `BOOT button (G28) held! Deep sleep CANCELLED`, and stay awake on Wi-Fi indefinitely for OTA flashing.

---

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
