Browse Source

read SDS011 & HDC1008 instead

master
noerw 1 year ago
parent
commit
54b9f91628
1 changed files with 45 additions and 22 deletions
  1. 45
    22
      esp8266-gps/esp8266-gps.ino

+ 45
- 22
esp8266-gps/esp8266-gps.ino View File

@@ -1,5 +1,4 @@
1 1
 #include <TinyGPS++.h>
2
-#include "lib/BME280/BME280I2C.h"
3 2
 #include "config.h"
4 3
 #include "gps.h"
5 4
 #include "wifi.h"
@@ -8,6 +7,11 @@
8 7
 #include "TelnetPrint.h"
9 8
 #include "streampipe.h"
10 9
 
10
+#include <Wire.h>
11
+#include <HDC100X.h>
12
+#include <SDS011-select-serial.h>
13
+#include <SoftwareSerial.h>
14
+
11 15
 #define DEBUG_OUT telnet // debug output is available via telnet!
12 16
 
13 17
 Storage storage = Storage();
@@ -16,7 +20,9 @@ TelnetPrint telnet = TelnetPrint();
16 20
 OsemApi api = OsemApi();
17 21
 Gps gps = Gps();
18 22
 
19
-BME280I2C bme(1, 1, 1, 3, 5, 0, false, 0x77);
23
+SoftwareSerial sdsSerial(D1, D2); //D1, D2 works but kills I2C.. D4 is LED!
24
+SDS011 sds(sdsSerial);
25
+HDC100X hdc(0x43);
20 26
 
21 27
 unsigned long cycleStart;
22 28
 WifiState wifiState; // global, as both measure and upload need the state
@@ -34,7 +40,8 @@ bool storeMeasurement(float lat, float lng, float value, const char* timeStamp,
34 40
 
35 41
 void measure(WifiState& wifiState, TinyGPSLocation& loc) {
36 42
   char dateString[22];
37
-  float temperature, humid, pressure;
43
+  float temperature, humidity, pm25, pm10;
44
+  int sds_error;
38 45
 
39 46
   // measure WiFi
40 47
   wifiState = wifi.scan(WIFI_SSID);
@@ -42,15 +49,21 @@ void measure(WifiState& wifiState, TinyGPSLocation& loc) {
42 49
   // update gps position. if we can't get a fix, skip the measurements
43 50
   if(!gps.updateLocation() || !gps.updateTime()) {
44 51
     DEBUG_OUT << "GPS timed out, skipping measurements" << EOL;
45
-    digitalWrite(BUILTIN_LED, LOW); // turn status LED on
52
+    //digitalWrite(BUILTIN_LED, LOW); // turn status LED on
46 53
     return;
47 54
   }
48 55
 
49
-  digitalWrite(BUILTIN_LED, HIGH);
56
+  //digitalWrite(BUILTIN_LED, HIGH);
50 57
   loc = gps.getLocation();
51 58
   gps.getISODate(dateString);
52 59
 
53
-  bme.read(pressure, temperature, humid, 1, true);
60
+  temperature = hdc.getTemp();
61
+  humidity = hdc.getHumi();
62
+  sds_error = sds.read(&pm25, &pm10);
63
+  if (sds_error) {
64
+    DEBUG_OUT << "could not read SDS011" << EOL;
65
+    //return;
66
+  }
54 67
 
55 68
   // print state
56 69
   DEBUG_OUT << "homeAvailable:  " << wifiState.homeAvailable << EOL;
@@ -58,7 +71,9 @@ void measure(WifiState& wifiState, TinyGPSLocation& loc) {
58 71
   DEBUG_OUT << "numNetworks:    " << wifiState.numNetworks << EOL;
59 72
   DEBUG_OUT << "numUnencrypted: " << wifiState.numUnencrypted << EOL;
60 73
   DEBUG_OUT << "temperature: " << temperature << EOL;
61
-  DEBUG_OUT << "pressure: " << pressure << EOL;
74
+  DEBUG_OUT << "humidity: " << humidity << EOL;
75
+  DEBUG_OUT << "PM2.5: " << pm25 << EOL;
76
+  DEBUG_OUT << "PM10: " << pm10 << EOL;
62 77
 
63 78
   DEBUG_OUT.print("lat: ");
64 79
   DEBUG_OUT.print(loc.lat(), 8);
@@ -66,8 +81,6 @@ void measure(WifiState& wifiState, TinyGPSLocation& loc) {
66 81
   DEBUG_OUT.println(loc.lng(), 8);
67 82
   DEBUG_OUT << dateString << EOL;
68 83
 
69
-  // IDEA: write location update to separate file?
70
-
71 84
   // write measurements to file
72 85
   if (!storeMeasurement(loc.lat(), loc.lng(), wifiState.numAccessPoints, dateString, ID_SENSOR_WIFI_APS))
73 86
     DEBUG_OUT << "measurement (wifi aps) store failed!" << EOL;
@@ -78,11 +91,21 @@ void measure(WifiState& wifiState, TinyGPSLocation& loc) {
78 91
   if (!storeMeasurement(loc.lat(), loc.lng(), wifiState.numUnencrypted, dateString, ID_SENSOR_WIFI_OPEN))
79 92
     DEBUG_OUT << "measurement (wifi open) store failed!" << EOL;
80 93
 
81
-  if (!storeMeasurement(loc.lat(), loc.lng(), temperature, dateString, ID_SENSOR_TEMP))
82
-    DEBUG_OUT << "measurement (temperature) store failed!" << EOL;
94
+  if (temperature != -40) {
95
+    if (!storeMeasurement(loc.lat(), loc.lng(), temperature, dateString, ID_SENSOR_TEMP))
96
+      DEBUG_OUT << "measurement (temperature) store failed!" << EOL;
97
+  
98
+    if (!storeMeasurement(loc.lat(), loc.lng(), humidity, dateString, ID_SENSOR_HUMI))
99
+      DEBUG_OUT << "measurement (humidity) store failed!" << EOL;
100
+  }
83 101
 
84
-  if (!storeMeasurement(loc.lat(), loc.lng(), pressure, dateString, ID_SENSOR_PRESSURE))
85
-    DEBUG_OUT << "measurement (pressure) store failed!" << EOL;
102
+  if (!sds_error) {
103
+    if (!storeMeasurement(loc.lat(), loc.lng(), pm10, dateString, ID_SENSOR_PM10))
104
+      DEBUG_OUT << "measurement (pm10) store failed!" << EOL;
105
+  
106
+    if (!storeMeasurement(loc.lat(), loc.lng(), pm25, dateString, ID_SENSOR_PM25))
107
+      DEBUG_OUT << "measurement (pm25) store failed!" << EOL;    
108
+  }
86 109
 
87 110
   DEBUG_OUT << EOL;
88 111
 }
@@ -157,20 +180,20 @@ void adaptiveDelay(unsigned long ms, TinyGPSLocation& lastLoc, unsigned long off
157 180
 
158 181
 /* MAIN ENTRY POINTS */
159 182
 void setup() {
160
-  pinMode(BUILTIN_LED, OUTPUT); // status indicator LED: on = no GPS fix
161
-  digitalWrite(BUILTIN_LED, LOW);
183
+  //pinMode(BUILTIN_LED, OUTPUT); // status indicator LED: on = no GPS fix
184
+  //digitalWrite(BUILTIN_LED, LOW);
162 185
   pinMode(PIN_MEASURE_MODE, INPUT_PULLUP); // switch for measurements (pull it down to disable)
163 186
   pinMode(PIN_UPLOAD_MODE, INPUT_PULLUP);  // switch for API uploads  (pull it down to disable)
164 187
 
165 188
   size_t bytesFree = storage.begin();
166 189
   gps.begin();
167 190
   wifi.begin(WIFI_SSID, WIFI_PASS);
168
-  DEBUG_OUT.begin(115200);
191
+  DEBUG_OUT.begin(9600);
169 192
 
170
-  while(!bme.begin(D14, D15)){
171
-    adaptiveDelay(1000, location);
172
-    DEBUG_OUT.println("Could not find BME280I2C sensor!");
173
-  }
193
+  sdsSerial.begin(9600);
194
+  Wire.begin();
195
+  hdc.begin(HDC100X_TEMP_HUMI, HDC100X_14BIT, HDC100X_14BIT, DISABLE);
196
+  hdc.getTemp();
174 197
 
175 198
   // wait until we got a first fix from GPS, and thus an initial time
176 199
   // exception: measure mode is disabled (for quick upload only)
@@ -182,13 +205,13 @@ void setup() {
182 205
     }
183 206
     location = gps.getLocation();
184 207
     DEBUG_OUT << " done!" << EOL;
185
-    digitalWrite(BUILTIN_LED, HIGH);
208
+    //digitalWrite(BUILTIN_LED, HIGH);
186 209
   }
187 210
 
188 211
   // DEBUG
189 212
   //while (!wifi.isConnected()) adaptiveDelay(500, location);
190 213
   //String temp;  while (storage.size()) storage.get(temp, true);
191
-
214
+  
192 215
   DEBUG_OUT << "Setup done!" << EOL;
193 216
   DEBUG_OUT << "WiFi MAC            WiFi IP" << EOL;
194 217
   DEBUG_OUT << WiFi.macAddress() << "   " << WiFi.localIP() << EOL;

Loading…
Cancel
Save