42#ifdef ARDUINO_ARCH_ESP32
48#define WIFI_SSID "YourSSID"
51#define WIFI_PASSWORD "YourPassword"
72#define CHECK_FOR_ERRORS(condition) if (condition) {log_e("BQ25896 error"); while(1)delay(1000);}
77 Serial.println(
"╔══════════════════════════════════════════╗");
78 Serial.println(
"║ BQ25896 Charger Web Monitor Test ║");
79 Serial.println(
"╚══════════════════════════════════════════╝");
93 Serial.println(
"BQ25896 begin() failed. Check wiring.");
99 Serial.println(
"BQ25896 initialized successfully");
117 Serial.print(
"Pre Charge Current: ");
119 Serial.print(
"Fast Charge Current: ");
121 Serial.print(
"Termination Current: ");
123 Serial.print(
"Charge Voltage: ");
133 Serial.print(
"Input Current Limit: ");
135 Serial.print(
"Input Voltage Limit: ");
137 Serial.print(
"Minimum System Voltage: ");
147 const char *ssid = WIFI_SSID;
148 const char *password = WIFI_PASSWORD;
149 WiFi.begin(ssid, password);
150 while (WiFi.status() != WL_CONNECTED) {
155 Serial.print(
"WiFi connected, IP: ");
156 Serial.println(WiFi.localIP());
158 server.on(
"/boost/on", []() {
160 server.send(400,
"text/plain",
"ERROR: Remove USB first before enabling boost");
164 server.send(200,
"text/plain", ret ?
"OK" :
"FAILED");
167 server.on(
"/boost/off", []() {
169 server.send(200,
"text/plain", ret ?
"OK" :
"FAILED");
172 server.on(
"/ship/on", []() {
174 server.send(400,
"text/plain",
"ERROR: Remove USB power first to enable ship mode");
178 server.send(200,
"text/plain", ret ?
"OK" :
"FAILED");
181 server.on(
"/", []() {
183 time_t now = millis() / 1000;
184 int hrs = (now / 3600) % 24;
185 int mins = (now / 60) % 60;
187 sprintf(timeStr,
"%02d:%02d:%02d", hrs, mins, secs);
192 String html =
"<html><head><meta charset='utf-8'><title>BQ25896 Charger Monitor</title>";
194 html +=
"function toggleBoost(enable) {";
195 html +=
" var xhr = new XMLHttpRequest();";
196 html +=
" xhr.open('GET', enable ? '/boost/on' : '/boost/off', true);";
197 html +=
" xhr.onload = function() { if(xhr.status==200) location.reload(); else alert(xhr.responseText); };";
198 html +=
" xhr.send();";
200 html +=
"function enableShipMode() {";
201 html +=
" if(!confirm('WARNING: Ship mode will disconnect the battery and power off the device!\\n\\nYou must connect USB power or Press PWR button to keep the device running after enabling ship mode.\\n\\nAre you sure you want to continue?')) return;";
202 html +=
" var xhr = new XMLHttpRequest();";
203 html +=
" xhr.open('GET', '/ship/on', true);";
204 html +=
" xhr.onload = function() { if(xhr.status==200) { alert('Ship mode enabled! Device will power off.'); location.reload(); } else { alert(xhr.responseText); } };";
205 html +=
" xhr.send();";
208 html +=
"</head><body>";
209 html +=
"<h1>BQ25896 Charger Status</h1>";
210 html +=
"<p><strong>Last Update: " + String(timeStr) +
"</strong></p>";
212 html +=
"<h2>Boost Control</h2>";
213 html +=
"<p>USB Status: <strong>" + String(vbusPresent ?
"Connected" :
"Not Connected") +
"</strong></p>";
214 html +=
"<p>Boost Status: <strong>" + String(boostEnabled ?
"ON" :
"OFF") +
"</strong></p>";
216 html +=
"<button onclick='toggleBoost(false)' style='background:#f44336;color:white;padding:10px 20px;border:none;cursor:pointer;'>Turn OFF Boost</button>";
217 }
else if (vbusPresent) {
218 html +=
"<button disabled style='background:#ccc;color:#666;padding:10px 20px;border:none;cursor:not-allowed;'>Turn ON Boost (USB Connected)</button>";
220 html +=
"<button onclick='toggleBoost(true)' style='background:#4CAF50;color:white;padding:10px 20px;border:none;cursor:pointer;'>Turn ON Boost</button>";
223 html +=
"<h2>Ship Mode (Battery Off)</h2>";
224 html +=
"<p>Ship mode disconnects the battery and powers off the device.</p>";
225 html +=
"<p>To exit ship mode, connect USB power.</p>";
226 html +=
"<button onclick='enableShipMode()' style='background:#FF9800;color:white;padding:10px 20px;border:none;cursor:pointer;'>Enable Ship Mode</button>";
228 html +=
"<h2>Charger Status</h2>";
229 html +=
"<table border='1' cellpadding='10'>";
235 html +=
"<tr><td>VBUS Voltage</td><td>" + String(ret ? val : -1) +
" mV</td></tr>";
238 html +=
"<tr><td>Battery Voltage</td><td>" + String(ret ? val : -1) +
" mV</td></tr>";
241 html +=
"<tr><td>VSYS Voltage</td><td>" + String(ret ? val : -1) +
" mV</td></tr>";
244 html +=
"<tr><td>Battery Current</td><td>" + String(ret ? val : -1) +
" mA</td></tr>";
247 html +=
"<tr><td>NTC Temperature</td><td>" + String(ret ? val : -1) +
" %</td></tr>";
249 html +=
"<tr><td>Charging</td><td>" + String(
bmu.
charger().
isCharging() ?
"Yes" :
"No") +
"</td></tr>";
252 html +=
"<tr><td>Online</td><td>" + String(status.
online ?
"Yes" :
"No") +
"</td></tr>";
253 html +=
"<tr><td>VBUS Present</td><td>" + String(status.
vbusPresent ?
"Yes" :
"No") +
"</td></tr>";
254 html +=
"<tr><td>Charge Done</td><td>" + String(status.
chargeDone ?
"Yes" :
"No") +
"</td></tr>";
255 html +=
"<tr><td>Fault</td><td>" + String(status.
fault ?
"Yes" :
"No") +
"</td></tr>";
256 String chargingStatusStr;
269 html +=
"<tr><td>Charging Status</td><td>" + chargingStatusStr +
"</td></tr>";
271 html +=
"<tr><td>Boost Enabled</td><td>" + String(boostEnabled ?
"Yes" :
"No") +
"</td></tr>";
275 html +=
"<tr><td>VINDPM Active</td><td>" + String(
bmu.
core().
isVindpmActive() ?
"Yes" :
"No") +
"</td></tr>";
276 html +=
"<tr><td>IINDPM Active</td><td>" + String(
bmu.
core().
isIindpmActive() ?
"Yes" :
"No") +
"</td></tr>";
280 html +=
"<meta http-equiv='refresh' content='1'>";
281 html +=
"<p>Auto-refresh every 1 second</p>";
282 html +=
"</body></html>";
284 server.send(200,
"text/html", html);
288 Serial.println(
"Web server started");
290 Serial.println(
"Setup complete!");
304 server.handleClient();
312 Serial.print(
"Fault Code: ");
316 Serial.println(
"\t Watchdog Fault");
319 Serial.println(
"\t Boost Mode Fault");
322 Serial.println(
"\t Charge Mode Fault");
325 Serial.println(
"\t - Input Fault (BUS OVP or VBAT<BUS<3.8V)");
328 Serial.println(
"\t - Thermal Shutdown");
331 Serial.println(
"\t - Safety Timer Expiration");
335 Serial.println(
"\t Battery Fault (BATOVP)");
339 Serial.println(
"\t NTC Fault");
348 Serial.begin(115200);
349 Serial.println(
"This example is only compatible with ESP32. Please run on ESP32 platform.");
#define CHECK_FOR_ERRORS(condition)
bool enableChannels(uint32_t mask) override
Enable one or more ADC channels.
static constexpr uint8_t ADC_CONV_START
ADC control flags.
bool read(Channel ch, float &out) override
Read ADC value for specified channel.
bool setFastChargeCurrent(uint16_t mA) override
Set fast charge (constant-current) current.
uint16_t getPreChargeCurrent() override
Get pre-charge current.
uint16_t getFastChargeCurrent() override
Get fast charge current.
bool setTerminationCurrent(uint16_t mA) override
Set termination current.
bool setChargeVoltage(uint16_t mV) override
Set charge voltage (constant voltage phase)
uint16_t getChargeVoltage() override
Get charge voltage.
bool isCharging() override
Check if charging is in progress.
bool setPreChargeCurrent(uint16_t mA) override
Set pre-charge current.
uint16_t getTerminationCurrent() override
Get termination current.
Status getStatus() override
Get charger status.
bool isInputCurrentOptimizerOptimized()
Check if Input Current Optimizer has optimized.
bool isVsysInRegulation()
Check if VSYS is in regulation.
bool isVindpmActive()
Get fault status.
bool isVbusPresent()
Check if VBUS (USB power) is present.
bool isIindpmActive()
Check if IINDPM is active (input current limiting)
bool setMode(Mode mode) override
Set LED operating mode.
bool isBoostEnabled() const override
Check if boost mode is enabled.
bool enableShipMode(bool enable) override
Enable or disable ship mode.
bool setMinimumSystemVoltage(uint32_t mv) override
Set minimum system voltage.
bool setInputVoltageLimit(uint32_t mv) override
Set input voltage limit (VINDPM)
bool setInputCurrentLimit(uint32_t mA) override
Set input current limit.
bool enableBoost(bool enable) override
Enable or disable OTG (boost) mode.
uint32_t getInputVoltageLimit() const override
Get input voltage limit.
uint16_t getBoostVoltage() const override
Get boost output voltage.
uint32_t getInputCurrentLimit() const override
Get input current limit.
uint32_t getMinimumSystemVoltage() const override
Get minimum system voltage.
@ BAT_VOLTAGE
Battery voltage.
@ BAT_TEMPERATURE
Battery temperature.
@ BAT_CURRENT
Battery current.
@ VBUS_VOLTAGE
VBUS voltage.
@ VSYS_VOLTAGE
VSYS voltage.
BQ25896Core & core()
Get core interface (mutable)
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialize using custom I2C callback.
BQ25896Charger & charger()
Get charger interface.
@ NO_CHARGING
Not charging, battery idle or disconnected.
@ PRE_CHARGE
Pre-charge phase (low current for weak battery)
@ TERMINATION
Charging terminated (battery full)
@ FAST_CHARGE
Fast charge phase (constant current)
@ DISABLE
LED is disabled (if supported).
bool isChargeThermalFault(uint8_t fault)
bool isChargeFault(uint8_t fault)
uint8_t getNtcFault(uint8_t fault)
uint8_t getChargeFaultType(uint8_t fault)
bool isBoostFault(uint8_t fault)
bool isBatteryFault(uint8_t fault)
bool isWatchdogTimeout(uint8_t fault)
bool isChargeTimerFault(uint8_t fault)
bool isChargeInputFault(uint8_t fault)
Charger status structure.
bool vbusPresent
VBUS detected / good (USB power present)
bool chargeDone
Charge termination/done (battery full)
uint32_t faultCode
Optional raw fault code for diagnostics.
bool fault
Any fault latched/active.
ChargingStatus chargingStatus
Charging status enumeration.
bool online
PMIC reachable / initialized.
volatile bool isFaultTrigger