Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Android Auto-Charging ⚡

Keep your phone battery in a healthy range automatically, using a Xiaomi smart plug — fully local, no cloud, no official app.

用米家智能插座让手机电量自动保持在健康区间:低电量自动充电、充满自动断电。纯本地控制,不依赖米家云、不依赖官方 App。

English · 中文


English

What it does

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.

Why it's different

  • No cloud — controls the plug directly over LAN via the miio protocol (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).

Editions

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

Repo structure

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)

Quick start

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.

Getting your plug's 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:

  1. 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.
  2. Manually extract from a Mi Home backup (older method, more involved).

Tip: also get the did (device id) and the plug's LAN ip from the same tool / your router.

Security

  • Your plug token is essentially the key to your device — never commit the real token. The scripts use PASTE_YOUR_TOKEN_HERE placeholders; fill in your own locally.
  • Control is LAN-only (UDP/TCP 54321). Use only on trusted Wi-Fi.

License

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

token 是一个 32 位十六进制字符串,相当于插座的钥匙,每个脚本都需要填。两种获取方式:

  1. 推荐:用 Xiaomi-cloud-tokens-extractor,选 QR 扫码登录(米家 App 扫码确认)即可绕过滑块验证,一键导出所有设备 token。
  2. 从米家 App 备份中手动提取(老方法,较繁琐)。

提示:did(设备 id)和插座的局域网 ip 也可从同一工具 / 路由器获取。

安全提醒

  • 插座 token 等于设备钥匙——切勿把真实 token 提交到公开仓库。脚本里用的是 PASTE_YOUR_TOKEN_HERE 占位符,请只在本地填你自己的。
  • 控制走局域网(UDP/TCP 54321),仅在可信 WiFi 下使用。

许可证

MIT © 2026

About

Automatically charge your phone via a Xiaomi smart plug - local, cloud-free. Desktop + Android editions.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages