Only show temp if sensor is attached. Added common functions to get temp and vcc

This commit is contained in:
Gunnar Skjold
2020-02-14 21:48:03 +01:00
parent f484f3eb0e
commit eff00f1fe0
7 changed files with 118 additions and 49 deletions

33
src/HwTools.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "HwTools.h"
double HwTools::getVcc() {
#if defined(ARDUINO_ESP8266_WEMOS_D1MINI)
return (((double) ESP.getVcc()) / 1000) * 1.1; // This board has a voltage divider on VCC, add 10%
#elif defined(ESP8266)
return ((double) ESP.getVcc()) / 1000;
#endif
return -1;
}
double HwTools::getTemperature() {
#if defined TEMP_SENSOR_PIN
if(!tempSensorInit) {
tempSensor->begin();
delay(25);
tempSensor->requestTemperatures();
hasTempSensor = tempSensor->getTempCByIndex(0) != DEVICE_DISCONNECTED_C;
tempSensorInit = true;
}
if(hasTempSensor) {
tempSensor->requestTemperatures();
return tempSensor->getTempCByIndex(0);
} else {
return DEVICE_DISCONNECTED_C;
}
#endif
return DEVICE_DISCONNECTED_C;
}