Hi there. This is my first go at messing with one of these things, so I'm a little impressed I got as far as I have. This gives me an IP I can go to and see a battery voltage. As it is, it seems to work ok. I'm really looking to see if there's some way to have it keep track of the voltage value with a timestamp. I see that there's a way to connect a sd card module to it and have it log to a file on there, so I might order some of those and give that a shot later, but for now I'd be happy if it just had a running log of time and voltage value every so many seconds that displayed on that web page for as long as I had it open or something. Any ideas?
/*****************************************************
Date: 18 june 2018
Written by: Usman Ali Butt
Property off: microcontroller-project.com
* ***************************************************/
#include <ESP8266WiFi.h>
const char* ssid = "**********";
const char* password = "**********";
const int voltageSensor = A0;
float vOUT = 0.0;
float v = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
int value = 0;
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 v = 0.0;
float vOUT = 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//////////////////////////////////
value = analogRead(voltageSensor);
vOUT = (value * 3.09) / 1024.0;
v = vOUT / (R2 / (R1 + R2));
/////////////////////////////////////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(v);
client.println("<br>");
if (value == HIGH) {
client.println("Updated");
} else {
client.print("Not Updated");
}
client.println("--------");
if (v <= 5) {
client.println("Battery dead OR disconnected");
}
else if (v > 5 && v <= 10) {
client.println("Need Imediate recharge");
}
else if (v > 10 && v <= 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 disconnected");
Serial.println("");
}