Skip to content

Commit

Permalink
refactor: add an LED indication when displaying the battery voltage o…
Browse files Browse the repository at this point in the history
…n webpage
  • Loading branch information
sayanee committed Apr 12, 2024
1 parent 188f8c6 commit dec4d71
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"valarray": "cpp",
"associative_base": "cpp",
"iterator": "cpp",
"serstream": "cpp"
"serstream": "cpp",
"__locale": "cpp"
}
}
2 changes: 0 additions & 2 deletions _code/measure-battery-level/Secret.sample.h

This file was deleted.

35 changes: 25 additions & 10 deletions _code/measure-battery-level/measure-battery-level.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#include <WiFiClient.h>
#include <WiFiAP.h>

#define LED 3
#define BATTERY_ENABLE_PIN 6
// FIX: Change pin from GPIO5 (ADC2) to GPIO0 (ADC1)
// Github Issue: https://github.com/hutscape/capsicum/issues/5
#define BATTERY_MEASURE_PIN 5
#define BATTERY_MEASURE_PIN 0

const char *ssid = "batt";
const char *password = "12345678";
Expand All @@ -19,6 +20,9 @@ void setup() {
Serial.println();
Serial.println("Configuring access point...");

pinMode(LED, OUTPUT);
pinMode(BATTERY_ENABLE_PIN, HIGH);

if (!WiFi.softAP(ssid, password)) {
log_e("Soft AP creation failed.");
while (1) {}
Expand All @@ -29,6 +33,8 @@ void setup() {
server.begin();

Serial.println("Server started");
Serial.print("Battery Analog level: ");
Serial.println(measureBatteryVoltage());
}

void loop() {
Expand All @@ -39,30 +45,39 @@ void loop() {
String currentLine = "";
while (client.connected()) {
if (client.available()) {
digitalWrite(LED, HIGH);

client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("hello world");

client.print("Battery Analog Level: ");
client.println(measureBatteryVoltage());

client.println();
break;
}
}
client.stop();
Serial.println("Client Disconnected.");
digitalWrite(LED, LOW);
}
}

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

delay(100);
pinMode(BATTERY_ENABLE_PIN, LOW);
delay(1000);

int rawValue = analogRead(BATTERY_MEASURE_PIN);
int sum = 0;
for (int i = 0; i < 100; i++) {
sum = sum + analogRead(BATTERY_MEASURE_PIN);
}
float mean = sum/100.0;
float adcValue = mean * (1.1 / 4096.0);
float batteryVoltage = adcValue * 11.0;
// TODO: Fix schematic to use 10k instead of 100k
float voltage = rawValue * (3.3 / 4095);

digitalWrite(BATTERY_ENABLE_PIN, LOW);
digitalWrite(BATTERY_ENABLE_PIN, HIGH);

return voltage;
return batteryVoltage;
}

0 comments on commit dec4d71

Please sign in to comment.