Battery sensor for TTGO
This commit is contained in:
parent
e4bbb1d11c
commit
5a0ffebfaa
|
@ -277,3 +277,14 @@ build_flags =
|
||||||
-D FIRMWARE='"macchina-a0"'
|
-D FIRMWARE='"macchina-a0"'
|
||||||
-D SENSORS
|
-D SENSORS
|
||||||
${esp32.build_flags}
|
${esp32.build_flags}
|
||||||
|
|
||||||
|
[env:ttgo-tcell]
|
||||||
|
extends = esp32
|
||||||
|
lib_deps =
|
||||||
|
${esp32.lib_deps}
|
||||||
|
${sensors.lib_deps}
|
||||||
|
build_flags =
|
||||||
|
-D TTGO_TCELL
|
||||||
|
-D FIRMWARE='"ttgo-tcell"'
|
||||||
|
-D SENSORS
|
||||||
|
${esp32.build_flags}
|
||||||
|
|
|
@ -8,9 +8,22 @@
|
||||||
|
|
||||||
namespace Battery {
|
namespace Battery {
|
||||||
#ifdef MACCHINA_A0
|
#ifdef MACCHINA_A0
|
||||||
|
const int voltageDividerResistor1 = 2200;
|
||||||
|
const int voltageDividerResistor2 = 10000;
|
||||||
|
const int chargingVoltage = 13200;
|
||||||
|
const int deadVoltage = 11883;
|
||||||
|
#define BATTERY_SENSOR
|
||||||
|
#elif defined(TTGO_TCELL)
|
||||||
|
const int voltageDividerResistor1 = 100000;
|
||||||
|
const int voltageDividerResistor2 = 100000;
|
||||||
|
const int chargingVoltage = 4150;
|
||||||
|
const int deadVoltage = 3200;
|
||||||
|
#define BATTERY_SENSOR
|
||||||
|
#endif
|
||||||
|
#ifdef BATTERY_SENSOR
|
||||||
int smoothMilliVolts;
|
int smoothMilliVolts;
|
||||||
int a0_read_batt_mv() {
|
int a0_read_batt_mv() {
|
||||||
int mv = round(((float)analogRead(GPIO_NUM_35) + 35) / 0.215);
|
int mv = analogReadMilliVolts(GPIO_NUM_35) * (voltageDividerResistor1 + voltageDividerResistor2) / voltageDividerResistor1;
|
||||||
if (smoothMilliVolts)
|
if (smoothMilliVolts)
|
||||||
smoothMilliVolts = round(0.1 * (mv - smoothMilliVolts) + smoothMilliVolts);
|
smoothMilliVolts = round(0.1 * (mv - smoothMilliVolts) + smoothMilliVolts);
|
||||||
else
|
else
|
||||||
|
@ -20,26 +33,32 @@ int a0_read_batt_mv() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void Setup() {
|
void Setup() {
|
||||||
#if MACCHINA_A0
|
#ifdef BATTERY_SENSOR
|
||||||
pinMode(GPIO_NUM_35, INPUT);
|
pinMode(GPIO_NUM_35, INPUT);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SendDiscovery() {
|
bool SendDiscovery() {
|
||||||
#ifdef MACCHINA_A0
|
#ifdef BATTERY_SENSOR
|
||||||
return sendTeleSensorDiscovery("Battery", EC_NONE, "{{ value_json.batt }}", "battery", "%") && sendTeleBinarySensorDiscovery("Charging", EC_NONE, "{{ value_json.charging }}", "battery_charging");
|
return sendTeleSensorDiscovery("Battery", EC_NONE, "{{ value_json.batt }}", "battery", "%") &&
|
||||||
|
sendTeleBinarySensorDiscovery("Charging", EC_NONE, "{{ value_json.charging }}", "battery_charging") &&
|
||||||
|
sendTeleSensorDiscovery("Battery Voltage", EC_NONE, "{{ value_json.mV }}", "voltage", "mV");
|
||||||
#else
|
#else
|
||||||
return true;
|
return true;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendTelemetry() {
|
void SendTelemetry() {
|
||||||
#ifdef MACCHINA_A0
|
#ifdef BATTERY_SENSOR
|
||||||
auto mv = a0_read_batt_mv();
|
auto mv = a0_read_batt_mv();
|
||||||
doc["mV"] = mv;
|
doc["mV"] = mv;
|
||||||
bool charging = (mv > 13200);
|
bool charging = (mv > chargingVoltage);
|
||||||
bool dead = (mv < 11883);
|
bool dead = (mv < deadVoltage);
|
||||||
|
#ifdef MACCHINA_A0
|
||||||
unsigned int soc = round(-13275.04 + 2.049731 * mv - (0.00007847975 * mv) * mv);
|
unsigned int soc = round(-13275.04 + 2.049731 * mv - (0.00007847975 * mv) * mv);
|
||||||
|
#else
|
||||||
|
unsigned int soc = round(mv < 3700 ? (-64 + 0.02 * mv) : -4266.02 + 2.021295 * mv - (0.00023382077 * mv) * mv);
|
||||||
|
#endif
|
||||||
doc["batt"] = dead ? 0 : (charging ? (unsigned int)100 : max((unsigned int)0, min((unsigned int)100, soc)));
|
doc["batt"] = dead ? 0 : (charging ? (unsigned int)100 : max((unsigned int)0, min((unsigned int)100, soc)));
|
||||||
doc["charging"] = charging ? "ON" : "OFF";
|
doc["charging"] = charging ? "ON" : "OFF";
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue