Measure battery voltage ESP8266

Updated on 3 September 2021
dev board WeMos D1 Mini
chip esp8266
features LiPo battery voltage
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Buy the components

Code

Download code measure-battery-voltage.ino
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

String AP_NamePrefix = "Cactus ";
const char WiFiAPPSK[] = "hutscape";

unsigned int raw = 0;
float volt = 0.0;

ESP8266WebServer server(80);

void setup() {
  Serial.begin(115200);
  pinMode(A0, INPUT);

  initAccessPoint();
}

void loop() {
  raw = analogRead(A0);
  volt = raw / 1023.0;
  volt *= 4.2;

  Serial.print("\n[INFO] Raw analog value is ");
  Serial.println(raw);

  Serial.print("[INFO] Current voltage is ");
  Serial.print(volt);
  Serial.println("V");

  server.handleClient();
}

void initAccessPoint() {
  Serial.println("\n[INFO] Configuring access point");
  WiFi.mode(WIFI_AP);

  String AP_NameString = getAPName();

  // convert String to char array
  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i=0; i < AP_NameString.length(); i++)
    AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAP(AP_NameChar, WiFiAPPSK);

  startServer();
  Serial.print("[INFO] Started access point at IP ");
  Serial.println(WiFi.softAPIP());
}

String getAPName() {
  uint8_t mac[WL_MAC_ADDR_LENGTH];
  WiFi.softAPmacAddress(mac);
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  macID.toUpperCase();
  return AP_NamePrefix + macID;
}

void startServer() {
  server.on("/", handleRoot);

  const char * headerkeys[] = {"User-Agent", "Cookie"};
  size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);

  server.collectHeaders(headerkeys, headerkeyssize);
  server.begin();
}

void handleRoot() {
  String content = "<h1>Check the current LiPo voltage!</h1>";
  content += "<p>Battery voltage: ";
  content += String(volt);
  content += "V</p>";

  server.send(200, "text/html", content);
  Serial.println("[INFO] Called /GET 200");
}

Makefile

.PHONY: lint compile upload clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright,-readability/todo *.ino

compile:
	arduino-cli compile --fqbn esp8266com:esp8266:d1_mini ./

upload: compile
	arduino-cli upload -p /dev/cu.wchusbserial1410 --fqbn esp8266com:esp8266:d1_mini ./

clean:
	rm -f .*.bin
	rm -f .*.elf

flash: lint compile upload clean

Browser console

Open the browser developer console.

Measure battery voltage ESP8266 browser

Description

Measure the LiPo battery voltage and ping that value to the cloud.

Connect 100k resistor from A0 to JST connector’s positive terminal of the WeMos battery shield.

Instructions:

  1. Power via LiPo through the JST connector on the WeMos battery shield and not via the USB power on the WeMos
  2. Connect to access point Cactus XXXX with password hutscape
  3. Get the current battery voltage by accessing cactus.local

References