Browse Source

Added the battery voltage code.

klaute 2 years ago
parent
commit
c16d431c89
4 changed files with 16 additions and 6 deletions
  1. 1
    0
      firmware/config.h
  2. 5
    2
      firmware/firmware.ino
  3. 2
    4
      firmware/influxdb.ino
  4. 8
    0
      firmware/sensors.ino

+ 1
- 0
firmware/config.h View File

@@ -6,6 +6,7 @@
6 6
 #define SENSOR_LIGHT          2
7 7
 #define SENSOR_WINDSPEED      3
8 8
 #define SENSOR_PRESSURE       4
9
+#define SENSOR_BAT_VOLTAGE    5
9 10
 
10 11
 #define UPDATE_SENSOR_INTERVAL_S     10
11 12
 #define UPDATE_WEBSERVER_INTVERVAL_S  1

+ 5
- 2
firmware/firmware.ino View File

@@ -33,7 +33,7 @@
33 33
 
34 34
 //*************************************************************************//
35 35
 
36
-float currentSensorData[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
36
+float currentSensorData[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
37 37
 
38 38
 uint16_t update_sensor_cnt    = 0;
39 39
 uint16_t update_webserver_cnt = 0;
@@ -56,6 +56,7 @@ void setup() {
56 56
   // Pin settings
57 57
   pinMode(STATUS_LED_PIN, OUTPUT);
58 58
   pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
59
+  pinMode(A0, INPUT);
59 60
 
60 61
   // Establish WiFi connection
61 62
   String wifiName = "oko-weather-" + String(ESP.getChipId());
@@ -113,6 +114,7 @@ void loop() {
113 114
     currentSensorData[SENSOR_LIGHT]       = fetchLight();
114 115
     currentSensorData[SENSOR_WINDSPEED]   = fetchWindspeed();
115 116
     currentSensorData[SENSOR_PRESSURE]    = fetchPressure();
117
+    currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
116 118
   
117 119
 #ifdef DEBUG
118 120
     Serial.println("");
@@ -122,6 +124,7 @@ void loop() {
122 124
     Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen");
123 125
     Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
124 126
     Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
127
+    Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
125 128
 #endif
126 129
 
127 130
     pushToInfluxDB(DEVICE_NAME, currentSensorData);
@@ -136,4 +139,4 @@ void loop() {
136 139
 }
137 140
 
138 141
 //*************************************************************************//
139
-
142
+

+ 2
- 4
firmware/influxdb.ino View File

@@ -5,10 +5,8 @@ void pushToInfluxDB(String device, float sensorValues[]) {
5 5
   influxdb.write("weather,device=" + device + " humidity=" + String(sensorValues[SENSOR_HUMIDITY]));
6 6
   influxdb.write("weather,device=" + device + " light=" + String(sensorValues[SENSOR_LIGHT]));
7 7
   influxdb.write("weather,device=" + device + " windspeed=" + String(sensorValues[SENSOR_WINDSPEED]));
8
-
9
-  int tmp = influxdb.write("weather,device=" + device + " pressure=" + String(sensorValues[SENSOR_PRESSURE]));
10
-
11
-  Serial.println("Opendb status: " + String(tmp));
8
+  influxdb.write("weather,device=" + device + " pressure=" + String(sensorValues[SENSOR_PRESSURE]));
9
+  influxdb.write("weather,device=" + device + " batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]));
12 10
 
13 11
 }
14 12
 

+ 8
- 0
firmware/sensors.ino View File

@@ -45,4 +45,12 @@ float fetchWindspeed() {
45 45
 
46 46
   return (float)anemometerRotations / 5.0 * 2.4;
47 47
 }
48
+
49
+// Copied from https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/
50
+float getBatteryVoltage() {
51
+  uint16_t raw = analogRead(A0);
52
+  float volt = raw / 1023.0;
53
+  volt = volt * 4.2;
54
+  return volt;
55
+}
48 56
 

Loading…
Cancel
Save