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

@ -80,7 +80,7 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp,
} }
void PayloadConvert::addGPS(gpsStatus_t value) { void PayloadConvert::addGPS(gpsStatus_t value) {
#if(HAS_GPS) #if (HAS_GPS)
buffer[cursor++] = (byte)((value.latitude & 0xFF000000) >> 24); buffer[cursor++] = (byte)((value.latitude & 0xFF000000) >> 24);
buffer[cursor++] = (byte)((value.latitude & 0x00FF0000) >> 16); buffer[cursor++] = (byte)((value.latitude & 0x00FF0000) >> 16);
buffer[cursor++] = (byte)((value.latitude & 0x0000FF00) >> 8); buffer[cursor++] = (byte)((value.latitude & 0x0000FF00) >> 8);
@ -100,7 +100,7 @@ void PayloadConvert::addGPS(gpsStatus_t value) {
} }
void PayloadConvert::addSensor(uint8_t buf[]) { void PayloadConvert::addSensor(uint8_t buf[]) {
#if(HAS_SENSORS) #if (HAS_SENSORS)
uint8_t length = buf[0]; uint8_t length = buf[0];
memcpy(buffer, buf + 1, length); memcpy(buffer, buf + 1, length);
cursor += length; // length of buffer cursor += length; // length of buffer
@ -108,7 +108,7 @@ void PayloadConvert::addSensor(uint8_t buf[]) {
} }
void PayloadConvert::addBME(bmeStatus_t value) { void PayloadConvert::addBME(bmeStatus_t value) {
#if(HAS_BME) #if (HAS_BME)
int16_t temperature = (int16_t)(value.temperature); // float -> int int16_t temperature = (int16_t)(value.temperature); // float -> int
uint16_t humidity = (uint16_t)(value.humidity); // float -> int uint16_t humidity = (uint16_t)(value.humidity); // float -> int
uint16_t pressure = (uint16_t)(value.pressure); // float -> int uint16_t pressure = (uint16_t)(value.pressure); // float -> int
@ -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;
@ -193,7 +203,7 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp,
} }
void PayloadConvert::addGPS(gpsStatus_t value) { void PayloadConvert::addGPS(gpsStatus_t value) {
#if(HAS_GPS) #if (HAS_GPS)
writeLatLng(value.latitude, value.longitude); writeLatLng(value.latitude, value.longitude);
#if (!PAYLOAD_OPENSENSEBOX) #if (!PAYLOAD_OPENSENSEBOX)
writeUint8(value.satellites); writeUint8(value.satellites);
@ -204,7 +214,7 @@ void PayloadConvert::addGPS(gpsStatus_t value) {
} }
void PayloadConvert::addSensor(uint8_t buf[]) { void PayloadConvert::addSensor(uint8_t buf[]) {
#if(HAS_SENSORS) #if (HAS_SENSORS)
uint8_t length = buf[0]; uint8_t length = buf[0];
memcpy(buffer, buf + 1, length); memcpy(buffer, buf + 1, length);
cursor += length; // length of buffer cursor += length; // length of buffer
@ -212,7 +222,7 @@ void PayloadConvert::addSensor(uint8_t buf[]) {
} }
void PayloadConvert::addBME(bmeStatus_t value) { void PayloadConvert::addBME(bmeStatus_t value) {
#if(HAS_BME) #if (HAS_BME)
writeFloat(value.temperature); writeFloat(value.temperature);
writePressure(value.pressure); writePressure(value.pressure);
writeUFloat(value.humidity); writeUFloat(value.humidity);
@ -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
@ -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)) #elif ((PAYLOAD_ENCODER == 3) || (PAYLOAD_ENCODER == 4))
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) {
@ -393,7 +419,7 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float celsius,
} }
void PayloadConvert::addGPS(gpsStatus_t value) { void PayloadConvert::addGPS(gpsStatus_t value) {
#if(HAS_GPS) #if (HAS_GPS)
int32_t lat = value.latitude / 100; int32_t lat = value.latitude / 100;
int32_t lon = value.longitude / 100; int32_t lon = value.longitude / 100;
int32_t alt = value.altitude * 100; int32_t alt = value.altitude * 100;
@ -414,18 +440,18 @@ void PayloadConvert::addGPS(gpsStatus_t value) {
} }
void PayloadConvert::addSensor(uint8_t buf[]) { void PayloadConvert::addSensor(uint8_t buf[]) {
#if(HAS_SENSORS) #if (HAS_SENSORS)
// to come // to come
/* /*
uint8_t length = buf[0]; uint8_t length = buf[0];
memcpy(buffer, buf+1, length); memcpy(buffer, buf+1, length);
cursor += length; // length of buffer cursor += length; // length of buffer
*/ */
#endif // HAS_SENSORS #endif // HAS_SENSORS
} }
void PayloadConvert::addBME(bmeStatus_t value) { void PayloadConvert::addBME(bmeStatus_t value) {
#if(HAS_BME) #if (HAS_BME)
// data value conversions to meet cayenne data type definition // data value conversions to meet cayenne data type definition
// 0.1°C per bit => -3276,7 .. +3276,7 °C // 0.1°C per bit => -3276,7 .. +3276,7 °C
@ -491,39 +517,7 @@ void PayloadConvert::addTime(time_t value) {
} }
#endif // PAYLOAD_ENCODER #endif // PAYLOAD_ENCODER
void PayloadConvert::addPM10( float value) { void PayloadConvert::addChars(char *string, int len) {
#if (HAS_SDS011) for (int i = 0; i < len; i++)
#if (PAYLOAD_ENCODER == 1) // plain addByte(string[i]);
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]);
}

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;
@ -25,7 +19,7 @@ bool sdcard_init() {
if (useSDCard) if (useSDCard)
createFile(); createFile();
else else
ESP_LOGD(TAG,"SD-card not found"); ESP_LOGD(TAG, "SD-card not found");
return useSDCard; return 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;
@ -43,12 +40,13 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle) {
sprintf(tempBuffer, "%02d:%02d:%02d,", hour(t), minute(t), second(t)); sprintf(tempBuffer, "%02d:%02d:%02d,", hour(t), minute(t), second(t));
fileSDCard.print(tempBuffer); fileSDCard.print(tempBuffer);
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);
fileSDCard.print( tempBuffer); sprintf(tempBuffer, ",%5.1f,%4.1f", sds.pm10, sds.pm25);
fileSDCard.print(tempBuffer);
#endif #endif
fileSDCard.println( ); fileSDCard.println();
if (++counterWrites > 2) { if (++counterWrites > 2) {
// force writing to SD-card // force writing to SD-card
@ -72,9 +70,9 @@ void createFile(void) {
fileSDCard = SD.open(bufferFilename, FILE_WRITE); fileSDCard = SD.open(bufferFilename, FILE_WRITE);
if (fileSDCard) { if (fileSDCard) {
ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename); ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename);
fileSDCard.print( SDCARD_FILE_HEADER ); fileSDCard.print(SDCARD_FILE_HEADER);
#if (HAS_SDS011) #if (HAS_SDS011)
fileSDCard.print( SDCARD_FILE_HEADER_SDS011 ); fileSDCard.print(SDCARD_FILE_HEADER_SDS011);
#endif #endif
fileSDCard.println(); fileSDCard.println();
useSDCard = true; useSDCard = true;

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,17 +15,14 @@ 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); sds011_sleep(); // we do sleep/wakup by ourselves
#endif
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);
} }
@ -68,9 +63,12 @@ void sendData() {
uint8_t bitmask = cfg.payloadmask; uint8_t bitmask = cfg.payloadmask;
uint8_t mask = 1; uint8_t mask = 1;
#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