Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds SoC Temperature + Batt Voltage + BootTime #248

Open
wants to merge 1 commit into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tinyGS/src/ConfigManager/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,17 @@ void ConfigManager::parseAdvancedConf()
{
advancedConf.lowPower = doc["lowPower"];
}

if (doc.containsKey(F("battery")))
{
JsonObject batteryConfig = doc["battery"];
if (batteryConfig.containsKey(F("pin")) & batteryConfig.containsKey(F("scale")))
{
advancedConf.battery = true;
advancedConf.battPin = batteryConfig["pin"];
advancedConf.battScale = batteryConfig["scale"];
}
}
}

void ConfigManager::parseModemStartup()
Expand Down
6 changes: 6 additions & 0 deletions tinyGS/src/ConfigManager/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ typedef struct
bool flipOled = true;
bool dnOled = true;
bool lowPower = false;
bool battery = false;
int battPin = 0;
float battScale = 0.0;
} AdvancedConfig;

class ConfigManager : public IotWebConf2
Expand Down Expand Up @@ -190,6 +193,9 @@ class ConfigManager : public IotWebConf2
bool getFlipOled() { return advancedConf.flipOled; }
bool getDayNightOled() { return advancedConf.dnOled; }
bool getLowPower() { return advancedConf.lowPower; }
bool getbattery() { return advancedConf.battery; }
int getbattPin() { return advancedConf.battPin; }
float getbattScale() { return advancedConf.battScale; }
bool getBoardConfig(board_t &board)
{
bool ret = true;
Expand Down
5 changes: 4 additions & 1 deletion tinyGS/src/Status.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ struct TextFrame {
};

struct Status {
const uint32_t version = 2403241; // version year month day release
const uint32_t version = 2406101; // version year month day release
const char* git_version = GIT_VERSION;
float ptemp = -1000.0; //processor_temp
float vbat = -1000.0; //battery_voltage
time_t bootTime;
bool mqtt_connected = false;
bool radio_ready = false;
int16_t radio_error = 0;
Expand Down
62 changes: 62 additions & 0 deletions tinyGS/tinyGS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ void loop() {
}

// connected
getProcessorTemperature();
checkBattery();
status.bootTime = time (NULL) - millis()/1000;

mqtt.loop();
OTA::loop();
Expand Down Expand Up @@ -319,4 +322,63 @@ void printControls()
Log::console(PSTR("!b - reboot the board"));
Log::console(PSTR("!p - send test packet to nearby stations (to check transmission)"));
Log::console(PSTR("------------------------------------"));
}

#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
#include "driver/temp_sensor.h"
#else
#ifdef __cplusplus
extern "C" {
uint8_t temprature_sens_read();
}
#endif
#endif

void getProcessorTemperature() {
// Implementation for getting the processor temperature
// Checks temp every 30 sec
// Different method for each ESP SoC.
#define TEMP_INTERVAL 30000
static unsigned long lastTempTime = 0;

float temperature = -1000.0;
if (millis() - lastTempTime > TEMP_INTERVAL) {
lastTempTime = millis();
#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
//float temperature
temp_sensor_config_t temp_sensor = TSENS_CONFIG_DEFAULT();
temp_sensor_set_config(temp_sensor);
temp_sensor_start();
esp_err_t temp_reading = temp_sensor_read_celsius(&temperature);
temp_sensor_stop();
if (temp_reading != ESP_OK) {
temperature = -1000.0; // Placeholder value for invalid reading
}
#else
uint8_t temp_raw = temprature_sens_read();
if (temp_raw != 128){
temperature = (temp_raw - 32) / 1.8f;
}
#endif
}
status.ptemp = temperature!=-1000.0 ? temperature : status.ptemp;
}

void checkBattery()
{// based on mdkendall https://github.com/G4lile0/tinyGS/pull/177/files
#define BATTERY_INTERVAL 30000
static unsigned long lastReadBattTime = 0;
float chng_threshold = 0.15; // threshold for 15% voltage change

if (millis() - lastReadBattTime > BATTERY_INTERVAL) {
lastReadBattTime = millis();
if (configManager.getbattery()) {
float vbatMeas = (float)analogReadMilliVolts(configManager.getbattPin()) * configManager.getbattScale() * 0.001f;
if (abs(status.vbat - vbatMeas) > chng_threshold * status.vbat) {
status.vbat = vbatMeas;
} else {
status.vbat = (0.75 * status.vbat) + (0.25 * vbatMeas);
}
}
}
}