Skip to content

Commit bb2149b

Browse files
committed
Add Battery Percentage
Disable Battery Display when monitoring not enabled for board Print built-in board templates in debug mode
1 parent 74dc551 commit bb2149b

5 files changed

Lines changed: 108 additions & 20 deletions

File tree

tinyGS/src/ConfigManager/ConfigManager.cpp

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "../Radio/Radio.h"
2424
#include "../Display/graphics.h"
2525
#include "../Mqtt/MQTT_credentials.h"
26+
#include "../Power/Battery.h"
2627
#include "ArduinoJson.h"
2728
#if ARDUINOJSON_USE_LONG_LONG == 0 && !PLATFORMIO
2829
#error "Using Arduino IDE is not recommended, please follow this guide https://github.com/G4lile0/tinyGS/wiki/Arduino-IDE or edit /ArduinoJson/src/ArduinoJson/Configuration.hpp and amend to #define ARDUINOJSON_USE_LONG_LONG 1 around line 68"
@@ -53,17 +54,17 @@ ConfigManager::ConfigManager()
5354
//OLED_add, OLED_SDA, OLED_SCL, OLED_RST, PROG_BUTTON, BOARD_LED, L_SX127X?, L_NSS, L_DI00, L_DI01, L_BUSSY, L_RST, L_MISO, L_MOSI, L_SCK, L_TCXO_V, RX_EN, TX_EN, ADC_CTL, BAT_AIN, VBAT_SCALE, BOARD
5455
#if CONFIG_IDF_TARGET_ESP32S3
5556
{ 0x3c, 17, 18, 21, 0, 35, RADIO_SX1262, 8, UNUSED, 14, 13, 12, 11, 10, 9, 1.6f, UNUSED, UNUSED, 37, 1, 0.00405f, "150–960Mhz - HELTEC LORA32 V3 SX1262" }, // SX1262
56-
{ 0x3c, 17, 18, UNUSED, 0, 35, RADIO_SX1278, 8, 6, 14, UNUSED, 12, 11, 10, 9, 0.0f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "Custom ESP32-S3 433MHz SX1278" }, // SX1278 @g4lile0
57-
{ 0x3c, 17, 18, UNUSED, 0, 3, RADIO_SX1262, 10, UNUSED, 1, 4, 5, 13, 11, 12, 1.6f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "433 Mhz TTGO T-Beam Sup SX1262 V1.0" }, // SX1268 @ Stephen
58-
{ 0x3c, 17, 18, UNUSED, 0, 37, RADIO_SX1280, 7, UNUSED, 9, UNUSED, 8, 3, 6, 5, 0.0f, 21, 10, UNUSED, UNUSED, UNUSED, "2.4Ghz LILYGO SX1280" }, // SX1280 @ K4KDR
57+
{ 0x3c, 17, 18, UNUSED, 0, 35, RADIO_SX1278, 8, 6, 14, UNUSED, 12, 11, 10, 9, 0.0f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "Custom ESP32-S3 433MHz SX1278" }, // SX1278 @g4lile0
58+
{ 0x3c, 17, 18, UNUSED, 0, 3, RADIO_SX1262, 10, UNUSED, 1, 4, 5, 13, 11, 12, 1.6f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "433 Mhz TTGO T-Beam Sup SX1262 V1.0" }, // SX1268 @ Stephen
59+
{ 0x3c, 17, 18, UNUSED, 0, 37, RADIO_SX1280, 7, UNUSED, 9, UNUSED, 8, 3, 6, 5, 0.0f, 21, 10, UNUSED, UNUSED, UNUSED, "2.4Ghz LILYGO SX1280" }, // SX1280 @ K4KDR
5960
#elif CONFIG_IDF_TARGET_ESP32C3
6061
{ 0x3c, 0, 1, UNUSED, 20, 21, RADIO_SX1262, 8, UNUSED, 3, 4, 5, 6, 7, 10, 1.6f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "433MHz HELTEC LORA32 HT-CT62 SX1262" }, // SX1262 @gargomoma
6162
{ 0x3c, 0, 1, UNUSED, 20, 21, RADIO_SX1278, 8, 4, UNUSED, UNUSED, 5, 6, 7, 10, 0.0f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "Custom ESP32-C3 433MHz SX1278" }, // SX1278 @gargomoma
6263
#else
63-
{ 0x3c, 4, 15, 16, 0, 25, RADIO_SX1278, 18, 26, 12, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 36, UNUSED, "433MHz HELTEC WiFi LoRA 32 V1" }, // SX1278 @4m1g0
64-
{ 0x3c, 4, 15, 16, 0, 25, RADIO_SX1276, 18, 26, 12, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 36, UNUSED, "863-928MHz HELTEC WiFi LoRA 32 V1" }, // SX1276
65-
{ 0x3c, 4, 15, 16, 0, 25, RADIO_SX1278, 18, 26, 35, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 36, UNUSED, "433MHz HELTEC WiFi LoRA 32 V2" }, // SX1278 @4m1g0
66-
{ 0x3c, 4, 15, 16, 0, 25, RADIO_SX1276, 18, 26, 35, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 36, UNUSED, "863-928MHz HELTEC WiFi LoRA 32 V2" }, // SX1276
64+
{ 0x3c, 4, 15, 16, 0, 25, RADIO_SX1278, 18, 26, 12, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "433MHz HELTEC WiFi LoRA 32 V1" }, // SX1278 @4m1g0
65+
{ 0x3c, 4, 15, 16, 0, 25, RADIO_SX1276, 18, 26, 12, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "863-928MHz HELTEC WiFi LoRA 32 V1" }, // SX1276
66+
{ 0x3c, 4, 15, 16, 0, 25, RADIO_SX1278, 18, 26, 35, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, 21, 37, 0.00283f, "433MHz HELTEC WiFi LoRA 32 V2" }, // SX1278 @4m1g0
67+
{ 0x3c, 4, 15, 16, 0, 25, RADIO_SX1276, 18, 26, 35, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, 21, 37, 0.00283f, "863-928MHz HELTEC WiFi LoRA 32 V2" }, // SX1276
6768
{ 0x3c, 4, 15, 16, 0, 2, RADIO_SX1278, 18, 26, UNUSED, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "433Mhz TTGO LoRa 32 v1" }, // SX1278 @g4lile0
6869
{ 0x3c, 4, 15, 16, 0, 2, RADIO_SX1276, 18, 26, UNUSED, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "868-915MHz TTGO LoRa 32 v1" }, // SX1276
6970
{ 0x3c, 21, 22, UNUSED, 0, 22, RADIO_SX1278, 18, 26, 33, UNUSED, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "433MHz TTGO LoRA 32 v2" }, // SX1278 @TCRobotics
@@ -261,7 +262,12 @@ void ConfigManager::handleDashboard()
261262
s += "<tr><td>WiFi RSSI </td><td>" + String(WiFi.isConnected() ? "<span class='G'>CONNECTED</span>" : "<span class='R'>NOT CONNECTED</span>") + "</td></tr>";
262263
s += "<tr><td>Radio </td><td>" + String(Radio::getInstance().isReady() ? "<span class='G'>READY</span>" : "<span class='R'>NOT READY</span>") + "</td></tr>";
263264
s += "<tr><td>Noise floor </td><td>" + String(status.modeminfo.currentRssi) + "</td></tr>";
264-
s += "<tr><td>Battery </td><td>" + String(status.vbat) + "V</td></tr>";
265+
if (status.vbat > 0) {
266+
s += "<tr><td>Battery </td><td>" + String(status.vbat) + "V " + String(getBatteryPercentage()) + "&percnt;</td></tr>";
267+
} else {
268+
// Empty if battery monitoring not enabled
269+
s += "<tr><td></td><td></td></tr>";
270+
}
265271
s += F("</table></div>");
266272

267273

@@ -498,7 +504,12 @@ void ConfigManager::handleRefreshWorldmap()
498504
if (status.radio_ready)
499505
radio.currentRssi ();
500506
data_string += String(status.modeminfo.currentRssi) + ",";
501-
data_string += String(status.vbat) + ",";
507+
if (status.vbat > 0) {
508+
data_string += String(status.vbat) + "V " + String(getBatteryPercentage()) + "&percnt;,";
509+
} else {
510+
// if battery monitoring not enabled
511+
data_string += "Unknown,";
512+
}
502513

503514

504515
// sat_info
@@ -744,8 +755,8 @@ void ConfigManager::printConfig()
744755
Log::debug(PSTR("tz: %s\nOLED Bright: %u\nTX %s"), getTZ(), getOledBright(), getAllowTx() ? "Enable" : "Disable");
745756
if (getBoardTemplate()[0] != '\0')
746757
Log::debug(PSTR("board_template: %s"),getBoardTemplate());
747-
else
748-
Log::debug(PSTR("board: %u --> %s\n:"),getBoard(), boards[getBoard()].BOARD.c_str());
758+
else
759+
Log::debug(PSTR("board_template: %s"), boardTemplateToJSON(boards[getBoard()]).c_str() );
749760
}
750761

751762
// void ConfigManager::setMqttServer(const char *server)
@@ -1029,3 +1040,36 @@ bool ConfigManager::parseBoardTemplate(board_t &board)
10291040

10301041
return true;
10311042
}
1043+
1044+
String ConfigManager::boardTemplateToJSON(board_t &board)
1045+
{
1046+
size_t size = JSON_OBJECT_SIZE(22) + 256;
1047+
DynamicJsonDocument doc(size);
1048+
String output;
1049+
1050+
doc["aADDR"] = board.OLED__address;
1051+
doc["oSDA"] = board.OLED__SDA;
1052+
doc["oSCL"] = board.OLED__SCL;
1053+
doc["oRST"] = board.OLED__RST;
1054+
doc["pBut"] = board.PROG__BUTTON;
1055+
doc["led"] = board.BOARD_LED;
1056+
doc["radio"] = board.L_radio;
1057+
doc["lNSS"] = board.L_NSS;
1058+
doc["lDIO0"] = board.L_DI00;
1059+
doc["lDIO1"] = board.L_DI01;
1060+
doc["lBUSSY"] = board.L_BUSSY;
1061+
doc["lRST"] = board.L_RST;
1062+
doc["lMISO"] = board.L_MISO;
1063+
doc["lMOSI"] = board.L_MOSI;
1064+
doc["lSCK"] = board.L_SCK;
1065+
doc["lTCXOV"] = board.L_TCXO_V;
1066+
doc["RXEN"] = board.RX_EN;
1067+
doc["TXEN"] = board.TX_EN;
1068+
doc["ADC_CTL"] = board.ADC_CTL;
1069+
doc["VBAT"] = board.VBAT_AIN;
1070+
doc["VBAT_SCALE"] = board.VBAT_SCALE;
1071+
doc["name"] = board.BOARD;
1072+
1073+
serializeJson(doc, output);
1074+
return output;
1075+
}

tinyGS/src/ConfigManager/ConfigManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class ConfigManager : public IotWebConf2
301301
void parseAdvancedConf();
302302
void parseModemStartup();
303303
bool parseBoardTemplate(board_t &);
304+
String boardTemplateToJSON(board_t &board);
304305

305306
std::function<boolean(iotwebconf2::WebRequestWrapper *)> formValidatorStd;
306307
DNSServer dnsServer;

tinyGS/src/Display/Display.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "graphics.h"
2222
#include "../ConfigManager/ConfigManager.h"
2323
#include "../Mqtt/MQTT_credentials.h"
24+
#include "../Power/Battery.h"
2425

2526
SSD1306* display;
2627
OLEDDisplayUi* ui = NULL;
@@ -238,7 +239,7 @@ void drawFrame3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int1
238239
if (status.vbat != 0.0)
239240
{
240241
display->setTextAlignment(TEXT_ALIGN_LEFT);
241-
display->drawString(x, 45 + y, "Bat: " + String(status.vbat) + "V");
242+
display->drawString(x, 45 + y, "Bat: " + String(status.vbat) + "V " + (getBatteryPercentage()) + "%");
242243
}
243244
}
244245

tinyGS/src/Power/Battery.cpp

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ void initBatteryMonitoring(void)
66
{
77
board_t board;
88
if (ConfigManager::getInstance().getBoardConfig(board)) {
9-
Log::console (PSTR ("Setting up battery monitoring pin"));
10-
if (board.ADC_CTL != UNUSED)
9+
// If ADC_CTL is defined, set it up
10+
if (board.ADC_CTL != UNUSED) {
1111
pinMode(board.ADC_CTL, OUTPUT); // Enable pin for ADC reading
12+
}
1213
if (board.VBAT_AIN != UNUSED) {
1314
pinMode(board.VBAT_AIN, INPUT); // VBAT monitoring pin
1415
adcAttachPin(board.VBAT_AIN);
@@ -18,6 +19,21 @@ void initBatteryMonitoring(void)
1819
}
1920
}
2021

22+
inline void enableADCReading(board_t board)
23+
{
24+
if (board.ADC_CTL != UNUSED) {
25+
digitalWrite(board.ADC_CTL, ADC_READ_ENABLE);
26+
delay(50); // Allow voltage to stabilize
27+
}
28+
}
29+
30+
inline void disableADCReading(board_t board)
31+
{
32+
if (board.ADC_CTL != UNUSED) {
33+
digitalWrite(board.ADC_CTL, ADC_READ_DISABLE);
34+
}
35+
}
36+
2137
void checkBattery(void)
2238
{
2339
static unsigned long lastReadTime = 0;
@@ -28,16 +44,33 @@ void checkBattery(void)
2844
lastReadTime = millis();
2945
if (ConfigManager::getInstance().getBoardConfig(board)) {
3046
if (board.VBAT_AIN != UNUSED && board.VBAT_SCALE != UNUSED) {
31-
digitalWrite(board.ADC_CTL, HIGH); // Enable ADC reading
32-
delay(100); // Allow voltage to stabilize
33-
uint16_t temp = analogRead(board.VBAT_AIN);
34-
digitalWrite(board.ADC_CTL, LOW); // Disable ADC reading to save power
47+
48+
enableADCReading(board);
49+
50+
uint16_t raw = analogRead(board.VBAT_AIN);
51+
52+
disableADCReading(board);
53+
3554
if (status.vbat == 0.0) {
36-
status.vbat = board.VBAT_SCALE * temp;
55+
status.vbat = board.VBAT_SCALE * raw;
3756
} else {
38-
status.vbat = (status.vbat + (board.VBAT_SCALE * temp))/2;
57+
status.vbat = (status.vbat + (board.VBAT_SCALE * raw))/2;
3958
}
4059
}
4160
}
4261
}
62+
}
63+
64+
float getBatteryPercentage(void)
65+
{
66+
const float maxVoltage = 3.7; // Voltage at 100%
67+
const float minVoltage = 3.0; // Voltage at 0%
68+
float voltage = status.vbat;
69+
if (voltage >= maxVoltage) {
70+
return 100.0;
71+
} else if (voltage <= minVoltage) {
72+
return 0.0;
73+
} else {
74+
return ((voltage - minVoltage) / (maxVoltage - minVoltage)) * 100.0;
75+
}
4376
}

tinyGS/src/Power/Battery.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77

88
#define BATTERY_CHECK_INTERVAL 10000
99

10+
#if CONFIG_IDF_TARGET_ESP32S3
11+
#define ADC_READ_ENABLE HIGH
12+
#define ADC_READ_DISABLE LOW
13+
#else
14+
#define ADC_READ_ENABLE LOW
15+
#define ADC_READ_DISABLE HIGH
16+
#endif
17+
1018
extern Status status;
1119

1220
void initBatteryMonitoring(void);
1321
void checkBattery(void);
22+
float getBatteryPercentage(void);
1423

1524
#endif

0 commit comments

Comments
 (0)