I also have a battery monitor circuit I am having issues with ,but don’t want to hijack this post in any way. Found this sketch which will do what I want it to do so why reinvent the wheel ? Pretty simple circuit , voltage divider 10k resistor—47K resistor A0 at junction . Battery neg on 10k and ESP8266 neg , A0 to junction point at 47 and 10k ,+ battery at 47K . Connects to WiFi and shows Battery voltage =0.48 battery dead Or disconnected. If I check battery I get on the post 12.7 and 47K to 10K . I know this is not your circuit but was hoping for some direction here, wasn’t able to get the author on this project, Reading at A0 ref to ground 2.14 vdc and 10.57 from junction to + of 47k
Thanks
Start your code here
/*****************************************************
* Date: 18 june 2018
* Written by: Usman Ali Butt
* Property off: microcontroller-project.com
* ***************************************************/
#include <ESP8266WiFi.h>
const char* ssid = “*****”;
const char* password = “*********”;
int BAT= A0; //Analog channel A0 as used to measure battery voltage
float RatioFactor=5.714; //Resistors Ration Factor
WiFiServer server(80);
void setup() {
Serial.begin(9600);
delay(10);
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
server.begin(); // Start the server
Serial.println(“Server started”);
// Print the IP address on serial monitor
Serial.print(“Use this URL to connect: “);
Serial.print(“http://”); //URL IP to be typed in mobile/desktop browser
Serial.print(WiFi.localIP());
Serial.println(“/”);
}
void loop() {
int value = LOW;
float Tvoltage=0.0;
float Vvalue=0.0,Rvalue=0.0;
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println(“new client”);
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil(‘\r’);
Serial.println(request);
client.flush();
// Match the request
if (request.indexOf(“/bat=ON”) != -1) {
/////////////////////////////////////Battery Voltage//////////////////////////////////
for(unsigned int i=0;i<10;i++){
Vvalue=Vvalue+analogRead(BAT); //Read analog Voltage
delay(5); //ADC stable
}
Vvalue=(float)Vvalue/10.0; //Find average of 10 values
Rvalue=(float)(Vvalue/1024.0)*5; //Convert Voltage in 5v factor
Tvoltage=Rvalue*RatioFactor; //Find original voltage by multiplying with factor
/////////////////////////////////////Battery Voltage//////////////////////////////////
value = HIGH;
}
// Return the response
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“”); // do not forget this one
client.println(“<!DOCTYPE HTML>”);
client.println(“<html>”);
client.println(“Battery Voltage =”);
client.print(Tvoltage);
client.println(“<br>”);
if(value == HIGH) {
client.println(“Updated”);
} else {
client.print(“Not Updated”);
}
client.println(“——–“);
if(Tvoltage<=5){
client.println(“Battery dead OR disconnected”);
}
else if(Tvoltage>5 && Tvoltage<=10){
client.println(“Need Imediate recharge”);
}
else if(Tvoltage>10 && Tvoltage<=12){
client.println(“Recharge”);
}
else{
client.println(“Battery Full”);
}
client.println(“<br><br>”);
client.println(“<a href=\”/bat=ON\”\”><button>Status</button></a><br />”);
client.println(“</html>”);
delay(1);
Serial.println(“Client disonnected”);
Serial.println(“”);
}
Ok so I resolved my issue , it was hardware but now I get on the web Battery full 19.42v and with my fluke meter across the battery terminals I get 12.7v so it has to be a scaling issue. I changed the line of code that measures the voltage Rvalue=(float)(Vvalue/1024.0)*5.0; to Rvalue=(float)(Vvalue/1024.0)*3.3; Now I read 12.82v , close enough but will probably tweak it more. Thanks for reading this.
Note that at the two ends of the range, (i.e. less than 0.5 volts and more than 12.2 volts for a lithium-ion 3-cell series), the A to D converter becomes slightly non-linear on the ESP32, which you may want to correct in software.
Just make sure you don’t blow up the analog port. Put a 1 K resistor in series and a voltage divider to bring down the battery voltage to 5 or 3.3 volts.