I’ve built a ESP32 password protected web server, the server works fine no problems, I’ve added a .96 OLED Display to the project no problem it works just fine.
The Server is solar powered, battery operated and I want to show the battery level on the Oled.
I can run the battery level to Oled sketch just fine without the Server code. Once I included the server code, the server works just fine but the analog pin(#4) does not read the voltage level at the pin, the display constantly shows 3.3 volts.
I have unhooked the voltage divider to pin 4, the server code stills works perfectly but the display still shows 3.3 volts.
With the voltage divider connected connected to pin4 the voltage at the pin is 1.85 and the display shows 3.3v, with the voltage divider disconnected the voltage at pin4 is 0v, and the display still shows 3.3v.
It seems like the battery level code will not work with the server code. any ideas on why this would happen.
This is the Server code call for the battery status:
batteryStatus(); digitalWrite(2, HIGH); delay(50); digitalWrite(2, LOW); delay(50); WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // checking if header is valid // dXNlcjpwYXNz = 'user:pass' (user:pass) base64 encode //bWljaGFlbGIzNDQzOm13Yl8wODYz = michaelb3443:mwb_0863 // Finding the right credential string, then loads web page if (header.indexOf("bWljaGFlbGIzNDQzOm13Yl8wODYz") >= 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); if (header.indexOf("GET /34/on") >= 0) { Serial.println("GPIO 34 on"); output34State = "on"; digitalWrite(output34, HIGH); } else if (header.indexOf("GET /34/off") >= 0) { Serial.println("GPIO 34 off"); output34State = "off"; digitalWrite(output34, LOW); } // turns the GPIOs on and off else if (header.indexOf("GET /35/on") >= 0) { Serial.println("GPIO 35 on"); output35State = "on"; digitalWrite(output35, HIGH); } else if (header.indexOf("GET /35/off") >= 0) { Serial.println("GPIO 35 off"); output35State = "off"; digitalWrite(output35, LOW); }
This is the battery level code
void batteryStatus() { // get some dummy data to display float r; float ADC_PIN = 4; r = analogRead(ADC_PIN); float voltage = r * (3.30 / 4094.00); //convert the value to a true voltage. Serial.print("Voltage = "); Serial.print(voltage); Serial.println(" Volts"); Display.setTextSize(2); // note set the background color or the old text will still display Display.setTextColor(WHITE, BLACK); Display.setCursor(4, 30); //Display.print(Format(r * (3.30 / 4094.00))); Display.print(voltage); Display.println(" Volts"); //draw the bar graph Display.fillRect(r, 50, 128 - r, 10, BLACK); Display.fillRect(3, 50, r, 10, WHITE); for (int i = 1; i < 13; i++) { Display.fillRect(i * 10, 50, 2, 10, BLACK); } // now that the display is build, display it... Display.display(); } void DrawTitles(void) { Display.setTextSize(1); Display.setTextColor(WHITE); Display.setCursor(26, 4); Display.println("Battery Level"); Display.setTextSize(1); Display.setTextColor(WHITE); Display.setCursor(23, 16); Display.println("Measured Value"); Display.drawRect(0, 0, 128, 60, 2); Display.drawLine(0, 0, 128, 0, WHITE); Display.drawLine(0, 0, 0, 40, WHITE); Display.drawLine(127, 0, 127, 40, WHITE); Display.drawLine(8, 21, 16, 21, WHITE); Display.drawLine(110, 21, 118, 21, WHITE); Display.display(); } String Format(double val, int dec, int dig ) { // this is my simple way of formatting a number // data = Format(number, digits, decimals) when needed int addpad = 0; char sbuf[20]; String fdata = (dtostrf(val, dec, dig, sbuf)); int slen = fdata.length(); for ( addpad = 1; addpad <= dec + dig - slen; addpad++) { fdata = " " + fdata; } return (fdata); }
Hi Michael.
You need to chose another pin to read the battery level.
GPIO4 is part of the ADC2 pins. ADC2 pins don’t work properly when you’re using wi-fi.
So, you need to choose another GPIO that belongs to the ADC1 pins. You can chose for example, GPIO33.
Let me know if this solves your problem.
Regards,
Sara
Thank You Sara, I switched the adc pin to gpio36 and its working perfectly,if I can ask another question about the esp32 password protected webserver, The sketch included with the ebook for the server has a white background I have been trying to change it to still grey or just plain gray, when I upload the code it prints it as text on the server page, Im figuring its not formatted correctly or not placed in the proper position in the code any help will again be appreciated.
This is the code Im trying to use:
client.println(“body{ background-color: grey;}”);
Hi again.
That’s the exact line you need to use in your code.
I’ve tested that line in the web server on the course, and it works well (see the following image).
You need to insert it between the following lines (line105):
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}"); client.println("body{ background-color: grey;}"); client.println(".button2 {background-color: #555555;}</style></head>");
It needs to go between the <style> </style tags> and <head></head> tags.
If you’ve added your own custom CSS to the code, it may be interfering with the background.
I hope this helps.
Regards,
Sara