# Makita XGT 40V Battery Diagnostics & Lockout Reset Tool

*Read this in other languages: [Русский](README.ru.md), [Українська](README.uk.md).*

A standalone ESP32-based tool that talks directly to the internal BMS of a
Makita XGT ("40V Max") battery pack over its single-wire `TR` data line. It
reads live telemetry (per-cell voltages, temperatures, charge cycles, health,
lockout status) and can send the battery's documented **lockout-flag reset
sequence**, all through a Wi-Fi web dashboard served by the ESP32 itself — no
app, no cloud, no PC required.

> This project builds entirely on reverse-engineering work published by other
> people — see [Credits & Sources](#credits--sources). This repository's job
> is to turn that research into a working, self-contained diagnostic device.

![Web dashboard screenshot](docs/images/dashboard-screenshot.png)

## ⚠️ Safety disclaimer — read this first

- This is **not** an official Makita tool, and none of the protocol
  information here comes from Makita. It was reverse-engineered by observing
  traffic between genuine batteries, chargers and tools. It may be incomplete
  or wrong, and Makita's protocol may be covered by patents in your
  jurisdiction — check the situation where you live before publishing
  derivative hardware.
- A battery's **lockout flag is usually set for a real reason** — cell
  imbalance beyond a safety threshold, over-discharge, or a thermal event.
  **Clearing the flag does not fix the underlying condition.** Manufacturers
  pot BMS boards in epoxy specifically to discourage this kind of reset.
  Always read the full cell-voltage/temperature telemetry *before* resetting
  a lockout, and do not keep using a pack that shows an abnormal cell
  voltage spread or repeated re-lockouts.
- Li-ion packs that have been over-discharged or sat in a fault state can be
  genuinely dangerous (fire/venting risk) if forced back into service. If in
  doubt, don't.
- You are working with a 40V DC power source. Design any circuitry you build
  to tolerate at least 40–45V on every signal pin (measured up to 33V on
  clone-brand batteries even on the "low voltage" `TR` line) and always fuse
  the power tap.
- Everything in this repository is provided with **no warranty whatsoever**.
  You take full responsibility for anything you build or run using this
  information, including damage to batteries, tools, chargers, or anything
  else. If you're not comfortable with that, stop here.

## Features

- Reads: model name, charge cycle count, remaining/design capacity, charge %,
  two temperature sensors, all individual cell voltages (+ pack voltage and
  cell-to-cell delta), parallel/series cell configuration, and the lockout
  flag.
- Sends the documented two-command **lockout-flag reset sequence** on demand.
- Self-hosted Wi-Fi access point + single-page web dashboard (no companion
  app, no internet dependency) with live updates over WebSocket, responsive
  from phone to desktop.
- **Captive portal**: connecting to the AP pops the dashboard open
  automatically (same mechanism as public/hotel Wi-Fi login pages), via a
  wildcard-DNS responder + a catch-all HTTP handler. Falls back to a manual
  browse to `http://192.168.4.1/` if a particular OS/browser doesn't trigger it.
- **Wi-Fi settings panel** in the UI: change the AP's SSID/password (or make
  it an open network) from the dashboard itself; validated and persisted to
  NVS (survives reboot/power loss), with the device restarting automatically
  to apply the new credentials.
- **Bilingual interface** (English / Ukrainian) with a one-click switcher in
  the header; the choice is remembered per-browser (`localStorage`) and
  defaults to the browser's own language on first visit.
- Schematic **10S cell-balance diagram**: each cell rendered individually
  with live voltage + delta-from-average, color-coded (OK / watch /
  attention) so an imbalanced or failing cell stands out at a glance.
- Rule-based **diagnostics/recommendations panel** that turns the raw
  numbers into plain-language guidance (cell imbalance, over/under-voltage,
  high temperature, cycle wear, capacity health, low charge, active
  lockout) — a starting point, not a substitute for manufacturer service.
- **Debug panel**: raw JSON telemetry viewer, a scrollback log of the last
  20 WebSocket messages, a manual "Refresh Now" (forces an immediate poll
  instead of waiting out the normal interval), and an onboard-LED
  self-test button.
- Runs standalone, powered directly from the battery pack itself (see
  [`docs/HARDWARE.md`](docs/HARDWARE.md)) — no USB/PC needed once flashed.
- Minimal wiring: **3 wires** to the battery (`B+`, `B-`, `TR`) for normal
  diagnostics + reset; a 4th (`DS/WK`) is only needed for the edge case of
  reviving a fully-shutdown/dead pack.

## Hardware target

- **MCU:** ESP32-S3 ("Super Mini" dev board), Arduino framework, built with
  PlatformIO.
- **Bus interface:** a single bit-banged GPIO (matching the battery's
  half-duplex `TR` line), with a minimal series-resistor + clamp-diode
  protection network. See [`docs/HARDWARE.md`](docs/HARDWARE.md) for the full
  wiring diagram, connector pinout, and the battery-powered supply design
  (40V pack → buck converter → 5V → onboard 3.3V regulator).

## How the protocol works (summary)

Full write-up with every register and both packet formats is in
[`docs/PROTOCOL.md`](docs/PROTOCOL.md). Short version:

- Physical layer: 9600 baud, 8 data bits, even parity, 1 stop bit, but
  **inverted** (idle = 0V = logical 1, a pulse to +5V = logical 0) and
  **MSB-first**, on a single shared `TR` wire (half duplex).
- Two packet shapes: an 8-byte **short form**
  (`0xCC CRC CMD arg0 arg1 arg2 arg3 0x33`) used for essentially all register
  reads/writes, and a longer `0xA5A5`-prefixed **long form** (always a
  multiple of 16 bytes) used for capability negotiation between real
  chargers/tools and the battery.
- The **lockout-reset sequence** — cross-validated against two independent
  reverse-engineering projects that arrived at byte-identical results —
  is two short-form commands:
  1. `CMD 0xD9`, args `[0x96, 0xA5]`
  2. `CMD 0xD2`, args `[0x2F]`

## Repository layout

```
.
├── docs/
│   ├── PROTOCOL.md      # full protocol reference: framing, checksum, register map
│   └── HARDWARE.md      # wiring, connector pinout, power supply design, BOM
├── src/
│   ├── XgtBus.{h,cpp}       # bit-banged half-duplex single-wire UART
│   ├── XgtProtocol.{h,cpp}  # packet framing, CRC, short/long-form transactions
│   ├── XgtRegisters.h       # named constants for commands/registers
│   ├── XgtBattery.{h,cpp}   # high-level telemetry read + lockout reset API
│   ├── StatusLed.{h,cpp}    # onboard NeoPixel self-test (board-pin dependent, see file)
│   ├── WifiConfig.{h,cpp}   # NVS-persisted AP SSID/password (edited from the web UI)
│   ├── WebUI.{h,cpp}        # Wi-Fi AP + captive portal + embedded bilingual web
│   │                        # dashboard (EN/UK) + WebSocket/REST API
│   └── main.cpp             # task wiring: battery polling task + web server
├── platformio.ini
└── LICENSE
```

## Building & flashing

1. Install [PlatformIO](https://platformio.org/) (VS Code extension or CLI).
2. Open this folder as a PlatformIO project.
3. Adjust `XGT_BUS_PIN`, Wi-Fi AP SSID/password, and the `board` entry in
   `platformio.ini` if your specific ESP32-S3 board variant needs it (flash
   size / PSRAM settings).
4. `pio run -t upload` (or the PlatformIO toolbar button).
5. Wire the battery as described in [`docs/HARDWARE.md`](docs/HARDWARE.md),
   power up, connect to the `XGT-Diagnostics` Wi-Fi AP it creates, and browse
   to `http://192.168.4.1/`.

## Project status

Early / unverified against real hardware — this is a from-scratch firmware
implementation derived from the referenced protocol research, not yet
bench-tested against a physical XGT battery. Treat all register
interpretations as provisional until confirmed on your own hardware, and
**read telemetry before ever pressing the reset button.**

### Roadmap

- [ ] Bench-test the bit-bang timing against a real battery, tune retry/
      timeout constants.
- [ ] Persist a reading history (LittleFS) with CSV export from the web UI.
- [ ] Generic long-form packet builder (currently only the fixed model-name
      query is implemented; charger/tool capability-negotiation commands are
      documented in `docs/PROTOCOL.md` but not yet wired up).
- [ ] Optional STA mode + mDNS (`xgt.local`) as an alternative to AP-only.
- [ ] Auto power-down / deep-sleep to minimize parasitic drain on the pack
      when left connected.

## Credits & sources

This project would not exist without the reverse-engineering work published
by:

- **Malvineous** — [makita-xgt-serial](https://github.com/Malvineous/makita-xgt-serial)
  (electrical protocol + full long-form packet/command reference)
- **twaymouth** — [XGT-Tester](https://github.com/twaymouth/XGT-Tester)
  (ESP32-C3 tester firmware; the short-form command bytes this project's
  register map was decoded from)
- **no-body-in-particular** — [m5din-makita-xgt](https://github.com/no-body-in-particular/m5din-makita-xgt)
  (M5Din lockout-reset tool; single-GPIO bit-bang driver and a full BMS
  emulator that independently confirms the register map and the unlock
  sequence)
- Checksum algorithm reverse-engineered by **ESkri**
  ([Stack Exchange answer](https://reverseengineering.stackexchange.com/a/32347/20888))
- Additional community data: [sites.google.com/view/xgtdata](https://sites.google.com/view/xgtdata/home)

Per Malvineous's request in the original documentation: if you use this
information, please link back to the sources above.

## License

[MIT](LICENSE) — see the safety disclaimer above; the license's "no warranty"
clause is doing a lot of work here.
