Keep your phone battery in a healthy range automatically, using a Xiaomi smart plug — fully local, no cloud, no official app.
用米家智能插座让手机电量自动保持在健康区间:低电量自动充电、充满自动断电。纯本地控制,不依赖米家云、不依赖官方 App。
Monitors your phone's battery level and toggles a Xiaomi smart plug so the phone charges only when it needs to:
- 🔋 Battery
< 30%→ plug ON (start charging) - 🔌 Battery
> 80%→ plug OFF (stop charging)
This avoids the two things that degrade lithium batteries the most: deep discharge and sitting at 100% while plugged in.
- No cloud — controls the plug directly over LAN via the
miioprotocol (UDP/TCP 54321). Works even with no internet, as long as the phone/PC and the plug share the same Wi-Fi. - No official app — no Mi Home automation, no Xiaomi account needed at runtime.
- Two editions — run it from a Windows PC (with a GUI) or natively on the phone (headless in Termux).
| Desktop 电脑版 | Android 手机版 | |
|---|---|---|
| Runs on | Windows (Python 3 + tkinter) | Android (Termux) |
| UI | GUI window | Headless + notification |
| Reads battery via | ADB wireless debugging | Termux:API |
| Best for | Keep it on a PC near the phone | Fully standalone, phone only |
android-auto-charging/
├── desktop/ # 电脑版(Windows + tkinter GUI)
│ ├── charge_guard.py # 监控主程序(图形界面)
│ ├── plug_control.py # 插座开关命令行工具
│ ├── requirements.txt
│ └── 启动充电管家.bat
└── android/ # 手机版(Termux,无头)
├── charge_guard.py # 监控主程序(后台循环)
├── plug_control.py # 插座开关命令行工具
├── setup_termux.sh # Termux 环境一键安装
├── boot_start.sh # 开机自启脚本(Termux:Boot)
└── shortcut_start.sh # 桌面快捷方式脚本(Termux:Widget)
Desktop: see desktop/README.md — install Python deps, fill in your plug's token, double-click the .bat.
Android: see android/README.md — install Termux + Termux:API, run setup_termux.sh, fill in your token.
The token is a 32-character hex string that acts as the key to your plug. You need it in every script. Two ways to get it:
- Recommended — use
Xiaomi-cloud-tokens-extractor: log in with the QR code method (Mi Home app → scan) to bypass the slider CAPTCHA, and it dumps all device tokens. - Manually extract from a Mi Home backup (older method, more involved).
Tip: also get the
did(device id) and the plug's LANipfrom the same tool / your router.
- Your plug
tokenis essentially the key to your device — never commit the real token. The scripts usePASTE_YOUR_TOKEN_HEREplaceholders; fill in your own locally. - Control is LAN-only (UDP/TCP 54321). Use only on trusted Wi-Fi.
MIT © 2026
实时监控手机电量,自动开关米家智能插座,让手机只在需要时充电:
- 🔋 电量
< 30%→ 开插座(开始充电) - 🔌 电量
> 80%→ 关插座(停止充电)
避开最伤锂电池的两件事:深度放电、以及一直插着电长期处于 100%。
- 纯本地、无云:直接通过
miio协议(UDP/TCP 54321)局域网控制插座。只要手机/电脑和插座在同一 WiFi,断网也能控制。 - 不依赖官方 App:运行时不需要米家 App、不需要小米账号。
- 两个版本:可在 Windows 电脑跑(带图形界面),也可直接在手机上跑(Termux 无头)。
见上方 Repo structure。
- 电脑版:见
desktop/README.md—— 装 Python 依赖、填入插座 token、双击.bat即可。 - 手机版:见
android/README.md—— 装 Termux + Termux:API、跑setup_termux.sh、填入 token。
token 是一个 32 位十六进制字符串,相当于插座的钥匙,每个脚本都需要填。两种获取方式:
- 推荐:用
Xiaomi-cloud-tokens-extractor,选 QR 扫码登录(米家 App 扫码确认)即可绕过滑块验证,一键导出所有设备 token。 - 从米家 App 备份中手动提取(老方法,较繁琐)。
提示:
did(设备 id)和插座的局域网ip也可从同一工具 / 路由器获取。
- 插座
token等于设备钥匙——切勿把真实 token 提交到公开仓库。脚本里用的是PASTE_YOUR_TOKEN_HERE占位符,请只在本地填你自己的。 - 控制走局域网(UDP/TCP 54321),仅在可信 WiFi 下使用。
MIT © 2026