Skip to content

Commit

Permalink
refactor (firmware): simplify web server client for reading batt voltage
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanee committed Apr 11, 2024
1 parent b89bc51 commit 97cd3e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
6 changes: 5 additions & 1 deletion _code/measure-battery-level.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ references:
url: https://github.com/espressif/arduino-esp32/blob/master/variants/esp32c3/pins_arduino.h
- name: Create an Access Point
url: https://raw.githubusercontent.com/espressif/arduino-esp32/990e3d5b431b63b4adc364b045a79afdad645a3f/libraries/WiFi/examples/WiFiAccessPoint/WiFiAccessPoint.ino
- name: How to check the battery voltage
url: https://wiki.seeedstudio.com/check_battery_voltage/
- name: Power Management
url: https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51/power-management#measuring-battery-3010518
difficulty: medium
features:
- esp32c3
Expand All @@ -29,5 +33,5 @@ images:
1. Remove the USB-C cable used for firmware upload
1. Turn on the power switch
1. Connect to access point `batt` with password `12345678`
1. Browser to `http://192.168.4.1/` on the browser
1. Browser to `http://192.168.4.1` on the browser
1. View `hello world`
2 changes: 1 addition & 1 deletion _code/measure-battery-level/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ BUILD = build
default: lint compile upload clean

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

compile: clean lint
arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./
Expand Down
40 changes: 24 additions & 16 deletions _code/measure-battery-level/measure-battery-level.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <WiFiClient.h>
#include <WiFiAP.h>

#define BATTERY_ENABLE_PIN 6
#define BATTERY_MEASURE_PIN 5

const char *ssid = "batt";
const char *password = "12345678";
WiFiServer server(80);
Expand Down Expand Up @@ -34,25 +37,30 @@ void loop() {
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("hello world");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("hello world");
client.println();
break;
}
}
client.stop();
Serial.println("Client Disconnected.");
}
}

float measureBatteryVoltage() {
pinMode(BATTERY_ENABLE_PIN, OUTPUT);
digitalWrite(BATTERY_ENABLE_PIN, HIGH);

delay(100);

int rawValue = analogRead(BATTERY_MEASURE_PIN);
// TODO: Fix schematic to use 10k instead of 100k
float voltage = rawValue * (3.3 / 4095);

digitalWrite(BATTERY_ENABLE_PIN, LOW);

return voltage;
}

0 comments on commit 97cd3e2

Please sign in to comment.