SDS011 code sanitizations
This commit is contained in:
parent
417c3c7e69
commit
39f99043d6
@ -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<uint16_t, std::less<uint16_t>, Mallocator<uint16_t>> macs;
|
||||
extern std::array<uint64_t, 0xff>::iterator it;
|
||||
extern std::array<uint64_t, 0xff> beacons;
|
||||
|
@ -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);
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#ifndef _SDS011READ_H
|
||||
#define _SDS011READ_H
|
||||
|
||||
#include "globals.h"
|
||||
#include <SDS011.h>
|
||||
|
||||
#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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
@ -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
|
||||
@ -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) {
|
||||
/*
|
||||
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) {
|
||||
@ -491,38 +517,6 @@ 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]);
|
||||
|
@ -6,12 +6,6 @@
|
||||
static const char TAG[] = __FILE__;
|
||||
|
||||
#include "sdcard.h"
|
||||
#if (HAS_SDS011)
|
||||
#include <sds011read.h>
|
||||
// the results of the sensor:
|
||||
extern float pm25;
|
||||
extern float pm10;
|
||||
#endif
|
||||
|
||||
static bool 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;
|
||||
@ -45,7 +42,8 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle) {
|
||||
sprintf(tempBuffer, "%d,%d", noWifi, noBle);
|
||||
fileSDCard.print(tempBuffer);
|
||||
#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);
|
||||
#endif
|
||||
fileSDCard.println();
|
||||
|
@ -1,5 +1,7 @@
|
||||
// routines for fetching data from the SDS011-sensor
|
||||
|
||||
#if (HAS_SDS011)
|
||||
|
||||
// Local logging tag
|
||||
static const char TAG[] = __FILE__;
|
||||
|
||||
@ -13,16 +15,13 @@ 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
|
||||
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
|
||||
|
@ -3,11 +3,6 @@
|
||||
|
||||
Ticker sendcycler;
|
||||
|
||||
#if (HAS_SDS011)
|
||||
extern float pm10;
|
||||
extern float pm25;
|
||||
#endif
|
||||
|
||||
void sendcycle() {
|
||||
xTaskNotifyFromISR(irqHandlerTask, SENDCYCLE_IRQ, eSetBits, NULL);
|
||||
}
|
||||
@ -71,6 +66,9 @@ void sendData() {
|
||||
#if (HAS_GPS)
|
||||
gpsStatus_t gps_status;
|
||||
#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
|
||||
|
Loading…
Reference in New Issue
Block a user