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

add support for custom VCC monitoring #1048

Open
wants to merge 2 commits into
base: dev
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 code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,17 @@
#define ADC_MODE_VALUE ADC_VCC
#endif

// custom Vcc mode (ADC_TOUT mode with correction)
#define ADC_VCC_CUSTOM 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not make this a configuration parameter, guarded by ifndefs?


#ifndef ADC_VCC_CUSTOM_MUL
#define ADC_VCC_CUSTOM_MUL (1.0)
#endif

#ifndef ADC_VCC_CUSTOM_ADD
#define ADC_VCC_CUSTOM_ADD (0.0)
#endif

// -----------------------------------------------------------------------------
// I2C
// -----------------------------------------------------------------------------
Expand Down
29 changes: 19 additions & 10 deletions code/espurna/utils.ino
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,8 @@ void heartbeat() {
if (serial) {
DEBUG_MSG_P(PSTR("[MAIN] Uptime: %lu seconds\n"), uptime_seconds);
DEBUG_MSG_P(PSTR("[MAIN] Free heap: %lu bytes\n"), free_heap);
#if ADC_MODE_VALUE == ADC_VCC
DEBUG_MSG_P(PSTR("[MAIN] Power: %lu mV\n"), ESP.getVcc());
#endif
int Vcc = custom_getVcc(ADC_MODE_VALUE);
DEBUG_MSG_P(PSTR("[MAIN] Power: %s \n"), (Vcc==(-1) ? "Unknown" : String(Vcc).c_str()));
#if NTP_SUPPORT
if (ntpSynced()) DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
#endif
Expand Down Expand Up @@ -191,9 +190,8 @@ void heartbeat() {
lightMQTT();
#endif
#if (HEARTBEAT_REPORT_VCC)
#if ADC_MODE_VALUE == ADC_VCC
mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
#endif
int Vcc = custom_getVcc(ADC_MODE_VALUE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if would be great to introduce this feature with conditions, like:

#if ADC_VCC_CUSTOM
int Vcc = custom_getVcc()
#else
int Vcc = ESP.getVcc()... 
#endif

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see that it's actually done in custom_getVcc()

mqttSend(MQTT_TOPIC_VCC, (Vcc==(-1) ? "Unknown" : String(Vcc).c_str()));
#endif
#if (HEARTBEAT_REPORT_STATUS)
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
Expand Down Expand Up @@ -332,9 +330,8 @@ void info() {

DEBUG_MSG_P(PSTR("[INIT] Settings size: %u bytes\n"), settingsSize());
DEBUG_MSG_P(PSTR("[INIT] Free heap: %u bytes\n"), getFreeHeap());
#if ADC_MODE_VALUE == ADC_VCC
DEBUG_MSG_P(PSTR("[INIT] Power: %u mV\n"), ESP.getVcc());
#endif
int Vcc = custom_getVcc(ADC_MODE_VALUE);
DEBUG_MSG_P(PSTR("[MAIN] Power: %s \n"), (Vcc==(-1) ? "Unknown" : String(Vcc).c_str()));

DEBUG_MSG_P(PSTR("[INIT] Power saving delay value: %lu ms\n"), systemLoopDelay());

Expand Down Expand Up @@ -437,10 +434,22 @@ void nice_delay(unsigned long ms) {
}

// This method is called by the SDK to know where to connect the ADC
// poulch74: add custom Vcc (see #define ADC_VCC_CUSTOM)
int __get_adc_mode() {
return (int) (ADC_MODE_VALUE);
return (int) ((ADC_MODE_VALUE==ADC_VCC_CUSTOM) ? ADC_TOUT:ADC_MODE_VALUE);
}

int custom_getVcc(int mode) {
if(mode == ADC_VCC_CUSTOM) {
double vcc = analogRead(0)*ADC_VCC_CUSTOM_MUL+ADC_VCC_CUSTOM_ADD;
if(vcc < 0) vcc = 0;
return (int)vcc;
}
if(mode == ADC_VCC) return ESP.getVcc();
return -1;// decode for string as Unknown
}


bool isNumber(const char * s) {
unsigned char len = strlen(s);
bool decimal = false;
Expand Down
4 changes: 2 additions & 2 deletions code/espurna/ws.ino
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ void _wsUpdate(JsonObject& root) {
root["uptime"] = getUptime();
root["rssi"] = WiFi.RSSI();
root["loadaverage"] = systemLoadAverage();
#if ADC_MODE_VALUE == ADC_VCC
root["vcc"] = ESP.getVcc();
#if ADC_MODE_VALUE == ADC_VCC || ADC_MODE_VALUE == ADC_VCC_CUSTOM
root["vcc"] = custom_getVcc(ADC_MODE_VALUE);
#endif
#if NTP_SUPPORT
if (ntpSynced()) root["now"] = now();
Expand Down