SDS011 code sanitizations

This commit is contained in:
Klaus K Wilting 2020-02-25 22:18:20 +01:00
parent 417c3c7e69
commit 39f99043d6
11 changed files with 107 additions and 100 deletions

View File

@ -111,6 +111,11 @@ typedef struct {
float gas; // raw gas sensor signal float gas; // raw gas sensor signal
} bmeStatus_t; } bmeStatus_t;
typedef struct {
float pm10;
float pm25;
} sdsStatus_t;
extern std::set<uint16_t, std::less<uint16_t>, Mallocator<uint16_t>> macs; extern std::set<uint16_t, std::less<uint16_t>, Mallocator<uint16_t>> macs;
extern std::array<uint64_t, 0xff>::iterator it; extern std::array<uint64_t, 0xff>::iterator it;
extern std::array<uint64_t, 0xff> beacons; extern std::array<uint64_t, 0xff> beacons;

View File

@ -59,8 +59,7 @@ public:
void addButton(uint8_t value); void addButton(uint8_t value);
void addSensor(uint8_t[]); void addSensor(uint8_t[]);
void addTime(time_t value); void addTime(time_t value);
void addPM10(float value); void addSDS(sdsStatus_t value);
void addPM25(float value);
private: private:
void addChars( char* string, int len); void addChars( char* string, int len);

View File

@ -1,6 +1,7 @@
#ifndef _SDS011READ_H #ifndef _SDS011READ_H
#define _SDS011READ_H #define _SDS011READ_H
#include "globals.h"
#include <SDS011.h> #include <SDS011.h>
#define SDCARD_FILE_HEADER_SDS011 ", PM10,PM25" #define SDCARD_FILE_HEADER_SDS011 ", PM10,PM25"
@ -9,5 +10,6 @@ bool sds011_init();
void sds011_loop(); void sds011_loop();
void sds011_sleep(void); void sds011_sleep(void);
void sds011_wakeup(void); void sds011_wakeup(void);
void sds011_store(sdsStatus_t *sds_store);
#endif // _SDS011READ_H #endif // _SDS011READ_H

View File

@ -73,8 +73,7 @@ lib_deps_sensors =
Adafruit BME280 Library@>=2.0.0 Adafruit BME280 Library@>=2.0.0
Adafruit BMP085 Library@>=1.0.1 Adafruit BMP085 Library@>=1.0.1
BSEC Software Library@1.5.1474 BSEC Software Library@1.5.1474
;SDS011 sensor Library https://github.com/ricki-z/SDS011.git#33fd8b6 ;SDS011 sensor Library
https://github.com/ricki-z/SDS011.git#33fd8b6
lib_deps_basic = lib_deps_basic =
ArduinoJson@^5.13.1 ArduinoJson@^5.13.1
76@>=1.2.4 ; #76 Timezone by Jack Christensen 76@>=1.2.4 ; #76 Timezone by Jack Christensen

View File

@ -10,8 +10,7 @@ static const char TAG[] = __FILE__;
TinyGPSPlus gps; TinyGPSPlus gps;
TinyGPSCustom gpstime(gps, "GPZDA", 1); // field 1 = UTC time TinyGPSCustom gpstime(gps, "GPZDA", 1); // field 1 = UTC time
static const String ZDA_Request = "$EIGPQ,ZDA*39\r\n"; static const String ZDA_Request = "$EIGPQ,ZDA*39\r\n";
static gpsStatus_t gps_status = {0};
gpsStatus_t gps_status = {0};
TaskHandle_t GpsTask; TaskHandle_t GpsTask;
#ifdef GPS_SERIAL #ifdef GPS_SERIAL

View File

@ -48,8 +48,14 @@
//#define HAS_BMP180 //#define HAS_BMP180
//#define BMP180_ADDR 0x77 //#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 // 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_sx1276_radio 1 // select LoRa chip
//#define CFG_sx1272_radio 1 // select LoRa chip //#define CFG_sx1272_radio 1 // select LoRa chip

View File

@ -15,8 +15,8 @@
// SDS011 dust sensor settings // SDS011 dust sensor settings
#define HAS_SDS011 1 // use SDS011 #define HAS_SDS011 1 // use SDS011
// used pins on the ESP-side: // used pins on the ESP-side:
#define ESP_PIN_TX 19 // connect to RX on the SDS011 #define SDS_TX 19 // connect to RX on the SDS011
#define ESP_PIN_RX 23 // connect to TX 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 HAS_LORA 1 // comment out if device shall not send data via LoRa
#define CFG_sx1276_radio 1 // HPD13A LoRa SoC #define CFG_sx1276_radio 1 // HPD13A LoRa SoC

View File

@ -124,6 +124,16 @@ void PayloadConvert::addBME(bmeStatus_t value) {
#endif #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) { void PayloadConvert::addButton(uint8_t value) {
#ifdef HAS_BUTTON #ifdef HAS_BUTTON
buffer[cursor++] = value; buffer[cursor++] = value;
@ -220,6 +230,13 @@ void PayloadConvert::addBME(bmeStatus_t value) {
#endif #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) { void PayloadConvert::addButton(uint8_t value) {
#ifdef HAS_BUTTON #ifdef HAS_BUTTON
writeUint8(value); writeUint8(value);
@ -242,9 +259,7 @@ void PayloadConvert::uintToBytes(uint64_t value, uint8_t byteSize) {
} }
} }
void PayloadConvert::writeUptime(uint64_t uptime) { void PayloadConvert::writeUptime(uint64_t uptime) { writeUint64(uptime); }
writeUint64(uptime);
}
void PayloadConvert::writeVersion(char *version) { void PayloadConvert::writeVersion(char *version) {
memcpy(buffer + cursor, version, 10); 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::writeUint8(uint8_t i) { uintToBytes(i, 1); }
void PayloadConvert::writeUFloat(float value) { void PayloadConvert::writeUFloat(float value) { writeUint16(value * 100); }
writeUint16(value * 100);
}
void PayloadConvert::writePressure(float value) { void PayloadConvert::writePressure(float value) { writeUint16(value * 10); }
writeUint16(value * 10);
}
/** /**
* Uses a 16bit two's complement with two decimals, so the range is * Uses a 16bit two's complement with two decimals, so the range is
@ -315,7 +326,22 @@ void PayloadConvert::writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f,
void PayloadConvert::addByte(uint8_t value) { void PayloadConvert::addByte(uint8_t value) {
/* /*
not implemented 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) { void PayloadConvert::addCount(uint16_t value, uint8_t snifftype) {
switch (snifftype) { switch (snifftype) {
@ -491,38 +517,6 @@ void PayloadConvert::addTime(time_t value) {
} }
#endif // PAYLOAD_ENCODER #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) { void PayloadConvert::addChars(char *string, int len) {
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
addByte(string[i]); addByte(string[i]);

View File

@ -6,12 +6,6 @@
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
#include "sdcard.h" #include "sdcard.h"
#if (HAS_SDS011)
#include <sds011read.h>
// the results of the sensor:
extern float pm25;
extern float pm10;
#endif
static bool useSDCard; static bool useSDCard;
@ -33,6 +27,9 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle) {
static int counterWrites = 0; static int counterWrites = 0;
char tempBuffer[12 + 1]; char tempBuffer[12 + 1];
time_t t = now(); time_t t = now();
#if (HAS_SDS011)
sdsStatus_t sds;
#endif
if (!useSDCard) if (!useSDCard)
return; return;
@ -45,7 +42,8 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle) {
sprintf(tempBuffer, "%d,%d", noWifi, noBle); sprintf(tempBuffer, "%d,%d", noWifi, noBle);
fileSDCard.print(tempBuffer); fileSDCard.print(tempBuffer);
#if (HAS_SDS011) #if (HAS_SDS011)
sprintf(tempBuffer, ",%5.1f,%4.1f", pm10, pm25); sds011_store(&sds);
sprintf(tempBuffer, ",%5.1f,%4.1f", sds.pm10, sds.pm25);
fileSDCard.print(tempBuffer); fileSDCard.print(tempBuffer);
#endif #endif
fileSDCard.println(); fileSDCard.println();

View File

@ -1,5 +1,7 @@
// routines for fetching data from the SDS011-sensor // routines for fetching data from the SDS011-sensor
#if (HAS_SDS011)
// Local logging tag // Local logging tag
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
@ -13,16 +15,13 @@ static HardwareSerial sdsSerial(2); // so we use it here
static SDS011 sdsSensor; // fine dust sensor static SDS011 sdsSensor; // fine dust sensor
// the results of the sensor: // the results of the sensor:
float pm25; static float pm10, pm25;
float pm10;
boolean isSDS011Active; boolean isSDS011Active;
// init // init
bool sds011_init() { bool sds011_init() {
pm25 = pm10 = 0.0; pm25 = pm10 = 0.0;
#if (HAS_SDS011) sdsSensor.begin(&sdsSerial, SDS_RX, SDS_TX);
sdsSensor.begin (&sdsSerial, ESP_PIN_RX, ESP_PIN_TX);
#endif
sds011_sleep(); // we do sleep/wakup by ourselves sds011_sleep(); // we do sleep/wakup by ourselves
return true; return true;
} }
@ -42,6 +41,12 @@ void sds011_loop() {
return; 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 // putting the SDS-sensor to sleep
void sds011_sleep(void) { void sds011_sleep(void) {
sdsSensor.sleep(); sdsSensor.sleep();
@ -56,3 +61,5 @@ void sds011_wakeup() {
isSDS011Active = true; isSDS011Active = true;
} }
} }
#endif // HAS_SDS011

View File

@ -3,11 +3,6 @@
Ticker sendcycler; Ticker sendcycler;
#if (HAS_SDS011)
extern float pm10;
extern float pm25;
#endif
void sendcycle() { void sendcycle() {
xTaskNotifyFromISR(irqHandlerTask, SENDCYCLE_IRQ, eSetBits, NULL); xTaskNotifyFromISR(irqHandlerTask, SENDCYCLE_IRQ, eSetBits, NULL);
} }
@ -71,6 +66,9 @@ void sendData() {
#if (HAS_GPS) #if (HAS_GPS)
gpsStatus_t gps_status; gpsStatus_t gps_status;
#endif #endif
#if (HAS_SDS011)
sdsStatus_t sds_status;
#endif
while (bitmask) { while (bitmask) {
switch (bitmask & mask) { switch (bitmask & mask) {
@ -101,8 +99,8 @@ void sendData() {
payload.addCount(macs_ble, MAC_SNIFF_BLE); payload.addCount(macs_ble, MAC_SNIFF_BLE);
#endif #endif
#if (HAS_SDS011) #if (HAS_SDS011)
payload.addPM10(pm10); sds011_store(&sds_status);
payload.addPM25(pm25); payload.addSDS(sds_status);
#endif #endif
SendPayload(COUNTERPORT, prio_normal); SendPayload(COUNTERPORT, prio_normal);
// clear counter if not in cumulative counter mode // clear counter if not in cumulative counter mode