Level: Beginner Board: ESP32-C3 SuperMini Power & blackouts
⚠ Safety Li-ion cells: watch polarity, use protected cells and a charger with protection, never leave a damaged cell charging unattended.
What you need Buy the parts kit · Estimated total: $7.00
ESP32-C3 SuperMini × 1
$2.50
Resistor kit 1/4W (1k, 2k, 4.7k, 10k, 22k, 100k) × 1
$1.50
Electrolytic capacitors 100-470 uF + ceramic 100 nF × 1
$1.50
Dupont jumper wires (M-F, M-M) × 1
$1.50
Some links are affiliate links: we may earn a commission at no extra cost to you.
Code battery-divider.ino Copy Download .ino
#include <WiFi.h>
const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
bool connectWiFi(uint32_t timeoutMs = 20000) {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
uint32_t t0 = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
delay(250);
Serial.print(".");
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.print("IP: ");
Serial.println(WiFi.localIP());
return true;
}
return false;
}
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
const char* TG_TOKEN = "123456:ABC-YOUR-BOT-TOKEN"; // from @BotFather
const char* TG_CHAT = "YOUR_CHAT_ID";
// Keep the text free of double quotes and line breaks (it is inserted into JSON).
bool telegramSend(const String& text) {
if (WiFi.status() != WL_CONNECTED) return false;
WiFiClientSecure client;
client.setInsecure(); // simple; pin the certificate for production
HTTPClient http;
String url = String("https://api.telegram.org/bot") + TG_TOKEN + "/sendMessage";
if (!http.begin(client, url)) return false;
http.addHeader("Content-Type", "application/json");
String body = String("{\"chat_id\":\"") + TG_CHAT + "\",\"text\":\"" + text + "\"}";
int code = http.POST(body);
http.end();
return code == 200;
}
const int PIN_ADC = 3;
const float R1 = 100000.0, R2 = 22000.0; // divider; max 15 V gives 2.7 V at the pin
float CAL = 1.00; // multiply to match a multimeter reading
const float LOW_V = 12.0; // low battery warning (4S LiFePO4: about 12.0 V)
const uint64_t SLEEP_US = 10ULL * 60ULL * 1000000ULL;
RTC_DATA_ATTR int lowSent = 0;
float readBatteryV() {
analogReadResolution(12); analogSetPinAttenuation(PIN_ADC, ADC_11db);
long s = 0; for (int i = 0; i < 32; i++) { s += analogReadMilliVolts(PIN_ADC); delay(3); }
float mv = s / 32.0; // calibrated millivolts at the pin
return mv / 1000.0 * (R1 + R2) / R2 * CAL;
}
void setup() {
Serial.begin(115200);
float v = readBatteryV(); Serial.printf("Battery: %.2f V\n", v);
if (v < LOW_V && lowSent < 3 && connectWiFi()) { telegramSend(String("Battery low: ") + String(v, 2) + " V"); lowSent++; }
if (v >= LOW_V + 0.3) lowSent = 0;
esp_sleep_enable_timer_wakeup(SLEEP_US); esp_deep_sleep_start();
}
void loop() {}Flash a ready firmware from the browser Paste your error into the AI Error Doctor
Build tips Calibrate CAL with a multimeter; the divider draws about 0.12 mA continuously. Use 1 MΩ / 220 kΩ and a 100 nF capacitor on the ADC pin for an even lower leakage. ← ← All projects
Related ideas ⚠ Caution Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.
Wakes rarely, measures the 12 V battery at rest and warns before it is too flat to start the engine, with almost no drain of its own.
⚠ Caution Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.
Watches the battery of a 12 V mini-UPS that powers the router, shows its voltage, load and estimated runtime, and warns before it runs flat.
Runs the cabinet fan only as fast as the temperature needs, reports overheating and warns if the fan stops, so the inverter and the battery stay cool and quiet.
Comments
No comments yet. Share your experience or ask a question.