diff --git a/include/globals.h b/include/globals.h index 41002c32..22ed8954 100644 --- a/include/globals.h +++ b/include/globals.h @@ -111,6 +111,11 @@ typedef struct { float gas; // raw gas sensor signal } bmeStatus_t; +typedef struct { + float pm10; + float pm25; +} sdsStatus_t; + extern std::set, Mallocator> macs; extern std::array::iterator it; extern std::array beacons; diff --git a/include/payload.h b/include/payload.h index 719d59ec..a7de4ddd 100644 --- a/include/payload.h +++ b/include/payload.h @@ -59,8 +59,7 @@ public: void addButton(uint8_t value); void addSensor(uint8_t[]); void addTime(time_t value); - void addPM10(float value); - void addPM25(float value); + void addSDS(sdsStatus_t value); private: void addChars( char* string, int len); diff --git a/include/sds011read.h b/include/sds011read.h index 8c573c0d..97307d1c 100644 --- a/include/sds011read.h +++ b/include/sds011read.h @@ -1,6 +1,7 @@ #ifndef _SDS011READ_H #define _SDS011READ_H +#include "globals.h" #include #define SDCARD_FILE_HEADER_SDS011 ", PM10,PM25" @@ -9,5 +10,6 @@ bool sds011_init(); void sds011_loop(); void sds011_sleep(void); void sds011_wakeup(void); +void sds011_store(sdsStatus_t *sds_store); #endif // _SDS011READ_H diff --git a/platformio.ini b/platformio.ini index 704ebdc7..3a1e9cfb 100644 --- a/platformio.ini +++ b/platformio.ini @@ -73,8 +73,7 @@ lib_deps_sensors = Adafruit BME280 Library@>=2.0.0 Adafruit BMP085 Library@>=1.0.1 BSEC Software Library@1.5.1474 - ;SDS011 sensor Library - https://github.com/ricki-z/SDS011.git#33fd8b6 + https://github.com/ricki-z/SDS011.git#33fd8b6 ;SDS011 sensor Library lib_deps_basic = ArduinoJson@^5.13.1 76@>=1.2.4 ; #76 Timezone by Jack Christensen diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 03318284..0205cc9d 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -10,8 +10,7 @@ static const char TAG[] = __FILE__; TinyGPSPlus gps; TinyGPSCustom gpstime(gps, "GPZDA", 1); // field 1 = UTC time static const String ZDA_Request = "$EIGPQ,ZDA*39\r\n"; - -gpsStatus_t gps_status = {0}; +static gpsStatus_t gps_status = {0}; TaskHandle_t GpsTask; #ifdef GPS_SERIAL diff --git a/src/hal/generic.h b/src/hal/generic.h index fa1d14c7..b63cea89 100644 --- a/src/hal/generic.h +++ b/src/hal/generic.h @@ -48,8 +48,14 @@ //#define HAS_BMP180 //#define BMP180_ADDR 0x77 +// SDS011 dust sensor settings +#define HAS_SDS011 1 // use SDS011 +// used pins on the ESP-side: +#define SDS_TX 19 // connect to RX on the SDS011 +#define SDS_RX 23 // connect to TX on the SDS011 + // user defined sensors -//#define HAS_SENSORS 1 // comment out if device has user defined sensors +#define HAS_SENSORS 1 // comment out if device has user defined sensors #define CFG_sx1276_radio 1 // select LoRa chip //#define CFG_sx1272_radio 1 // select LoRa chip diff --git a/src/hal/ttgov21new.h b/src/hal/ttgov21new.h index 9fa665f2..e7292f2a 100644 --- a/src/hal/ttgov21new.h +++ b/src/hal/ttgov21new.h @@ -15,8 +15,8 @@ // SDS011 dust sensor settings #define HAS_SDS011 1 // use SDS011 // used pins on the ESP-side: -#define ESP_PIN_TX 19 // connect to RX on the SDS011 -#define ESP_PIN_RX 23 // connect to TX on the SDS011 +#define SDS_TX 19 // connect to RX on the SDS011 +#define SDS_RX 23 // connect to TX on the SDS011 #define HAS_LORA 1 // comment out if device shall not send data via LoRa #define CFG_sx1276_radio 1 // HPD13A LoRa SoC diff --git a/src/payload.cpp b/src/payload.cpp index 57428234..b3291348 100644 --- a/src/payload.cpp +++ b/src/payload.cpp @@ -80,7 +80,7 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp, } void PayloadConvert::addGPS(gpsStatus_t value) { -#if(HAS_GPS) +#if (HAS_GPS) buffer[cursor++] = (byte)((value.latitude & 0xFF000000) >> 24); buffer[cursor++] = (byte)((value.latitude & 0x00FF0000) >> 16); buffer[cursor++] = (byte)((value.latitude & 0x0000FF00) >> 8); @@ -100,7 +100,7 @@ void PayloadConvert::addGPS(gpsStatus_t value) { } void PayloadConvert::addSensor(uint8_t buf[]) { -#if(HAS_SENSORS) +#if (HAS_SENSORS) uint8_t length = buf[0]; memcpy(buffer, buf + 1, length); cursor += length; // length of buffer @@ -108,7 +108,7 @@ void PayloadConvert::addSensor(uint8_t buf[]) { } void PayloadConvert::addBME(bmeStatus_t value) { -#if(HAS_BME) +#if (HAS_BME) int16_t temperature = (int16_t)(value.temperature); // float -> int uint16_t humidity = (uint16_t)(value.humidity); // float -> int uint16_t pressure = (uint16_t)(value.pressure); // float -> int @@ -124,6 +124,16 @@ void PayloadConvert::addBME(bmeStatus_t value) { #endif } +void PayloadConvert::addSDS(sdsStatus_t sds) { +#if (HAS_SDS011) + char tempBuffer[10 + 1]; + sprintf(tempBuffer, ",%5.1f", sds.pm10); + addChars(tempBuffer, strlen(tempBuffer)); + sprintf(tempBuffer, ",%5.1f", sds.pm25); + addChars(tempBuffer, strlen(tempBuffer)); +#endif // HAS_SDS011 +} + void PayloadConvert::addButton(uint8_t value) { #ifdef HAS_BUTTON buffer[cursor++] = value; @@ -193,7 +203,7 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp, } void PayloadConvert::addGPS(gpsStatus_t value) { -#if(HAS_GPS) +#if (HAS_GPS) writeLatLng(value.latitude, value.longitude); #if (!PAYLOAD_OPENSENSEBOX) writeUint8(value.satellites); @@ -204,7 +214,7 @@ void PayloadConvert::addGPS(gpsStatus_t value) { } void PayloadConvert::addSensor(uint8_t buf[]) { -#if(HAS_SENSORS) +#if (HAS_SENSORS) uint8_t length = buf[0]; memcpy(buffer, buf + 1, length); cursor += length; // length of buffer @@ -212,7 +222,7 @@ void PayloadConvert::addSensor(uint8_t buf[]) { } void PayloadConvert::addBME(bmeStatus_t value) { -#if(HAS_BME) +#if (HAS_BME) writeFloat(value.temperature); writePressure(value.pressure); writeUFloat(value.humidity); @@ -220,6 +230,13 @@ void PayloadConvert::addBME(bmeStatus_t value) { #endif } +void PayloadConvert::addSDS(sdsStatus_t sds) { +#if (HAS_SDS011) + writeUint16((uint16_t)(sds.pm10 * 10)); + writeUint16((uint16_t)(sds.pm25 * 10)); +#endif // HAS_SDS011 +} + void PayloadConvert::addButton(uint8_t value) { #ifdef HAS_BUTTON writeUint8(value); @@ -242,9 +259,7 @@ void PayloadConvert::uintToBytes(uint64_t value, uint8_t byteSize) { } } -void PayloadConvert::writeUptime(uint64_t uptime) { - writeUint64(uptime); -} +void PayloadConvert::writeUptime(uint64_t uptime) { writeUint64(uptime); } void PayloadConvert::writeVersion(char *version) { memcpy(buffer + cursor, version, 10); @@ -265,13 +280,9 @@ void PayloadConvert::writeUint16(uint16_t i) { uintToBytes(i, 2); } void PayloadConvert::writeUint8(uint8_t i) { uintToBytes(i, 1); } -void PayloadConvert::writeUFloat(float value) { - writeUint16(value * 100); -} +void PayloadConvert::writeUFloat(float value) { writeUint16(value * 100); } -void PayloadConvert::writePressure(float value) { - writeUint16(value * 10); -} +void PayloadConvert::writePressure(float value) { writeUint16(value * 10); } /** * Uses a 16bit two's complement with two decimals, so the range is @@ -312,10 +323,25 @@ void PayloadConvert::writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f, #elif ((PAYLOAD_ENCODER == 3) || (PAYLOAD_ENCODER == 4)) -void PayloadConvert::addByte(uint8_t value) { - /* +void PayloadConvert::addByte(uint8_t value) { + /* not implemented - */ } + */ +} + +void PayloadConvert::addSDS(sdsStatus_t sds) { +#if (HAS_SDS011) + +#if (PAYLOAD_ENCODER == 3) // Cayenne LPP dynamic +#error not implemented yet +#endif + +#if (PAYLOAD_ENCODER == 4) // Cayenne LPP packed +#error not implemented yet +#endif + +#endif // HAS_SDS011 +} void PayloadConvert::addCount(uint16_t value, uint8_t snifftype) { switch (snifftype) { @@ -393,7 +419,7 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float celsius, } void PayloadConvert::addGPS(gpsStatus_t value) { -#if(HAS_GPS) +#if (HAS_GPS) int32_t lat = value.latitude / 100; int32_t lon = value.longitude / 100; int32_t alt = value.altitude * 100; @@ -414,18 +440,18 @@ void PayloadConvert::addGPS(gpsStatus_t value) { } void PayloadConvert::addSensor(uint8_t buf[]) { -#if(HAS_SENSORS) -// to come -/* - uint8_t length = buf[0]; - memcpy(buffer, buf+1, length); - cursor += length; // length of buffer -*/ +#if (HAS_SENSORS) + // to come + /* + uint8_t length = buf[0]; + memcpy(buffer, buf+1, length); + cursor += length; // length of buffer + */ #endif // HAS_SENSORS } void PayloadConvert::addBME(bmeStatus_t value) { -#if(HAS_BME) +#if (HAS_BME) // data value conversions to meet cayenne data type definition // 0.1°C per bit => -3276,7 .. +3276,7 °C @@ -491,39 +517,7 @@ void PayloadConvert::addTime(time_t value) { } #endif // PAYLOAD_ENCODER -void PayloadConvert::addPM10( float value) { -#if (HAS_SDS011) -#if (PAYLOAD_ENCODER == 1) // plain - char tempBuffer[10+1]; - sprintf( tempBuffer, ",%5.1f", value); - addChars(tempBuffer, strlen(tempBuffer)); -#elif (PAYLOAD_ENCODER == 2 ) // packed - writeUint16( (uint16_t) (value*10) ); -#elif (PAYLOAD_ENCODER == 3 ) // Cayenne LPP dynamic -#error not implemented yet -#elif (PAYLOAD_ENCODER == 4 ) // Cayenne LPP packed -#error not implemented yet -#endif -#endif // HAS_SDS011 -} - -void PayloadConvert::addPM25( float value) { -#if (HAS_SDS011) -#if (PAYLOAD_ENCODER == 1) // plain - char tempBuffer[10+1]; - sprintf( tempBuffer, ",%5.1f", value); - addChars(tempBuffer, strlen(tempBuffer)); -#elif (PAYLOAD_ENCODER == 2 ) // packed - writeUint16( (uint16_t) (value*10) ); -#elif (PAYLOAD_ENCODER == 3 ) // Cayenne LPP dynamic -#error not implemented yet -#elif (PAYLOAD_ENCODER == 4 ) // Cayenne LPP packed -#error not implemented yet -#endif -#endif // HAS_SDS011 -} - -void PayloadConvert::addChars( char * string, int len) { - for (int i=0; i < len; i++) - addByte(string[i]); -} +void PayloadConvert::addChars(char *string, int len) { + for (int i = 0; i < len; i++) + addByte(string[i]); +} \ No newline at end of file diff --git a/src/sdcard.cpp b/src/sdcard.cpp index 002cfdf8..5faab5cd 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -6,12 +6,6 @@ static const char TAG[] = __FILE__; #include "sdcard.h" -#if (HAS_SDS011) -#include -// the results of the sensor: -extern float pm25; -extern float pm10; -#endif static bool useSDCard; @@ -25,7 +19,7 @@ bool sdcard_init() { if (useSDCard) createFile(); else - ESP_LOGD(TAG,"SD-card not found"); + ESP_LOGD(TAG, "SD-card not found"); return useSDCard; } @@ -33,6 +27,9 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle) { static int counterWrites = 0; char tempBuffer[12 + 1]; time_t t = now(); +#if (HAS_SDS011) + sdsStatus_t sds; +#endif if (!useSDCard) return; @@ -43,12 +40,13 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle) { sprintf(tempBuffer, "%02d:%02d:%02d,", hour(t), minute(t), second(t)); fileSDCard.print(tempBuffer); sprintf(tempBuffer, "%d,%d", noWifi, noBle); - fileSDCard.print( tempBuffer); + fileSDCard.print(tempBuffer); #if (HAS_SDS011) - sprintf(tempBuffer, ",%5.1f,%4.1f", pm10, pm25); - fileSDCard.print( tempBuffer); + sds011_store(&sds); + sprintf(tempBuffer, ",%5.1f,%4.1f", sds.pm10, sds.pm25); + fileSDCard.print(tempBuffer); #endif - fileSDCard.println( ); + fileSDCard.println(); if (++counterWrites > 2) { // force writing to SD-card @@ -72,9 +70,9 @@ void createFile(void) { fileSDCard = SD.open(bufferFilename, FILE_WRITE); if (fileSDCard) { ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename); - fileSDCard.print( SDCARD_FILE_HEADER ); + fileSDCard.print(SDCARD_FILE_HEADER); #if (HAS_SDS011) - fileSDCard.print( SDCARD_FILE_HEADER_SDS011 ); + fileSDCard.print(SDCARD_FILE_HEADER_SDS011); #endif fileSDCard.println(); useSDCard = true; diff --git a/src/sds011read.cpp b/src/sds011read.cpp index f280fbe6..a693f651 100644 --- a/src/sds011read.cpp +++ b/src/sds011read.cpp @@ -1,5 +1,7 @@ // routines for fetching data from the SDS011-sensor +#if (HAS_SDS011) + // Local logging tag static const char TAG[] = __FILE__; @@ -13,17 +15,14 @@ static HardwareSerial sdsSerial(2); // so we use it here static SDS011 sdsSensor; // fine dust sensor // the results of the sensor: -float pm25; -float pm10; +static float pm10, pm25; boolean isSDS011Active; // init bool sds011_init() { pm25 = pm10 = 0.0; -#if (HAS_SDS011) - sdsSensor.begin (&sdsSerial, ESP_PIN_RX, ESP_PIN_TX); -#endif - sds011_sleep(); // we do sleep/wakup by ourselves + sdsSensor.begin(&sdsSerial, SDS_RX, SDS_TX); + sds011_sleep(); // we do sleep/wakup by ourselves return true; } @@ -42,6 +41,12 @@ void sds011_loop() { return; } +// retrieving stored data: +void sds011_store(sdsStatus_t *sds_store) { + sds_store->pm10 = pm10; + sds_store->pm25 = pm25; +} + // putting the SDS-sensor to sleep void sds011_sleep(void) { sdsSensor.sleep(); @@ -56,3 +61,5 @@ void sds011_wakeup() { isSDS011Active = true; } } + +#endif // HAS_SDS011 diff --git a/src/senddata.cpp b/src/senddata.cpp index f5b867f6..a98c129e 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -3,11 +3,6 @@ Ticker sendcycler; -#if (HAS_SDS011) -extern float pm10; -extern float pm25; -#endif - void sendcycle() { xTaskNotifyFromISR(irqHandlerTask, SENDCYCLE_IRQ, eSetBits, NULL); } @@ -68,9 +63,12 @@ void sendData() { uint8_t bitmask = cfg.payloadmask; uint8_t mask = 1; - #if (HAS_GPS) +#if (HAS_GPS) gpsStatus_t gps_status; - #endif +#endif +#if (HAS_SDS011) + sdsStatus_t sds_status; +#endif while (bitmask) { switch (bitmask & mask) { @@ -101,8 +99,8 @@ void sendData() { payload.addCount(macs_ble, MAC_SNIFF_BLE); #endif #if (HAS_SDS011) - payload.addPM10(pm10); - payload.addPM25(pm25); + sds011_store(&sds_status); + payload.addSDS(sds_status); #endif SendPayload(COUNTERPORT, prio_normal); // clear counter if not in cumulative counter mode