From 54b9f916283ac83e519cf4efffb79546c53ad131 Mon Sep 17 00:00:00 2001 From: noerw Date: Sat, 23 Sep 2017 14:39:40 +0200 Subject: [PATCH] read SDS011 & HDC1008 instead --- esp8266-gps/esp8266-gps.ino | 67 +++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/esp8266-gps/esp8266-gps.ino b/esp8266-gps/esp8266-gps.ino index 30f40c0..5a60bff 100644 --- a/esp8266-gps/esp8266-gps.ino +++ b/esp8266-gps/esp8266-gps.ino @@ -1,5 +1,4 @@ #include -#include "lib/BME280/BME280I2C.h" #include "config.h" #include "gps.h" #include "wifi.h" @@ -8,6 +7,11 @@ #include "TelnetPrint.h" #include "streampipe.h" +#include +#include +#include +#include + #define DEBUG_OUT telnet // debug output is available via telnet! Storage storage = Storage(); @@ -16,7 +20,9 @@ TelnetPrint telnet = TelnetPrint(); OsemApi api = OsemApi(); Gps gps = Gps(); -BME280I2C bme(1, 1, 1, 3, 5, 0, false, 0x77); +SoftwareSerial sdsSerial(D1, D2); //D1, D2 works but kills I2C.. D4 is LED! +SDS011 sds(sdsSerial); +HDC100X hdc(0x43); unsigned long cycleStart; 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, void measure(WifiState& wifiState, TinyGPSLocation& loc) { char dateString[22]; - float temperature, humid, pressure; + float temperature, humidity, pm25, pm10; + int sds_error; // measure WiFi wifiState = wifi.scan(WIFI_SSID); @@ -42,15 +49,21 @@ void measure(WifiState& wifiState, TinyGPSLocation& loc) { // update gps position. if we can't get a fix, skip the measurements if(!gps.updateLocation() || !gps.updateTime()) { DEBUG_OUT << "GPS timed out, skipping measurements" << EOL; - digitalWrite(BUILTIN_LED, LOW); // turn status LED on + //digitalWrite(BUILTIN_LED, LOW); // turn status LED on return; } - digitalWrite(BUILTIN_LED, HIGH); + //digitalWrite(BUILTIN_LED, HIGH); loc = gps.getLocation(); gps.getISODate(dateString); - bme.read(pressure, temperature, humid, 1, true); + temperature = hdc.getTemp(); + humidity = hdc.getHumi(); + sds_error = sds.read(&pm25, &pm10); + if (sds_error) { + DEBUG_OUT << "could not read SDS011" << EOL; + //return; + } // print state DEBUG_OUT << "homeAvailable: " << wifiState.homeAvailable << EOL; @@ -58,7 +71,9 @@ void measure(WifiState& wifiState, TinyGPSLocation& loc) { DEBUG_OUT << "numNetworks: " << wifiState.numNetworks << EOL; DEBUG_OUT << "numUnencrypted: " << wifiState.numUnencrypted << EOL; DEBUG_OUT << "temperature: " << temperature << EOL; - DEBUG_OUT << "pressure: " << pressure << EOL; + DEBUG_OUT << "humidity: " << humidity << EOL; + DEBUG_OUT << "PM2.5: " << pm25 << EOL; + DEBUG_OUT << "PM10: " << pm10 << EOL; DEBUG_OUT.print("lat: "); DEBUG_OUT.print(loc.lat(), 8); @@ -66,8 +81,6 @@ void measure(WifiState& wifiState, TinyGPSLocation& loc) { DEBUG_OUT.println(loc.lng(), 8); DEBUG_OUT << dateString << EOL; - // IDEA: write location update to separate file? - // write measurements to file if (!storeMeasurement(loc.lat(), loc.lng(), wifiState.numAccessPoints, dateString, ID_SENSOR_WIFI_APS)) DEBUG_OUT << "measurement (wifi aps) store failed!" << EOL; @@ -78,11 +91,21 @@ void measure(WifiState& wifiState, TinyGPSLocation& loc) { if (!storeMeasurement(loc.lat(), loc.lng(), wifiState.numUnencrypted, dateString, ID_SENSOR_WIFI_OPEN)) DEBUG_OUT << "measurement (wifi open) store failed!" << EOL; - if (!storeMeasurement(loc.lat(), loc.lng(), temperature, dateString, ID_SENSOR_TEMP)) - DEBUG_OUT << "measurement (temperature) store failed!" << EOL; + if (temperature != -40) { + if (!storeMeasurement(loc.lat(), loc.lng(), temperature, dateString, ID_SENSOR_TEMP)) + DEBUG_OUT << "measurement (temperature) store failed!" << EOL; + + if (!storeMeasurement(loc.lat(), loc.lng(), humidity, dateString, ID_SENSOR_HUMI)) + DEBUG_OUT << "measurement (humidity) store failed!" << EOL; + } - if (!storeMeasurement(loc.lat(), loc.lng(), pressure, dateString, ID_SENSOR_PRESSURE)) - DEBUG_OUT << "measurement (pressure) store failed!" << EOL; + if (!sds_error) { + if (!storeMeasurement(loc.lat(), loc.lng(), pm10, dateString, ID_SENSOR_PM10)) + DEBUG_OUT << "measurement (pm10) store failed!" << EOL; + + if (!storeMeasurement(loc.lat(), loc.lng(), pm25, dateString, ID_SENSOR_PM25)) + DEBUG_OUT << "measurement (pm25) store failed!" << EOL; + } DEBUG_OUT << EOL; } @@ -157,20 +180,20 @@ void adaptiveDelay(unsigned long ms, TinyGPSLocation& lastLoc, unsigned long off /* MAIN ENTRY POINTS */ void setup() { - pinMode(BUILTIN_LED, OUTPUT); // status indicator LED: on = no GPS fix - digitalWrite(BUILTIN_LED, LOW); + //pinMode(BUILTIN_LED, OUTPUT); // status indicator LED: on = no GPS fix + //digitalWrite(BUILTIN_LED, LOW); pinMode(PIN_MEASURE_MODE, INPUT_PULLUP); // switch for measurements (pull it down to disable) pinMode(PIN_UPLOAD_MODE, INPUT_PULLUP); // switch for API uploads (pull it down to disable) size_t bytesFree = storage.begin(); gps.begin(); wifi.begin(WIFI_SSID, WIFI_PASS); - DEBUG_OUT.begin(115200); + DEBUG_OUT.begin(9600); - while(!bme.begin(D14, D15)){ - adaptiveDelay(1000, location); - DEBUG_OUT.println("Could not find BME280I2C sensor!"); - } + sdsSerial.begin(9600); + Wire.begin(); + hdc.begin(HDC100X_TEMP_HUMI, HDC100X_14BIT, HDC100X_14BIT, DISABLE); + hdc.getTemp(); // wait until we got a first fix from GPS, and thus an initial time // exception: measure mode is disabled (for quick upload only) @@ -182,13 +205,13 @@ void setup() { } location = gps.getLocation(); DEBUG_OUT << " done!" << EOL; - digitalWrite(BUILTIN_LED, HIGH); + //digitalWrite(BUILTIN_LED, HIGH); } // DEBUG //while (!wifi.isConnected()) adaptiveDelay(500, location); //String temp; while (storage.size()) storage.get(temp, true); - + DEBUG_OUT << "Setup done!" << EOL; DEBUG_OUT << "WiFi MAC WiFi IP" << EOL; DEBUG_OUT << WiFi.macAddress() << " " << WiFi.localIP() << EOL;