From 32a806afae1a4395abd749bf8093d52cb1e1f1fd Mon Sep 17 00:00:00 2001 From: Matthew Kendall Date: Wed, 3 Aug 2022 19:58:47 -0700 Subject: [PATCH 1/4] Add battery voltage monitoring feature --- tinyGS/src/ConfigManager/ConfigManager.cpp | 47 ++++++++++++---------- tinyGS/src/ConfigManager/ConfigManager.h | 2 + tinyGS/src/ConfigManager/html.h | 4 +- tinyGS/src/Display/Display.cpp | 5 +++ tinyGS/src/Status.h | 1 + tinyGS/tinyGS.ino | 19 +++++++++ 6 files changed, 54 insertions(+), 24 deletions(-) diff --git a/tinyGS/src/ConfigManager/ConfigManager.cpp b/tinyGS/src/ConfigManager/ConfigManager.cpp index 54943129..1e691fff 100644 --- a/tinyGS/src/ConfigManager/ConfigManager.cpp +++ b/tinyGS/src/ConfigManager/ConfigManager.cpp @@ -49,26 +49,26 @@ na SX1281 2.4–2.5Ghz 130 5.5 2000 0.476-202 ConfigManager::ConfigManager() : IotWebConf2(thingName, &dnsServer, &server, initialApPassword, configVersion), server(80), gsConfigHtmlFormatProvider(*this), boards({ - //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, BOARD - { 0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 12, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "433MHz HELTEC WiFi LoRA 32 V1" }, // SX1278 @4m1g0 - { 0x3c, 4, 15, 16, 0, 25, 2, 18, 26, 12, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "863-928MHz HELTEC WiFi LoRA 32 V1" }, // SX1276 - { 0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 35, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "433MHz HELTEC WiFi LoRA 32 V2" }, // SX1278 @4m1g0 - { 0x3c, 4, 15, 16, 0, 25, 2, 18, 26, 35, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "863-928MHz HELTEC WiFi LoRA 32 V2" }, // SX1276 - { 0x3c, 4, 15, 16, 0, 2, 1, 18, 26, UNUSED, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "433Mhz TTGO LoRa 32 v1" }, // SX1278 @g4lile0 - { 0x3c, 4, 15, 16, 0, 2, 2, 18, 26, UNUSED, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "868-915MHz TTGO LoRa 32 v1" }, // SX1276 - { 0x3c, 21, 22, 16, 0, 22, 1, 18, 26, 33, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "433MHz TTGO LoRA 32 v2" }, // SX1278 @TCRobotics - { 0x3c, 21, 22, 16, 0, 22, 2, 18, 26, 33, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "868-915MHz TTGO LoRA 32 v2" }, // SX1276 - { 0x3c, 21, 22, 16, 39, 22, 1, 18, 26, 33, 32, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "433MHz T-BEAM + OLED" }, // SX1278 - { 0x3c, 21, 22, 16, 39, 22, 2, 18, 26, 33, 32, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "868-915MHz T-BEAM + OLED" }, // SX1276 - { 0x3c, 21, 22, 16, 0, 25, 5, 5, UNUSED, 27, 26, 14, 19, 23, 18, 0.0f, UNUSED, UNUSED, "Custom ESP32 Wroom + SX126x (Crystal)" }, // SX1268 @4m1g0, @lillefyr - { 0x3c, 21, 22, 16, 0, 25, 5, 18, UNUSED, 33, 32, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "TTGO LoRa 32 V2 Modified with module SX126x (crystal)" }, // SX1268 @TCRobotics - { 0x3c, 21, 22, 16, 0, 25, 5, 5, UNUSED, 2, 13, 26, 19, 23, 18, 1.6f, UNUSED, UNUSED, "Custom ESP32 Wroom + SX126x DRF1268T (TCX0) (5, 2, 26, 13)" }, // SX1268 @sdey76 - { 0x3c, 21, 22, 16, 0, 25, 5, 5, UNUSED, 26, 12, 14, 19, 23, 18, 1.6f, UNUSED, UNUSED, "Custom ESP32 Wroom + SX126x DRF1268T (TCX0) (5, 26, 14, 12)" }, // SX1268 @imants - { 0x3c, 21, 22, 16, 38, 22, 1, 18, 26, 33, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "433MHz T-BEAM V1.0 + OLED" }, // SX1278 @fafu - { 0x3c, 21, 22, 16, 0, 2, 5, 5, UNUSED, 34, 32, 14, 19, 27, 18, 1.6f, UNUSED, UNUSED, "433MHz FOSSA 1W Ground Station" }, // SX1268 @jgromes - { 0x3c, 21, 22, 16, 0, 2, 2, 5, UNUSED, 34, 32, 14, 19, 27, 18, 1.6f, UNUSED, UNUSED, "868-915MHz FOSSA 1W Ground Station" }, //SX1276 @jgromes - { 0x3c, 21, 22, 16, 0, 22, 8, 5, 26, 34, 32, 14, 19, 27, 18, 0.0f, UNUSED, UNUSED, "2.4GHz ESP32 + SX1280" }, //SX1280 @g4lile0 - { 0x3c, 21, 22, 16, 38, 22, 2, 18, 26, 33, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, "868-915MHzT-BEAM V1.0 + OLED" }, // SX1278 @fafu + //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, VBAT_AIN, VBAT_SCALE, BOARD + { 0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 12, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "433MHz HELTEC WiFi LoRA 32 V1" }, // SX1278 @4m1g0 + { 0x3c, 4, 15, 16, 0, 25, 2, 18, 26, 12, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "863-928MHz HELTEC WiFi LoRA 32 V1" }, // SX1276 + { 0x3c, 4, 15, 16, 0, 25, 1, 18, 26, 35, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "433MHz HELTEC WiFi LoRA 32 V2" }, // SX1278 @4m1g0 + { 0x3c, 4, 15, 16, 0, 25, 2, 18, 26, 35, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "863-928MHz HELTEC WiFi LoRA 32 V2" }, // SX1276 + { 0x3c, 4, 15, 16, 0, 2, 1, 18, 26, UNUSED, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "433Mhz TTGO LoRa 32 v1" }, // SX1278 @g4lile0 + { 0x3c, 4, 15, 16, 0, 2, 2, 18, 26, UNUSED, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "868-915MHz TTGO LoRa 32 v1" }, // SX1276 + { 0x3c, 21, 22, 16, 0, 22, 1, 18, 26, 33, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "433MHz TTGO LoRA 32 v2" }, // SX1278 @TCRobotics + { 0x3c, 21, 22, 16, 0, 22, 2, 18, 26, 33, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "868-915MHz TTGO LoRA 32 v2" }, // SX1276 + { 0x3c, 21, 22, 16, 39, 22, 1, 18, 26, 33, 32, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "433MHz T-BEAM + OLED" }, // SX1278 + { 0x3c, 21, 22, 16, 39, 22, 2, 18, 26, 33, 32, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "868-915MHz T-BEAM + OLED" }, // SX1276 + { 0x3c, 21, 22, 16, 0, 25, 5, 5, UNUSED, 27, 26, 14, 19, 23, 18, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "Custom ESP32 Wroom + SX126x (Crystal)" }, // SX1268 @4m1g0, @lillefyr + { 0x3c, 21, 22, 16, 0, 25, 5, 18, UNUSED, 33, 32, 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "TTGO LoRa 32 V2 Modified with module SX126x (crystal)" }, // SX1268 @TCRobotics + { 0x3c, 21, 22, 16, 0, 25, 5, 5, UNUSED, 2, 13, 26, 19, 23, 18, 1.6f, UNUSED, UNUSED, UNUSED, 0.0f, "Custom ESP32 Wroom + SX126x DRF1268T (TCX0) (5, 2, 26, 13)" }, // SX1268 @sdey76 + { 0x3c, 21, 22, 16, 0, 25, 5, 5, UNUSED, 26, 12, 14, 19, 23, 18, 1.6f, UNUSED, UNUSED, UNUSED, 0.0f, "Custom ESP32 Wroom + SX126x DRF1268T (TCX0) (5, 26, 14, 12)" }, // SX1268 @imants + { 0x3c, 21, 22, 16, 38, 22, 1, 18, 26, 33, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "433MHz T-BEAM V1.0 + OLED" }, // SX1278 @fafu + { 0x3c, 21, 22, 16, 0, 2, 5, 5, UNUSED, 34, 32, 14, 19, 27, 18, 1.6f, UNUSED, UNUSED, UNUSED, 0.0f, "433MHz FOSSA 1W Ground Station" }, // SX1268 @jgromes + { 0x3c, 21, 22, 16, 0, 2, 2, 5, UNUSED, 34, 32, 14, 19, 27, 18, 1.6f, UNUSED, UNUSED, UNUSED, 0.0f, "868-915MHz FOSSA 1W Ground Station" }, //SX1276 @jgromes + { 0x3c, 21, 22, 16, 0, 22, 8, 5, 26, 34, 32, 14, 19, 27, 18, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "2.4GHz ESP32 + SX1280" }, //SX1280 @g4lile0 + { 0x3c, 21, 22, 16, 38, 22, 2, 18, 26, 33, UNUSED , 14, 19, 27, 5, 0.0f, UNUSED, UNUSED, UNUSED, 0.0f, "868-915MHzT-BEAM V1.0 + OLED" }, // SX1278 @fafu }) { @@ -213,10 +213,10 @@ void ConfigManager::handleDashboard() if (WiFi.isConnected() ){ s += "WiFi RSSI " + String(WiFi.RSSI()) + ""; } - s += "Radio " + String(Radio::getInstance().isReady() ? "READY" : "NOT READY") + ""; - //s += "Uptime " + // process and update in js + ""; + s += "Battery " + String(status.vbat) + ""; s += F(""); + s += F("

Modem Configuration

"); s += ""; s += ""; @@ -394,6 +394,7 @@ void ConfigManager::handleRefreshWorldmap() data_string += String(WiFi.RSSI()) + ","; } data_string += String(Radio::getInstance().isReady() ? "READY" : "NOT READY") + ","; + data_string += String(status.vbat) + ","; // last packet received data (for lastpacket id table data) data_string += String(status.lastPacketInfo.time) + ","; @@ -775,6 +776,8 @@ bool ConfigManager::parseBoardTemplate(board_t &board) board.TX_EN = doc["TXEN"]; else board.TX_EN = UNUSED; + board.VBAT_AIN = doc["VBAT"] || UNUSED; + board.VBAT_SCALE = doc["VBATX"]; return true; } diff --git a/tinyGS/src/ConfigManager/ConfigManager.h b/tinyGS/src/ConfigManager/ConfigManager.h index d05cd7a5..25b43a88 100644 --- a/tinyGS/src/ConfigManager/ConfigManager.h +++ b/tinyGS/src/ConfigManager/ConfigManager.h @@ -110,6 +110,8 @@ typedef struct float L_TCXO_V; uint8_t RX_EN; uint8_t TX_EN; + uint8_t VBAT_AIN; /* GPIO pin for VBAT monitoring */ + float VBAT_SCALE; /* potential divider between battery and GPIO pin */ String BOARD; } board_t; diff --git a/tinyGS/src/ConfigManager/html.h b/tinyGS/src/ConfigManager/html.h index a68ab6db..3a6c1ce9 100644 --- a/tinyGS/src/ConfigManager/html.h +++ b/tinyGS/src/ConfigManager/html.h @@ -50,7 +50,7 @@ const char BOARD_NAMES[][BOARD_NAME_LENGTH] PROGMEM = constexpr auto BOARD_LENGTH = 3; const char BOARD_VALUES[][BOARD_LENGTH] PROGMEM = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18" }; -const char IOTWEBCONF_DASHBOARD_STYLE_INNER[] PROGMEM = "table{margin:20px auto;}h3{text-align:center;}.card{height:12em;margin:10px;text-align:left;font-family:Arial;border:3px groove;border-radius:0.3rem;display:inline-block;padding:10px;min-width:260px;}td{padding:0 10px;}textarea{resize:vertical;width:100%;margin:0;height:318px;padding:5px;overflow:auto;}#c1{width:98%;padding:5px;}#t1{width:98%}.console{display:inline-block;text-align:center;margin:10px 0;width:98%;max-width:1080px;}.G{color:green;}.R{color:red}"; +const char IOTWEBCONF_DASHBOARD_STYLE_INNER[] PROGMEM = "table{margin:20px auto;}h3{text-align:center;}.card{height:13em;margin:10px;text-align:left;font-family:Arial;border:3px groove;border-radius:0.3rem;display:inline-block;padding:10px;min-width:260px;}td{padding:0 10px;}textarea{resize:vertical;width:100%;margin:0;height:318px;padding:5px;overflow:auto;}#c1{width:98%;padding:5px;}#t1{width:98%}.console{display:inline-block;text-align:center;margin:10px 0;width:98%;max-width:1080px;}.G{color:green;}.R{color:red}"; const char IOTWEBCONF_DASHBOARD_BODY_INNER[] PROGMEM = "
\n"; const char IOTWEBCONF_CONSOLE_BODY_INNER[] PROGMEM = "

\n"; const char IOTWEBCONF_CONSOLE_SCRIPT[] PROGMEM = "var x=null,lt,to,tp,pc='';var sn=0,id=0;function f(p){var c,o='',t;clearTimeout(lt);t = document.getElementById('t1');if (p==1) {c =document.getElementById('c1');o='&c1='+encodeURIComponent(c.value);c.value='';t.scrollTop=99999;sn=t.scrollTop;}if (t.scrollTop >= sn){if (x!=null){x.abort();}x=new XMLHttpRequest();x.onreadystatechange=function() {if(x.readyState==4&&x.status==200){var z,d;var a=x.responseText;console.log(a);id=a.substr(0,a.indexOf('\\n'));z=a.substr(a.indexOf('\\n')+1);if(z.length>0){t.value+=z;}t.scrollTop=99999;sn=t.scrollTop;}};x.open('GET','cs?c2='+id+o,true);x.send();}lt=setTimeout(f,2345);return false;}window.addEventListener('load', f);"; @@ -61,6 +61,6 @@ const char ADVANCED_CONFIG_SCRIPT[] PROGMEM = "function tableDoneHandler(btn){var tbd=document.getElementById('current-table'); var ds=tableDictString(tbd); current_ctrl.value=ds; document.getElementById('dt-' + current_id).remove(); current_ctrl=null; current_id=null; }" "function editElementDict(ed){if (current_ctrl===null){var ph=ed.getAttribute('placeholder'); var dstring = ed.value!='' ? ed.value : ph; if(dstring !== ''){ current_id=ed.id; var dict = JSON.parse(dstring); var tblhtml = '
' + dictTable(dict) + '
'; ed.insertAdjacentHTML('afterend', tblhtml); current_ctrl=ed; } } }" "var current_id, current_ctrl=null; window.addEventListener('load', function() {setup_click('board_template'); setup_click('modem_startup');});"; -const char IOTWEBCONF_WORLDMAP_SCRIPT[] PROGMEM = "var wmx=null,wmt;function wmf(p){var sp,mc,gs,lp;clearTimeout(wmt);wmx=new XMLHttpRequest();wmx.onreadystatechange=function() {if(wmx.readyState==4&&x.status==200){var wma=wmx.responseText;var wmp = wma.split(',');sp=document.getElementById('wmsatpos');sp.setAttribute('cx', wmp[0]);sp.setAttribute('cy', wmp[1]);mc=document.getElementById('modemconfig');for(let r=0;r<6;r++){mc.rows[r].cells[1].innerHTML=wmp[r+2]};if(wmp[3]=='LoRa'){mc.rows[3].cells[0].innerHTML='Spreading Factor ';mc.rows[4].cells[0].innerHTML='Coding Rate ';}else{mc.rows[3].cells[0].innerHTML='Bitrate ';mc.rows[4].cells[0].innerHTML='Frequency dev ';};gs=document.getElementById('gsstatus');for(let r=0;r<6;r++){gs.rows[r].cells[1].innerHTML=wmp[r+8];};lp=document.getElementById('lastpacket');for(let r=0;r<4;r++){lp.rows[r].cells[1].innerHTML=wmp[r+14];};lp.rows[4].cells[0].innerHTML=wmp[18];}};wmx.open('GET','wm',true);wmx.send();wmt=setTimeout(wmf,5000);return false;}window.addEventListener('load', wmf);"; +const char IOTWEBCONF_WORLDMAP_SCRIPT[] PROGMEM = "var wmx=null,wmt;function wmf(p){var sp,mc,gs,lp;clearTimeout(wmt);wmx=new XMLHttpRequest();wmx.onreadystatechange=function() {if(wmx.readyState==4&&x.status==200){var wma=wmx.responseText;var wmp = wma.split(',');sp=document.getElementById('wmsatpos');sp.setAttribute('cx', wmp[0]);sp.setAttribute('cy', wmp[1]);mc=document.getElementById('modemconfig');for(let r=0;r<6;r++){mc.rows[r].cells[1].innerHTML=wmp[r+2]};if(wmp[3]=='LoRa'){mc.rows[3].cells[0].innerHTML='Spreading Factor ';mc.rows[4].cells[0].innerHTML='Coding Rate ';}else{mc.rows[3].cells[0].innerHTML='Bitrate ';mc.rows[4].cells[0].innerHTML='Frequency dev ';};gs=document.getElementById('gsstatus');for(let r=0;r<7;r++){gs.rows[r].cells[1].innerHTML=wmp[r+8];};lp=document.getElementById('lastpacket');for(let r=0;r<4;r++){lp.rows[r].cells[1].innerHTML=wmp[r+15];};lp.rows[4].cells[0].innerHTML=wmp[19];}};wmx.open('GET','wm',true);wmx.send();wmt=setTimeout(wmf,5000);return false;}window.addEventListener('load', wmf);"; const char IOTWEBCONF_CONFIG_STYLE_INNER[] PROGMEM = " fieldset[id='Board config'] div:nth-of-type(3) ~ div { display:none}"; diff --git a/tinyGS/src/Display/Display.cpp b/tinyGS/src/Display/Display.cpp index 80be7513..768b5cf5 100644 --- a/tinyGS/src/Display/Display.cpp +++ b/tinyGS/src/Display/Display.cpp @@ -220,6 +220,11 @@ void drawFrame3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int1 display->drawString(128 + x, 23 + y, String(status.modeminfo.freqDev)+ "/" + String(status.modeminfo.bw)+ "kHz"); display->drawString(128 + x, 34 + y, String(status.modeminfo.bitrate)+ "kbps"); } + if (status.vbat != 0.0) + { + display->setTextAlignment(TEXT_ALIGN_LEFT); + display->drawString(x, 45 + y, "Bat: " + String(status.vbat) + "V"); + } } void drawFrame4(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) diff --git a/tinyGS/src/Status.h b/tinyGS/src/Status.h index 023dc061..ef109f9a 100644 --- a/tinyGS/src/Status.h +++ b/tinyGS/src/Status.h @@ -71,6 +71,7 @@ struct Status { bool mqtt_connected = false; bool radio_ready = false; int16_t radio_error = 0; + float vbat = 0.0; PacketInfo lastPacketInfo; ModemInfo modeminfo; float satPos[2] = {0, 0}; diff --git a/tinyGS/tinyGS.ino b/tinyGS/tinyGS.ino index c19f1371..4c6f9e04 100644 --- a/tinyGS/tinyGS.ino +++ b/tinyGS/tinyGS.ino @@ -101,6 +101,7 @@ Status status; void printControls(); void switchTestmode(); void checkButton(); +void checkBattery(void); void setupNTP(); void configured() @@ -204,6 +205,8 @@ void loop() { return; } + checkBattery(); + // connected mqtt.loop(); @@ -249,6 +252,22 @@ void checkButton() } } +void checkBattery(void) +{ + #define BATTERY_INTERVAL 1000 + static unsigned long lastReadTime = 0; + board_t board; + + if (millis() - lastReadTime > BATTERY_INTERVAL) { + lastReadTime = millis(); + if (configManager.getBoardConfig(board)) { + if (board.VBAT_AIN != UNUSED) { + status.vbat = (float)analogReadMilliVolts(board.VBAT_AIN) * board.VBAT_SCALE * 0.001f; + } + } + } +} + void handleSerial() { if(Serial.available()) From 23cf996ba5c035716da5d59e626a7353881c7b43 Mon Sep 17 00:00:00 2001 From: Matthew Kendall Date: Tue, 9 Aug 2022 12:19:17 -0700 Subject: [PATCH 2/4] Fix problem parsing board template --- tinyGS/src/ConfigManager/ConfigManager.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tinyGS/src/ConfigManager/ConfigManager.cpp b/tinyGS/src/ConfigManager/ConfigManager.cpp index 1e691fff..4de802d1 100644 --- a/tinyGS/src/ConfigManager/ConfigManager.cpp +++ b/tinyGS/src/ConfigManager/ConfigManager.cpp @@ -776,7 +776,10 @@ bool ConfigManager::parseBoardTemplate(board_t &board) board.TX_EN = doc["TXEN"]; else board.TX_EN = UNUSED; - board.VBAT_AIN = doc["VBAT"] || UNUSED; + if (doc.containsKey("VBAT")) + board.VBAT_AIN = doc["VBAT"]; + else + board.VBAT_AIN = UNUSED; board.VBAT_SCALE = doc["VBATX"]; return true; From 5d8685477c3b2fae7b81cf590864833f7bc5d6c8 Mon Sep 17 00:00:00 2001 From: Matthew Kendall Date: Tue, 9 Aug 2022 15:48:20 -0700 Subject: [PATCH 3/4] Add averaging to battery measurement to reduce noise. --- tinyGS/tinyGS.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tinyGS/tinyGS.ino b/tinyGS/tinyGS.ino index 4c6f9e04..fae2963d 100644 --- a/tinyGS/tinyGS.ino +++ b/tinyGS/tinyGS.ino @@ -254,7 +254,7 @@ void checkButton() void checkBattery(void) { - #define BATTERY_INTERVAL 1000 + #define BATTERY_INTERVAL 250 static unsigned long lastReadTime = 0; board_t board; @@ -262,7 +262,8 @@ void checkBattery(void) lastReadTime = millis(); if (configManager.getBoardConfig(board)) { if (board.VBAT_AIN != UNUSED) { - status.vbat = (float)analogReadMilliVolts(board.VBAT_AIN) * board.VBAT_SCALE * 0.001f; + float vbatMeas = (float)analogReadMilliVolts(board.VBAT_AIN) * board.VBAT_SCALE * 0.001f; + status.vbat = (0.75 * status.vbat) + (0.25 * vbatMeas); } } } From 69039cd82adc82e3ff3126aa759719749fbb59da Mon Sep 17 00:00:00 2001 From: Matthew Kendall Date: Tue, 9 Aug 2022 15:50:19 -0700 Subject: [PATCH 4/4] Consolidate MQTT battery reporting with new measurement. --- tinyGS/src/Mqtt/MQTT_Client.cpp | 38 ++------------------------------- tinyGS/src/Mqtt/MQTT_Client.h | 3 --- 2 files changed, 2 insertions(+), 39 deletions(-) diff --git a/tinyGS/src/Mqtt/MQTT_Client.cpp b/tinyGS/src/Mqtt/MQTT_Client.cpp index 205155c4..6add90ab 100644 --- a/tinyGS/src/Mqtt/MQTT_Client.cpp +++ b/tinyGS/src/Mqtt/MQTT_Client.cpp @@ -94,7 +94,7 @@ void MQTT_Client::loop() else { StaticJsonDocument<128> doc; - doc["Vbat"] = voltage(); + doc["Vbat"] = status.vbat; doc["Mem"] = ESP.getFreeHeap(); doc["RSSI"] =WiFi.RSSI(); doc["radio"]= status.radio_error; @@ -203,7 +203,7 @@ void MQTT_Client::sendWelcome() doc["board"] = configManager.getBoard(); doc["mac"] = clientId; doc["seconds"] = millis()/1000; - doc["Vbat"] = voltage(); + doc["Vbat"] = status.vbat; char buffer[1048]; serializeJson(doc, buffer); @@ -864,37 +864,3 @@ void MQTT_Client::begin() setServer(configManager.getMqttServer(), configManager.getMqttPort()); setCallback(manageMQTTDataCallback); } - - - -int MQTT_Client::voltage() { - int medianVoltage; - int length = 21; - int voltages[22]; - - for (int i = 0; i < 22; i++) - { - voltages[i] = analogRead(36); - } - - // BubbleSortAsc from https://www.luisllamas.es/arduino-bubble-sort/ - int i, j, flag = 1; - int temp; - for (i = 1; (i <= length) && flag; i++) - { - flag = 0; - for (j = 0; j < (length - 1); j++) - { - if (voltages[j + 1] < voltages[j]) - { - temp = voltages[j]; - voltages[j] = voltages[j + 1]; - voltages[j + 1] = temp; - flag = 1; - } - } - } - medianVoltage = voltages[10]; - return medianVoltage; -} - diff --git a/tinyGS/src/Mqtt/MQTT_Client.h b/tinyGS/src/Mqtt/MQTT_Client.h index 33cf10e5..f9b2f2d4 100644 --- a/tinyGS/src/Mqtt/MQTT_Client.h +++ b/tinyGS/src/Mqtt/MQTT_Client.h @@ -72,9 +72,6 @@ class MQTT_Client : public PubSubClient { void remoteGoToSleep(char* payload, size_t payload_len); void remoteGoToSiesta(char* payload, size_t payload_len); - - int voltage(); - bool usingNewCert = true; unsigned long lastPing = 0; unsigned long lastConnectionAtempt = 0;
Listening to " + String(status.modeminfo.satellite) + "
Modulation " + String(status.modeminfo.modem_mode) + "