diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 4df614e4bc..bdd5b92022 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -646,6 +646,17 @@ #define ADC_MODE_VALUE ADC_VCC #endif +// custom Vcc mode (ADC_TOUT mode with correction) +#define ADC_VCC_CUSTOM 1 + +#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 // ----------------------------------------------------------------------------- diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index 2cc34d08ae..e06d2c8d55 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -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 @@ -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); + mqttSend(MQTT_TOPIC_VCC, (Vcc==(-1) ? "Unknown" : String(Vcc).c_str())); #endif #if (HEARTBEAT_REPORT_STATUS) mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); @@ -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()); @@ -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; diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index aab9be2ef2..9e51a45bcd 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -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();