From c16d431c8930378dc0e5587321a3976f0f6aa9bb Mon Sep 17 00:00:00 2001 From: klaute Date: Wed, 11 Jul 2018 11:12:53 +0200 Subject: [PATCH] Added the battery voltage code. --- firmware/config.h | 1 + firmware/firmware.ino | 7 +++++-- firmware/influxdb.ino | 6 ++---- firmware/sensors.ino | 8 ++++++++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index a5db2e4..58ad161 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -6,6 +6,7 @@ #define SENSOR_LIGHT 2 #define SENSOR_WINDSPEED 3 #define SENSOR_PRESSURE 4 +#define SENSOR_BAT_VOLTAGE 5 #define UPDATE_SENSOR_INTERVAL_S 10 #define UPDATE_WEBSERVER_INTVERVAL_S 1 diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 080f30a..c602082 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -33,7 +33,7 @@ //*************************************************************************// -float currentSensorData[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; +float currentSensorData[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; uint16_t update_sensor_cnt = 0; uint16_t update_webserver_cnt = 0; @@ -56,6 +56,7 @@ void setup() { // Pin settings pinMode(STATUS_LED_PIN, OUTPUT); pinMode(ANEMOMETER_PIN, INPUT_PULLUP); + pinMode(A0, INPUT); // Establish WiFi connection String wifiName = "oko-weather-" + String(ESP.getChipId()); @@ -113,6 +114,7 @@ void loop() { currentSensorData[SENSOR_LIGHT] = fetchLight(); currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed(); currentSensorData[SENSOR_PRESSURE] = fetchPressure(); + currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage(); #ifdef DEBUG Serial.println(""); @@ -122,6 +124,7 @@ void loop() { Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen"); Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h"); Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa"); + Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V"); #endif pushToInfluxDB(DEVICE_NAME, currentSensorData); @@ -136,4 +139,4 @@ void loop() { } //*************************************************************************// - + diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index 0c70db0..87b0d75 100755 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -5,10 +5,8 @@ void pushToInfluxDB(String device, float sensorValues[]) { influxdb.write("weather,device=" + device + " humidity=" + String(sensorValues[SENSOR_HUMIDITY])); influxdb.write("weather,device=" + device + " light=" + String(sensorValues[SENSOR_LIGHT])); influxdb.write("weather,device=" + device + " windspeed=" + String(sensorValues[SENSOR_WINDSPEED])); - - int tmp = influxdb.write("weather,device=" + device + " pressure=" + String(sensorValues[SENSOR_PRESSURE])); - - Serial.println("Opendb status: " + String(tmp)); + influxdb.write("weather,device=" + device + " pressure=" + String(sensorValues[SENSOR_PRESSURE])); + influxdb.write("weather,device=" + device + " batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE])); } diff --git a/firmware/sensors.ino b/firmware/sensors.ino index 770a19e..6838864 100755 --- a/firmware/sensors.ino +++ b/firmware/sensors.ino @@ -45,4 +45,12 @@ float fetchWindspeed() { return (float)anemometerRotations / 5.0 * 2.4; } + +// Copied from https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/ +float getBatteryVoltage() { + uint16_t raw = analogRead(A0); + float volt = raw / 1023.0; + volt = volt * 4.2; + return volt; +}