From e8cc27dc504038bd7e18630aa424fadeced48f1d Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Tue, 13 Aug 2019 18:40:53 +0200 Subject: [PATCH] calculate voltage --- src/main.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f11ae74..a535a76 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,14 +84,14 @@ struct __attribute__((packed)) sensor_readings_t { uint32_t voc = 0; // Ohm float pm10 = NAN; // µg/m³ float pm25 = NAN; // µg/m³ - float lux = NAN; + float lux = NAN; // lx float uvi = NAN; float uva = NAN; float uvb = NAN; float temperature_max = NAN; // °C float temperature_min = NAN; // °C - int battery = 0; - int8_t rssi = 0; + float voltage = NAN; // V + int8_t rssi = 0; // dBm time_t lastUpdate = 0; } sensor_readings; @@ -371,11 +371,12 @@ void getSensorMeasurements() { } } - sensor_readings.battery = analogRead(_VBAT); + int battery = analogRead(_VBAT); + sensor_readings.voltage = (battery/4096.0)*2*3.42; sensor_readings.rssi = WiFi.RSSI(); ESP_LOGI(TAG, "RSSI : %5d dBm", sensor_readings.rssi); - ESP_LOGI(TAG, "Battery : %5d ", sensor_readings.battery); + ESP_LOGI(TAG, "Battery : %5d ", battery); ESP_LOGI(TAG, "Heap : %5lu", ESP.getFreeHeap()); sensor_readings.lastUpdate = getTimestamp(); @@ -417,7 +418,7 @@ void receiveMqtt(const char* topic, const char* data, int data_len) { if (jsonDoc.containsKey("uvb")) sensor->uvb = jsonDoc["uvb"].as(); if (jsonDoc.containsKey("pm10")) sensor->pm10 = jsonDoc["pm10"].as(); if (jsonDoc.containsKey("pm2.5")) sensor->pm25 = jsonDoc["pm2.5"].as(); - if (jsonDoc.containsKey("voltage")) sensor->battery = jsonDoc["voltage"].as(); + if (jsonDoc.containsKey("voltage")) sensor->voltage = jsonDoc["voltage"].as(); if (jsonDoc.containsKey("rssi")) sensor->rssi = jsonDoc["rssi"].as(); if (jsonDoc.containsKey("timestamp")) sensor->lastUpdate = jsonDoc["timestamp"].as(); ESP_LOGI(TAG, "got new values from %s, timestamp: %lu", topic, sensor->lastUpdate); @@ -670,7 +671,7 @@ void sendValues() { jsonDoc["pm10"] = sensor_readings.pm10; jsonDoc["pm2.5"] = sensor_readings.pm25; } - jsonDoc["voltage"] = sensor_readings.battery; + jsonDoc["voltage"] = sensor_readings.voltage; jsonDoc["rssi"] = sensor_readings.rssi; jsonDoc["timestamp"] = sensor_readings.lastUpdate; serializeJson(jsonDoc, buf, JSON_BUF_LEN);