From eee4b0ca2f1bc3ff13b5f92df7fa3f3412124eae Mon Sep 17 00:00:00 2001 From: roberbike Date: Wed, 16 Mar 2022 13:20:23 +0100 Subject: [PATCH 01/30] Geiger first test version --- src/Sensors.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++- src/Sensors.hpp | 15 ++++++- 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index c8b7db2b..282ef63e 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -72,7 +72,7 @@ bool Sensors::readAllSensors() { bme680Read(); dhtRead(); disableWire1(); - + geigerLoop();; printValues(); printSensorsRegistered(devmode); printUnitsRegistered(devmode); @@ -117,6 +117,7 @@ void Sensors::init(int pms_type, int pms_rx, int pms_tx) { sht31Init(); aht10Init(); dhtInit(); + geigerInit(); printSensorsRegistered(true); } @@ -1717,3 +1718,103 @@ bool Sensors::serialInit(int pms_type, unsigned long speed_baud, int pms_rx, int #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SENSORSHANDLER) Sensors sensors; #endif + +// *************** GEIGER **************** +// *************************************** + + + +// variables shared between main code and interrupt code +hw_timer_t * timer = NULL; +volatile uint32_t updateTime = 0; // time for next update +volatile uint16_t tic_cnt = 0; + +// To calculate a running average over 10 sec, we keep tic counts in 250ms intervals and add all 40 tic_buf values +#define TICBUFSIZE 40 // running average buffer size +volatile uint16_t tic_buf[TICBUFSIZE]; // buffer for 40 times 250ms tick values (to have a running average for 10 seconds) +volatile uint16_t tic_buf_cnt = 0; + +// In order to display a history of the last 7 minutes, we keep the last 50 values of 10sec tics +#define SEC10BUFSIZE 50 // history array for displaymode==true +volatile uint16_t sec10 = 0; // every 10 seconds counter +volatile uint16_t sec10_buf[SEC10BUFSIZE]; // buffer to hold 10 sec history (40*10 = 400 seconds) +volatile bool sec10updated = false; // set to true when sec10_buf is updated + + +// ######################################################################### +// Interrupt routine called on each click from the geiger tube +// +void IRAM_ATTR TicISR() { + tic_cnt++; +} + +// ######################################################################### +// Interrupt timer routine called every 250 ms +// +void IRAM_ATTR onTimer() { + tic_buf[tic_buf_cnt++] = tic_cnt; + tic_cnt = 0; + if (tic_buf_cnt>=TICBUFSIZE) { + uint16_t tot = 0; + for (int i=0; i=SEC10BUFSIZE) sec10 = 0; + sec10updated = true; + } +} + + // Convert tics to mR/hr +float tics2mrem(uint16_t tics) { + return float(tics) * TICFACTOR; +} + + void geigerInit() { + + Serial.begin(115200); // For debug + Serial.println("Geiger counter startup"); + updateTime = millis(); // Next update time + // attach interrupt routine to TIC interface from the geiger counter module + pinMode(PINTIC, INPUT); + attachInterrupt(PINTIC, TicISR, FALLING); + + // attach interrupt routine to internal timer, to fire every 250 ms + timer = timerBegin(0, 80, true); + timerAttachInterrupt(timer, &onTimer, true); + timerAlarmWrite(timer, 250000, true); // 250 ms + timerAlarmEnable(timer); + Serial.println("Geiger counter ready"); +} + +void geigerLoop() { + + // curent mR/hr value to display - add all tics from walking average 10 seconds + uint16_t v=0; + for (int i=0; i mR/hr + /*************************************************************** * S E T U P E S P 3 2 B O A R D S A N D F I E L D S ***************************************************************/ @@ -98,7 +104,8 @@ X(PRESS, "hPa", "P") \ X(ALT, "m", "Alt") \ X(GAS, "Ohm", "Gas") \ - X(UCOUNT, "COUNT", "UCOUNT") + X(UCOUNT, "COUNT", "UCOUNT")\ + X(RADIATION, "mR", "mRem") #define X(unit, symbol, name) unit, typedef enum UNIT : size_t { SENSOR_UNITS } UNIT; @@ -121,7 +128,8 @@ typedef enum UNIT : size_t { SENSOR_UNITS } UNIT; X(SAHT10, "AHT10", 3) \ X(SAM232X, "AM232X", 3) \ X(SDHTX, "DHTX", 3) \ - X(SCOUNT, "SCOUNT", 3) + X(SCOUNT, "SCOUNT", 3) /* \ + X(RADIATION, "CAJOE", 2) */ #define X(utype, uname, umaintype) utype, typedef enum SENSORS : size_t { SENSORS_TYPES } SENSORS; // backward compatibility @@ -455,3 +463,6 @@ extern Sensors sensors; #endif #endif + +void geigerInit(); +void geigerLoop(); From b89b90c86fad646df89b8275461d409f0c5863f4 Mon Sep 17 00:00:00 2001 From: roberbike Date: Wed, 16 Mar 2022 13:28:51 +0100 Subject: [PATCH 02/30] serial mensage changued --- src/Sensors.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 282ef63e..93fb8078 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -1799,8 +1799,8 @@ void geigerLoop() { char buf[12]; dtostrf(mrem, 7, (mrem<1 ? 2: (mrem<10 ? 1 : 0)), buf); - Serial.println("tics2mrem"); Serial.println (mrem); - Serial.println("tic2men"); Serial.println(v); + Serial.println("mili Rem"); Serial.println (mrem); + Serial.println("tics"); Serial.println(v); } From 831d8795f7a39d2a76068043fbf41bf9b2f0a447 Mon Sep 17 00:00:00 2001 From: roberbike Date: Wed, 16 Mar 2022 16:20:52 +0100 Subject: [PATCH 03/30] minor changues --- src/Sensors.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 93fb8078..243d86dc 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -1799,8 +1799,8 @@ void geigerLoop() { char buf[12]; dtostrf(mrem, 7, (mrem<1 ? 2: (mrem<10 ? 1 : 0)), buf); - Serial.println("mili Rem"); Serial.println (mrem); - Serial.println("tics"); Serial.println(v); + Serial.print("mRem: "); Serial.println (mrem); + Serial.print("tics: "); Serial.println(v); } From c527a4c6bb016943d254030055008ab35d7a75a1 Mon Sep 17 00:00:00 2001 From: roberbike Date: Sat, 16 Apr 2022 23:45:23 +0200 Subject: [PATCH 04/30] Geiger example --- examples/Radiation_CAJOE/Radiation_CAJOE.ino | 158 +++++++++++++++++++ src/Sensors.hpp | 6 +- 2 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 examples/Radiation_CAJOE/Radiation_CAJOE.ino diff --git a/examples/Radiation_CAJOE/Radiation_CAJOE.ino b/examples/Radiation_CAJOE/Radiation_CAJOE.ino new file mode 100644 index 00000000..166b3053 --- /dev/null +++ b/examples/Radiation_CAJOE/Radiation_CAJOE.ino @@ -0,0 +1,158 @@ +/** + * @file main.cpp + * @date June 2018 - 2021 + * @brief Radiation sensor example + * @license GPL3 + * + * @license GPL3 + * + * Full documentation: + * https://github.com/kike-canaries/canairio_sensorlib#canairio-air-quality-sensors-library + * + * Full implementation for WiFi and Bluetooth Air Quality fixed and mobile station: + * https://github.com/kike-canaries/canairio_firmware#canairio-firmware + * + * CanAirIO project: + * https://canair.io + */ + +#include + +#include + +void onSensorDataOk() { + Serial.print(" CO2: " + sensors.getStringCO2()); + Serial.print(" CO2humi: " + String(sensors.getCO2humi())); + Serial.print(" CO2temp: " + String(sensors.getCO2temp())); + + Serial.print(" H: " + String(sensors.getHumidity())); + Serial.println(" T: " + String(sensors.getTemperature())); +} + +void onSensorDataError(const char* msg) { + Serial.println(msg); +} + +/****************************************************************************** +* M A I N +******************************************************************************/ + +void setup() { + Serial.begin(115200); + delay(200); + Serial.println("\n== Sensor test setup ==\n"); + + Serial.println("-->[SETUP] Detecting sensors.."); + + sensors.setSampleTime(5); // config sensors sample time interval + sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback + sensors.setOnErrorCallBack(&onSensorDataError); // [optional] error callback + sensors.setDebugMode(true); // [optional] debug mode + + sensors.init(); // forced UAQ sensor. Empty for auto detection + + delay(500); +} + +void loop() { + sensors.loop(); // read sensor data and showed it +} +// *************** GEIGER **************** +// *************************************** + + + +// variables shared between main code and interrupt code +hw_timer_t * timer = NULL; +volatile uint32_t updateTime = 0; // time for next update +volatile uint16_t tic_cnt = 0; + +// To calculate a running average over 10 sec, we keep tic counts in 250ms intervals and add all 40 tic_buf values +#define TICBUFSIZE 40 // running average buffer size +volatile uint16_t tic_buf[TICBUFSIZE]; // buffer for 40 times 250ms tick values (to have a running average for 10 seconds) +volatile uint16_t tic_buf_cnt = 0; + +// In order to display a history of the last 7 minutes, we keep the last 50 values of 10sec tics +#define SEC10BUFSIZE 50 // history array for displaymode==true +volatile uint16_t sec10 = 0; // every 10 seconds counter +volatile uint16_t sec10_buf[SEC10BUFSIZE]; // buffer to hold 10 sec history (40*10 = 400 seconds) +volatile bool sec10updated = false; // set to true when sec10_buf is updated + + +// ######################################################################### +// Interrupt routine called on each click from the geiger tube +// +void IRAM_ATTR TicISR() { + tic_cnt++; +} + +// ######################################################################### +// Interrupt timer routine called every 250 ms +// +void IRAM_ATTR onTimer() { + tic_buf[tic_buf_cnt++] = tic_cnt; + tic_cnt = 0; + if (tic_buf_cnt>=TICBUFSIZE) { + uint16_t tot = 0; + for (int i=0; i=SEC10BUFSIZE) sec10 = 0; + sec10updated = true; + } +} + + // Convert tics to mR/hr +float tics2mrem(uint16_t tics) { + return float(tics) * TICFACTOR; +} + + void geigerInit() { + + Serial.begin(115200); // For debug + Serial.println("Geiger counter startup"); + updateTime = millis(); // Next update time + // attach interrupt routine to TIC interface from the geiger counter module + pinMode(PINTIC, INPUT); + attachInterrupt(PINTIC, TicISR, FALLING); + + // attach interrupt routine to internal timer, to fire every 250 ms + timer = timerBegin(0, 80, true); + timerAttachInterrupt(timer, &onTimer, true); + timerAlarmWrite(timer, 250000, true); // 250 ms + timerAlarmEnable(timer); + Serial.println("Geiger counter ready"); +} + +void geigerLoop() { + + // curent mR/hr value to display - add all tics from walking average 10 seconds + uint16_t v=0; + for (int i=0; i Date: Mon, 18 Apr 2022 19:57:00 +0200 Subject: [PATCH 05/30] fixed esp8266 --- src/Sensors.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++------- src/Sensors.hpp | 11 ++++++---- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 243d86dc..2fbf2496 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -1721,11 +1721,17 @@ Sensors sensors; // *************** GEIGER **************** // *************************************** +#ifdef ESP8266 + static volatile unsigned long counts = 0; + static int secondcounts[60]; + static unsigned long int secidx_prev = 0; + static unsigned long int count_prev = 0; + static unsigned long int second_prev = 0; +#else - - // variables shared between main code and interrupt code hw_timer_t * timer = NULL; +#endif volatile uint32_t updateTime = 0; // time for next update volatile uint16_t tic_cnt = 0; @@ -1740,14 +1746,21 @@ volatile uint16_t sec10 = 0; // every 10 seconds counter volatile uint16_t sec10_buf[SEC10BUFSIZE]; // buffer to hold 10 sec history (40*10 = 400 seconds) volatile bool sec10updated = false; // set to true when sec10_buf is updated - + #ifdef ESP8266 + // interrupt routine +ICACHE_RAM_ATTR static void TicISR() +{ + counts++; +} +#else // ######################################################################### // Interrupt routine called on each click from the geiger tube // void IRAM_ATTR TicISR() { tic_cnt++; } - + #endif + // ######################################################################### // Interrupt timer routine called every 250 ms // @@ -1770,11 +1783,11 @@ float tics2mrem(uint16_t tics) { } void geigerInit() { - Serial.begin(115200); // For debug Serial.println("Geiger counter startup"); + #ifndef ESP8266 updateTime = millis(); // Next update time - // attach interrupt routine to TIC interface from the geiger counter module + // attach interrupt routine to TIC interface from the geiger counter module pinMode(PINTIC, INPUT); attachInterrupt(PINTIC, TicISR, FALLING); @@ -1784,10 +1797,18 @@ float tics2mrem(uint16_t tics) { timerAlarmWrite(timer, 250000, true); // 250 ms timerAlarmEnable(timer); Serial.println("Geiger counter ready"); + #else + // start counting + memset(secondcounts, 0, sizeof(secondcounts)); + Serial.println("Starting count ..."); + + pinMode(PINTIC, INPUT); + attachInterrupt(digitalPinToInterrupt(PINTIC), TicISR, FALLING); + #endif } void geigerLoop() { - + #ifdef ESP32 // curent mR/hr value to display - add all tics from walking average 10 seconds uint16_t v=0; for (int i=0; i= LOG_PERIOD) { + second_prev = second; + + // calculate sum + int cpm = 0; + for (int i = 0; i < 60; i++) { + cpm += secondcounts[i]; + } + } +#endif } /*// Convert tics to mR/hr diff --git a/src/Sensors.hpp b/src/Sensors.hpp index 45382a82..17a0dafc 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -25,6 +25,7 @@ * ************************************************************/ #define PINTIC 27 // GPIO18 is tic from geiger counter #define TICFACTOR 0.05 // factor between number of tics/second --> mR/hr +#define LOG_PERIOD 10 // for esp8266 variant /*************************************************************** * S E T U P E S P 3 2 B O A R D S A N D F I E L D S @@ -128,8 +129,8 @@ typedef enum UNIT : size_t { SENSOR_UNITS } UNIT; X(SAHT10, "AHT10", 3) \ X(SAM232X, "AM232X", 3) \ X(SDHTX, "DHTX", 3) \ - X(SCOUNT, "SCOUNT", 3) /* \ - X(RADIATION, "CAJOE", 2) */ + X(SCOUNT, "SCOUNT", 3) /*\ + X(SRADIATION, "CAJOE", 2) */ #define X(utype, uname, umaintype) utype, typedef enum SENSORS : size_t { SENSORS_TYPES } SENSORS; // backward compatibility @@ -450,8 +451,8 @@ class Sensors { uint8_t *getUnitsRegistered(); - void geigerInit(); - void geigerLoop(); + // void geigerInit(); + // void geigerLoop(); // @todo use DEBUG_ESP_PORT ? #ifdef WM_DEBUG_PORT @@ -466,3 +467,5 @@ extern Sensors sensors; #endif #endif + void geigerInit(); + void geigerLoop(); \ No newline at end of file From 02e1887f81b99403e0e928f74362c74e7f18a4ce Mon Sep 17 00:00:00 2001 From: roberbike Date: Mon, 18 Apr 2022 20:11:47 +0200 Subject: [PATCH 06/30] esp8266 serial print values --- src/Sensors.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 2fbf2496..94a88223 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -1842,6 +1842,8 @@ void geigerLoop() { for (int i = 0; i < 60; i++) { cpm += secondcounts[i]; } + Serial.print("Counts: "); Serial.println(counts); + Serial.print("cpm: "); Serial.println(cpm); } #endif } From e5ceedf607c5783d01c3d3c23729d3bdc636373d Mon Sep 17 00:00:00 2001 From: roberbike Date: Sat, 23 Apr 2022 17:57:28 +0200 Subject: [PATCH 07/30] Changued PINTIC for esp8266 --- src/Sensors.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Sensors.hpp b/src/Sensors.hpp index 17a0dafc..1dd1d44d 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -23,7 +23,12 @@ /************************************************************** * GEIGER * ************************************************************/ -#define PINTIC 27 // GPIO18 is tic from geiger counter +// Define pin for tics from geiger counter +#ifdef ESP8266 +#define PINTIC D5 +#else +#define PINTIC 27 +#endif #define TICFACTOR 0.05 // factor between number of tics/second --> mR/hr #define LOG_PERIOD 10 // for esp8266 variant From 184765afc1bf23bd7e97bfb16c6a6d814c771ee0 Mon Sep 17 00:00:00 2001 From: iw2lsi Date: Tue, 7 Jun 2022 19:21:24 +0200 Subject: [PATCH 08/30] CAJOE Geiger Counter refactored --- examples/Radiation_CAJOE/Radiation_CAJOE.ino | 158 ++++++++++++++ src/MovingSum.h | 163 +++++++++++++++ src/Sensors.cpp | 207 ++++++++++++++++++- src/Sensors.hpp | 42 +++- 4 files changed, 564 insertions(+), 6 deletions(-) create mode 100644 examples/Radiation_CAJOE/Radiation_CAJOE.ino create mode 100644 src/MovingSum.h diff --git a/examples/Radiation_CAJOE/Radiation_CAJOE.ino b/examples/Radiation_CAJOE/Radiation_CAJOE.ino new file mode 100644 index 00000000..166b3053 --- /dev/null +++ b/examples/Radiation_CAJOE/Radiation_CAJOE.ino @@ -0,0 +1,158 @@ +/** + * @file main.cpp + * @date June 2018 - 2021 + * @brief Radiation sensor example + * @license GPL3 + * + * @license GPL3 + * + * Full documentation: + * https://github.com/kike-canaries/canairio_sensorlib#canairio-air-quality-sensors-library + * + * Full implementation for WiFi and Bluetooth Air Quality fixed and mobile station: + * https://github.com/kike-canaries/canairio_firmware#canairio-firmware + * + * CanAirIO project: + * https://canair.io + */ + +#include + +#include + +void onSensorDataOk() { + Serial.print(" CO2: " + sensors.getStringCO2()); + Serial.print(" CO2humi: " + String(sensors.getCO2humi())); + Serial.print(" CO2temp: " + String(sensors.getCO2temp())); + + Serial.print(" H: " + String(sensors.getHumidity())); + Serial.println(" T: " + String(sensors.getTemperature())); +} + +void onSensorDataError(const char* msg) { + Serial.println(msg); +} + +/****************************************************************************** +* M A I N +******************************************************************************/ + +void setup() { + Serial.begin(115200); + delay(200); + Serial.println("\n== Sensor test setup ==\n"); + + Serial.println("-->[SETUP] Detecting sensors.."); + + sensors.setSampleTime(5); // config sensors sample time interval + sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback + sensors.setOnErrorCallBack(&onSensorDataError); // [optional] error callback + sensors.setDebugMode(true); // [optional] debug mode + + sensors.init(); // forced UAQ sensor. Empty for auto detection + + delay(500); +} + +void loop() { + sensors.loop(); // read sensor data and showed it +} +// *************** GEIGER **************** +// *************************************** + + + +// variables shared between main code and interrupt code +hw_timer_t * timer = NULL; +volatile uint32_t updateTime = 0; // time for next update +volatile uint16_t tic_cnt = 0; + +// To calculate a running average over 10 sec, we keep tic counts in 250ms intervals and add all 40 tic_buf values +#define TICBUFSIZE 40 // running average buffer size +volatile uint16_t tic_buf[TICBUFSIZE]; // buffer for 40 times 250ms tick values (to have a running average for 10 seconds) +volatile uint16_t tic_buf_cnt = 0; + +// In order to display a history of the last 7 minutes, we keep the last 50 values of 10sec tics +#define SEC10BUFSIZE 50 // history array for displaymode==true +volatile uint16_t sec10 = 0; // every 10 seconds counter +volatile uint16_t sec10_buf[SEC10BUFSIZE]; // buffer to hold 10 sec history (40*10 = 400 seconds) +volatile bool sec10updated = false; // set to true when sec10_buf is updated + + +// ######################################################################### +// Interrupt routine called on each click from the geiger tube +// +void IRAM_ATTR TicISR() { + tic_cnt++; +} + +// ######################################################################### +// Interrupt timer routine called every 250 ms +// +void IRAM_ATTR onTimer() { + tic_buf[tic_buf_cnt++] = tic_cnt; + tic_cnt = 0; + if (tic_buf_cnt>=TICBUFSIZE) { + uint16_t tot = 0; + for (int i=0; i=SEC10BUFSIZE) sec10 = 0; + sec10updated = true; + } +} + + // Convert tics to mR/hr +float tics2mrem(uint16_t tics) { + return float(tics) * TICFACTOR; +} + + void geigerInit() { + + Serial.begin(115200); // For debug + Serial.println("Geiger counter startup"); + updateTime = millis(); // Next update time + // attach interrupt routine to TIC interface from the geiger counter module + pinMode(PINTIC, INPUT); + attachInterrupt(PINTIC, TicISR, FALLING); + + // attach interrupt routine to internal timer, to fire every 250 ms + timer = timerBegin(0, 80, true); + timerAttachInterrupt(timer, &onTimer, true); + timerAlarmWrite(timer, 250000, true); // 250 ms + timerAlarmEnable(timer); + Serial.println("Geiger counter ready"); +} + +void geigerLoop() { + + // curent mR/hr value to display - add all tics from walking average 10 seconds + uint16_t v=0; + for (int i=0; i, +// +// : should be an integer type large enough to fit the current sample variable +// : must be large enough to contain the sum of filter_length samples, each of type +// +// example: MovingSum adc +// MovingSum adc +// MovingSum adc +// ---------------------------------------------------------------------------------------------------------------------------------------- + +template +class MovingSum +{ + +public: + + MovingSum(unsigned short _filter_length=DEFAULT_FILTER_LENGTH); + ~MovingSum(); + + void add(MA_dt x); + void clear(void); + MA_dt* getData(void); + MA_st getCurrentSum(void) const; + unsigned short getFilterLength(void) const; + unsigned short getCurrentFilterLength(void) const; + +private: + + MA_st sum; // sum of current samples + MA_dt *data; // vector with raw data + unsigned short index; // index of current sample + unsigned short filter_length; // length of the filter + bool filter_complete; // true when there are filter_length samples + + void init(); + +}; + +// ---------------------------------------------------------------------------------------------------------------------------------------- +// constructor - Creates a new instance of MovingSum with a default or a given filter length. +// ---------------------------------------------------------------------------------------------------------------------------------------- + +template +MovingSum::MovingSum(unsigned short _filter_length) +{ + filter_length = _filter_length; + + init(); +} + +// ---------------------------------------------------------------------------------------------------------------------------------------- +// destructor - releases the memory objects associated with the current MovingSum instance. +// ---------------------------------------------------------------------------------------------------------------------------------------- + +template +MovingSum::~MovingSum() +{ + delete[] data; +} + +// ---------------------------------------------------------------------------------------------------------------------------------------- +// init - initialize the class and allocate the required memory space +// ---------------------------------------------------------------------------------------------------------------------------------------- + +template +void MovingSum::init(void) +{ + sum = 0; + index = -1; + filter_complete = false; + + data = new MA_dt[filter_length]; + + clear(); +} + +// ---------------------------------------------------------------------------------------------------------------------------------------- +// clear - clears the vector of data by setting it to zero. +// ---------------------------------------------------------------------------------------------------------------------------------------- + +template +void MovingSum::clear(void) +{ + for(unsigned short i=0; i +void MovingSum::add(MA_dt x) +{ + index = (index + 1) % filter_length; + sum -= data[index]; + data[index] = x; + sum += x; + + if (!filter_complete && index==filter_length-1){ + filter_complete = true; + } + +} + +// ---------------------------------------------------------------------------------------------------------------------------------------- +// getCurrentSum - returns the current sum as updated after the invocation of MovingSum::add(MA_dt). +// ---------------------------------------------------------------------------------------------------------------------------------------- + +template +MA_st MovingSum::getCurrentSum(void) const +{ + return sum; +} + +// ---------------------------------------------------------------------------------------------------------------------------------------- +// getData - returns the raw data that are currently stored in an internal vector. +// ---------------------------------------------------------------------------------------------------------------------------------------- + +template +MA_dt* MovingSum::getData(void) +{ + return data; +} + +// ---------------------------------------------------------------------------------------------------------------------------------------- +// getFilterLength - returns the Filter's Length. +// ---------------------------------------------------------------------------------------------------------------------------------------- + +template +unsigned short MovingSum::getFilterLength(void) const +{ + return filter_length; +} + +// ---------------------------------------------------------------------------------------------------------------------------------------- +// getCurrentFilterLength - returns the current Filter's Length. +// ---------------------------------------------------------------------------------------------------------------------------------------- + +template +unsigned short MovingSum::getCurrentFilterLength(void) const +{ + return filter_complete ? filter_length : (index+1); +} + +#endif + +// ---------------------------------------------------------------------------------------------------------------------------------------- +// END +// ---------------------------------------------------------------------------------------------------------------------------------------- diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 2b7bb71d..21c817d5 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -24,6 +24,24 @@ int sensors_device_types[] = { SENSORS_TYPES }; uint8_t sensors_registered [SCOUNT]; +// ######################################################################### + +#ifdef CAJOE_GEIGER +#ifdef ESP32 + + // variables shared between main code and interrupt code + hw_timer_t* geiger_timer = NULL; + portMUX_TYPE* geiger_timerMux = NULL; + + float uSvh = 0.0f; + uint32_t tics_cpm = 0U; // tics in last 60s + uint16_t tics_cnt = 0U; // tics in 250ms x2 + uint32_t tics_tot = 0U; // total tics since boot x2 + MovingSum* cajoe_fms; + +#endif +#endif + /*********************************************************************************** * P U B L I C M E T H O D S * *********************************************************************************/ @@ -60,8 +78,8 @@ bool Sensors::readAllSensors() { DEBUG("-->[SLIB] UART data ready \t:", dataReady ? "true" : "false"); } enableWire1(); - // CO2scd30Read(); - // GCJA5Read(); + CO2scd30Read(); + GCJA5Read(); sps30Read(); CO2scd4xRead(); am2320Read(); @@ -73,6 +91,10 @@ bool Sensors::readAllSensors() { dhtRead(); disableWire1(); +#ifdef CAJOE_GEIGER + geigerEvaluate(); +#endif + printValues(); printSensorsRegistered(devmode); printUnitsRegistered(devmode); @@ -106,9 +128,9 @@ void Sensors::init(int pms_type, int pms_rx, int pms_tx) { DEBUG("-->[SLIB] UART sensors detected\t:", "0"); } startI2C(); - // CO2scd30Init(); + CO2scd30Init(); sps30I2CInit(); - // GCJA5Init(); + GCJA5Init(); CO2scd4xInit(); bme680Init(); bmp280Init(); @@ -117,6 +139,9 @@ void Sensors::init(int pms_type, int pms_rx, int pms_tx) { sht31Init(); aht10Init(); dhtInit(); +#ifdef CAJOE_GEIGER + geigerInit(); +#endif printSensorsRegistered(true); } @@ -559,6 +584,14 @@ float Sensors::getUnitValue(UNIT unit) { return alt; case GAS: return gas; + +#ifdef CAJOE_GEIGER + case CPM: + return tics_cpm; + case RADIATION: + return uSvh; +#endif + default: return 0.0; } @@ -1571,6 +1604,13 @@ void Sensors::resetAllVariables() { alt = 0.0; gas = 0.0; pres = 0.0; + +#ifdef CAJOE_GEIGER + if (cajoe_fms != NULL) cajoe_fms->clear(); + tics_cpm = 0; + uSvh = 0.0; +#endif + } void Sensors::DEBUG(const char *text, const char *textb) { @@ -1717,3 +1757,162 @@ bool Sensors::serialInit(int pms_type, unsigned long speed_baud, int pms_rx, int #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SENSORSHANDLER) Sensors sensors; #endif + +// ######################################################################### +// Interrupt routine called on each click from the geiger tube +// WARNING! the ISR is actually called on both the rising and the falling edge even if configure for FALLING or RISING + +#ifdef CAJOE_GEIGER +#ifdef ESP32 + + void IRAM_ATTR GeigerTicISR() { + + portENTER_CRITICAL_ISR(geiger_timerMux); + tics_cnt++; // tics in 250ms x2 + tics_tot++; // total tics since boot x2 + portEXIT_CRITICAL_ISR(geiger_timerMux); + } + +#endif +#endif + +// ######################################################################### +// Interrupt timer routine called every 250 ms + +#ifdef CAJOE_GEIGER +#ifdef ESP32 + + void IRAM_ATTR onGeigerTimer() { + + portENTER_CRITICAL_ISR(geiger_timerMux); + cajoe_fms->add(tics_cnt / 2); // tics are counted twice... on falling and rising edges... + tics_cnt = 0; + portEXIT_CRITICAL_ISR(geiger_timerMux); + } + +#endif +#endif + +// ######################################################################### +// Converts CPM to uSv/h units (J305 tube) + +#ifdef CAJOE_GEIGER + +float Sensors::CPM2uSvh(uint32_t cpm) { + + return float(cpm) * J305_CONV_FACTOR; +} + +#endif + +// ######################################################################### +// Initialize Geiger counter + +#ifdef CAJOE_GEIGER + +void Sensors::geigerInit() { + + tics_cnt = 0U; // tics in 250ms x2 + tics_tot = 0U; // total tics since boot x2 + + geiger_timer = NULL; + geiger_timerMux = new portMUX_TYPE(portMUX_INITIALIZER_UNLOCKED); + +// moving sum for CAJOE Geiger Counter, configured for 240 samples (1 sample every 250 ms * 240 samples = 60000ms = 60s) + cajoe_fms = new MovingSum(GEIGER_BUFSIZE); + +#ifdef ESP32 + + Serial.println("-->[SLIB] Geiger counter startup"); + + sensorRegister(SENSORS::SCAJOE); + unitRegister(UNIT::CPM); + unitRegister(UNIT::RADIATION); + +// attach interrupt routine to the GPI connected to the Geiger counter module + pinMode(GEIGER_PINTIC, INPUT); +// attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, RISING); // counted twice (unexpected) +// attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING);// counted twice (unexpected) + attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, CHANGE); // counted twice (as expected) + +// attach interrupt routine to internal timer, to fire every 250 ms + geiger_timer = timerBegin(GEIGER_TIMER, 80, true); + timerAttachInterrupt(geiger_timer, &onGeigerTimer, true); + timerAlarmWrite(geiger_timer, 250000, true); // 250 ms + timerAlarmEnable(geiger_timer); + Serial.println("-->[SLIB] Geiger counter ready"); + +#else + + Serial.println("-->[SLIB] GEIGER counter not yet supported on this platform..."); + +#endif +} + +#endif + +// ######################################################################### +// Geiger loop +// CAJOE kit comes with a Chinese J305 geiger tube +// Conversion Factor used for conversion from CPM to uSv/h units is 0.008120370 (J305 tube) + +#ifdef CAJOE_GEIGER + +void Sensors::geigerEvaluate() { + + #ifdef ESP32 + + bool ready; + uint32_t tics_len; + + portENTER_CRITICAL(geiger_timerMux); + tics_cpm = cajoe_fms->getCurrentSum(); + tics_len = cajoe_fms->getCurrentFilterLength(); + portEXIT_CRITICAL(geiger_timerMux); + +// wait until the moving sum is full and then + ready = (tics_len == cajoe_fms->getFilterLength()); +// convert CPM (tics in last minute) to uSv/h and put in display buffer for TFT +// moving sum buffer size is 240 (1 sample every 250 ms * 240 samples = 60000ms = 60s): the complete sum cover exactly last 60s + if (ready){ + uSvh = CPM2uSvh(tics_cpm); + }else{ + uSvh = 0.0; + } + + Serial.print("-->[SLIB] tTOT: "); Serial.println(tics_tot / 2); // tics are counted twice... on falling and rising edges... + Serial.print("-->[SLIB] tLEN: "); Serial.print (tics_len); Serial.println(ready ? " (ready)" : " (not ready)"); + Serial.print("-->[SLIB] tCPM: "); Serial.println(tics_cpm); + Serial.print("-->[SLIB] uSvh: "); Serial.println(uSvh); + Serial.println(); + +#else + + Serial.println("GEIGER counter not yet supported on this platform..."); + +#endif +} + +#endif + +// ######################################################################### + +#ifdef CAJOE_GEIGER + +uint32_t Sensors::getGeigerCPM(void) { + + return tics_cpm; +} + +#endif + +// ######################################################################### + +#ifdef CAJOE_GEIGER + +float Sensors::getGeigerMicroSievertHour(void) { + + return uSvh; +} + +#endif diff --git a/src/Sensors.hpp b/src/Sensors.hpp index ca0bda3f..74dbf6c5 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -17,9 +17,28 @@ #include #include +#define CAJOE_GEIGER + +#ifdef CAJOE_GEIGER +#include "MovingSum.h" +#endif + #define CSL_VERSION "0.5.4" #define CSL_REVISION 357 +/************************************************************** + * GEIGER + * ************************************************************/ + +#ifdef CAJOE_GEIGER +#ifdef ESP32 +#define GEIGER_TIMER 1 // timer0 is already used somewhere ??? +#define GEIGER_PINTIC 26 // GPIO27 is busy (used as sensor(s) enable) +#define GEIGER_BUFSIZE 240 // moving sum buffer size (1 sample every 250 ms * 240 samples = 60000ms = 60s) +#define J305_CONV_FACTOR 0.008120370 // conversion Factor used for conversion from CPM to uSv/h units (J305 tube) +#endif +#endif + /*************************************************************** * S E T U P E S P 3 2 B O A R D S A N D F I E L D S ***************************************************************/ @@ -98,6 +117,8 @@ X(PRESS, "hPa", "P") \ X(ALT, "m", "Alt") \ X(GAS, "Ohm", "Gas") \ + X(CPM, "CPM", "CPM") \ + X(RADIATION, "uSv/h", "Radiation") \ X(UCOUNT, "COUNT", "UCOUNT") #define X(unit, symbol, name) unit, @@ -121,7 +142,8 @@ typedef enum UNIT : size_t { SENSOR_UNITS } UNIT; X(SAHT10, "AHT10", 3) \ X(SAM232X, "AM232X", 3) \ X(SDHTX, "DHTX", 3) \ - X(SCOUNT, "SCOUNT", 3) + X(SCAJOE, "CAJOE", 4) \ + X(SCOUNT, "SCOUNT", 3) #define X(utype, uname, umaintype) utype, typedef enum SENSORS : size_t { SENSORS_TYPES } SENSORS; // backward compatibility @@ -131,7 +153,9 @@ typedef enum SENSORS : size_t { SENSORS_TYPES } SENSORS; // backward compatibil enum class SensorGroup { SENSOR_NONE, SENSOR_PM, SENSOR_CO2, - SENSOR_ENV }; + SENSOR_ENV, + SENSOR_RAD // CAJOE_GEIGER + }; typedef void (*errorCbFn)(const char *msg); typedef void (*voidCbFn)(); @@ -248,6 +272,11 @@ class Sensors { float getGas(); +#ifdef CAJOE_GEIGER + uint32_t getGeigerCPM(void); + float getGeigerMicroSievertHour(void); +#endif + void setTempOffset(float offset); void setCO2AltitudeOffset(float altitude); @@ -392,6 +421,14 @@ class Sensors { void dhtRead(); bool dhtIsReady(float *temperature, float *humidity); + // Geiger methods: + +#ifdef CAJOE_GEIGER + void geigerInit(); + void geigerEvaluate(); + float CPM2uSvh(uint32_t cpm); +#endif + // UART sensors methods: bool sensorSerialInit(int pms_type, int rx, int tx); @@ -455,3 +492,4 @@ extern Sensors sensors; #endif #endif + \ No newline at end of file From 5f428337f3bbee3b25476a84816aa7302ebe2504 Mon Sep 17 00:00:00 2001 From: iw2lsi Date: Thu, 9 Jun 2022 10:04:59 +0200 Subject: [PATCH 09/30] code cleanup --- src/Sensors.hpp | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/Sensors.hpp b/src/Sensors.hpp index 1383b15b..f2b281e0 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -26,18 +26,6 @@ #define CSL_VERSION "0.5.6" #define CSL_REVISION 361 -/************************************************************** - * GEIGER - * ************************************************************/ -// Define pin for tics from geiger counter -#ifdef ESP8266 -#define PINTIC D5 -#else -#define PINTIC 27 -#endif -#define TICFACTOR 0.05 // factor between number of tics/second --> mR/hr -#define LOG_PERIOD 10 // for esp8266 variant - /************************************************************** * GEIGER * ************************************************************/ @@ -46,10 +34,13 @@ #ifdef ESP32 #define GEIGER_TIMER 1 // timer0 is already used somewhere ??? #define GEIGER_PINTIC 26 // GPIO27 is busy (used as sensor(s) enable) +#else +#define GEIGER_TIMER 0 // timer0 was used +#define GEIGER_PINTIC D5 // +#endif #define GEIGER_BUFSIZE 240 // moving sum buffer size (1 sample every 250 ms * 240 samples = 60000ms = 60s) #define J305_CONV_FACTOR 0.008120370 // conversion Factor used for conversion from CPM to uSv/h units (J305 tube) #endif -#endif /*************************************************************** * S E T U P E S P 3 2 B O A R D S A N D F I E L D S @@ -156,7 +147,6 @@ typedef enum UNIT : size_t { SENSOR_UNITS } UNIT; X(SDHTX, "DHTX", 3) \ X(SCAJOE, "CAJOE", 4) \ X(SCOUNT, "SCOUNT", 3) - //X(SRADIATION, "CAJOE", 2) */ #define X(utype, uname, umaintype) utype, typedef enum SENSORS : size_t { SENSORS_TYPES } SENSORS; // backward compatibility @@ -491,9 +481,6 @@ class Sensors { void unitRegister(UNIT unit); uint8_t *getUnitsRegistered(); - - // void geigerInit(); - // void geigerLoop(); // @todo use DEBUG_ESP_PORT ? #ifdef WM_DEBUG_PORT @@ -508,4 +495,4 @@ extern Sensors sensors; #endif #endif - + \ No newline at end of file From 872317ad4ab98275a4e30906742c08b7ceea4856 Mon Sep 17 00:00:00 2001 From: iw2lsi Date: Thu, 9 Jun 2022 15:22:33 +0200 Subject: [PATCH 10/30] moving sum is now evaluated on a 60s timeline, 1 sample every second; WIP on ESP8266 --- src/Sensors.cpp | 114 +++++++++++++++++++++++++++++++----------------- src/Sensors.hpp | 9 ++-- 2 files changed, 77 insertions(+), 46 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index d46a5b6c..181eea1c 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -25,22 +25,22 @@ int sensors_device_types[] = { SENSORS_TYPES }; uint8_t sensors_registered [SCOUNT]; // ######################################################################### +// variables shared between main code and interrupt code #ifdef CAJOE_GEIGER -#ifdef ESP32 - // variables shared between main code and interrupt code +#ifdef ESP32 hw_timer_t* geiger_timer = NULL; portMUX_TYPE* geiger_timerMux = NULL; +#endif float uSvh = 0.0f; uint32_t tics_cpm = 0U; // tics in last 60s - uint16_t tics_cnt = 0U; // tics in 250ms x2 + uint16_t tics_cnt = 0U; // tics in 1000ms x2 uint32_t tics_tot = 0U; // total tics since boot x2 MovingSum* cajoe_fms; #endif -#endif /*********************************************************************************** * P U B L I C M E T H O D S @@ -267,7 +267,7 @@ uint16_t Sensors::getPM1() { /// @deprecated get PM1.0 ug/m3 formated value String Sensors::getStringPM1() { - char output[5]; + char output[6]; sprintf(output, "%03d", getPM1()); return String(output); } @@ -279,7 +279,7 @@ uint16_t Sensors::getPM25() { /// @deprecated get PM2.5 ug/m3 formated value String Sensors::getStringPM25() { - char output[5]; + char output[6]; sprintf(output, "%03d", getPM25()); return String(output); } @@ -291,7 +291,7 @@ uint16_t Sensors::getPM4() { /// @deprecated get PM4 ug/m3 formated value String Sensors::getStringPM4() { - char output[5]; + char output[6]; sprintf(output, "%03d", getPM4()); return String(output); } @@ -303,7 +303,7 @@ uint16_t Sensors::getPM10() { /// @deprecated get PM10 ug/m3 formated value String Sensors::getStringPM10() { - char output[5]; + char output[6]; sprintf(output, "%03d", getPM10()); return String(output); } @@ -315,7 +315,7 @@ uint16_t Sensors::getCO2() { /// @deprecated get CO2 ppm formated value String Sensors::getStringCO2() { - char output[5]; + char output[6]; sprintf(output, "%04d", getCO2()); return String(output); } @@ -1763,32 +1763,40 @@ Sensors sensors; // WARNING! the ISR is actually called on both the rising and the falling edge even if configure for FALLING or RISING #ifdef CAJOE_GEIGER -#ifdef ESP32 +#ifdef ESP32 void IRAM_ATTR GeigerTicISR() { +#else + IRAM_ATTR static void GeigerTicISR() { +#endif +#ifdef ESP32 portENTER_CRITICAL_ISR(geiger_timerMux); - tics_cnt++; // tics in 250ms x2 +#endif + + tics_cnt++; // tics in 1000ms x2 tics_tot++; // total tics since boot x2 - portEXIT_CRITICAL_ISR(geiger_timerMux); - } +#ifdef ESP32 + portEXIT_CRITICAL_ISR(geiger_timerMux); #endif +} + #endif // ######################################################################### -// Interrupt timer routine called every 250 ms +// Interrupt timer routine called every 1000 ms #ifdef CAJOE_GEIGER #ifdef ESP32 - void IRAM_ATTR onGeigerTimer() { +void IRAM_ATTR onGeigerTimer() { - portENTER_CRITICAL_ISR(geiger_timerMux); - cajoe_fms->add(tics_cnt / 2); // tics are counted twice... on falling and rising edges... - tics_cnt = 0; - portEXIT_CRITICAL_ISR(geiger_timerMux); - } + portENTER_CRITICAL_ISR(geiger_timerMux); + cajoe_fms->add(tics_cnt / 2); // tics are counted twice... on falling and rising edges... + tics_cnt = 0; + portEXIT_CRITICAL_ISR(geiger_timerMux); +} #endif #endif @@ -1812,68 +1820,95 @@ float Sensors::CPM2uSvh(uint32_t cpm) { void Sensors::geigerInit() { - tics_cnt = 0U; // tics in 250ms x2 + tics_cnt = 0U; // tics in 1000ms x2 tics_tot = 0U; // total tics since boot x2 - + +#ifdef ESP32 geiger_timer = NULL; geiger_timerMux = new portMUX_TYPE(portMUX_INITIALIZER_UNLOCKED); +#endif -// moving sum for CAJOE Geiger Counter, configured for 240 samples (1 sample every 250 ms * 240 samples = 60000ms = 60s) +// moving sum for CAJOE Geiger Counter, configured for 60 samples (1 sample every 1s * 60 samples = 60s) cajoe_fms = new MovingSum(GEIGER_BUFSIZE); -#ifdef ESP32 - Serial.println("-->[SLIB] Geiger counter startup"); sensorRegister(SENSORS::SCAJOE); unitRegister(UNIT::CPM); unitRegister(UNIT::RADIATION); +#ifdef ESP32 + // attach interrupt routine to the GPI connected to the Geiger counter module pinMode(GEIGER_PINTIC, INPUT); // attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, RISING); // counted twice (unexpected) // attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING);// counted twice (unexpected) attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, CHANGE); // counted twice (as expected) -// attach interrupt routine to internal timer, to fire every 250 ms +// attach interrupt routine to internal timer, to fire every 1000 ms geiger_timer = timerBegin(GEIGER_TIMER, 80, true); timerAttachInterrupt(geiger_timer, &onGeigerTimer, true); - timerAlarmWrite(geiger_timer, 250000, true); // 250 ms + timerAlarmWrite(geiger_timer, 1000000, true); // 1000 ms timerAlarmEnable(geiger_timer); - Serial.println("-->[SLIB] Geiger counter ready"); #else - Serial.println("-->[SLIB] GEIGER counter not yet supported on this platform..."); + pinMode(GEIGER_PINTIC, INPUT); + + attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); // WARNING! not sure if it's counted twice as on ESP32 or not... #endif + + Serial.println("-->[SLIB] Geiger counter ready"); } #endif // ######################################################################### -// Geiger loop +// Geiger counts evaluation // CAJOE kit comes with a Chinese J305 geiger tube -// Conversion Factor used for conversion from CPM to uSv/h units is 0.008120370 (J305 tube) +// Conversion factor used for conversion from CPM to uSv/h is 0.008120370 (J305 tube) #ifdef CAJOE_GEIGER void Sensors::geigerEvaluate() { - #ifdef ESP32 - bool ready; uint32_t tics_len; +#ifdef ESP32 + portENTER_CRITICAL(geiger_timerMux); tics_cpm = cajoe_fms->getCurrentSum(); tics_len = cajoe_fms->getCurrentFilterLength(); portEXIT_CRITICAL(geiger_timerMux); -// wait until the moving sum is full and then +#else + + unsigned long int second; + unsigned long int secidx; + static unsigned long int secidx_prev = 0; + + second = millis() / 1000; + secidx = second % 60; + +// update the moving sum every second + if (secidx != secidx_prev){ + cajoe_fms->add(tics_cnt / 2); // WARNING! tics are counted twice... also on ESP8266 ??? needs to be checked... + secidx_prev = secidx; + tics_cnt = 0; + } + + tics_cpm = cajoe_fms->getCurrentSum(); + tics_len = cajoe_fms->getCurrentFilterLength(); + +#endif + +// check whether the moving sum is full ready = (tics_len == cajoe_fms->getFilterLength()); + // convert CPM (tics in last minute) to uSv/h and put in display buffer for TFT -// moving sum buffer size is 240 (1 sample every 250 ms * 240 samples = 60000ms = 60s): the complete sum cover exactly last 60s +// moving sum buffer size is 60 (1 sample every 1000 ms * 60 samples): the complete sum cover exactly last 60s if (ready){ uSvh = CPM2uSvh(tics_cpm); }else{ @@ -1884,13 +1919,10 @@ void Sensors::geigerEvaluate() { Serial.print("-->[SLIB] tLEN: "); Serial.print (tics_len); Serial.println(ready ? " (ready)" : " (not ready)"); Serial.print("-->[SLIB] tCPM: "); Serial.println(tics_cpm); Serial.print("-->[SLIB] uSvh: "); Serial.println(uSvh); - Serial.println(); - -#else - - Serial.println("GEIGER counter not yet supported on this platform..."); -#endif +// WARNING! does this actually print on TFT ??? + Serial.print("uSvh: "); Serial.println(uSvh); + Serial.print("cpm: "); Serial.println(tics_cpm); } #endif diff --git a/src/Sensors.hpp b/src/Sensors.hpp index f2b281e0..69fb9ee7 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -32,14 +32,13 @@ #ifdef CAJOE_GEIGER #ifdef ESP32 -#define GEIGER_TIMER 1 // timer0 is already used somewhere ??? +#define GEIGER_TIMER 1 // timer0 is already used (at least on TTGO-TDisplay) somewhere ??? #define GEIGER_PINTIC 26 // GPIO27 is busy (used as sensor(s) enable) #else -#define GEIGER_TIMER 0 // timer0 was used -#define GEIGER_PINTIC D5 // +#define GEIGER_PINTIC D5 // #endif -#define GEIGER_BUFSIZE 240 // moving sum buffer size (1 sample every 250 ms * 240 samples = 60000ms = 60s) -#define J305_CONV_FACTOR 0.008120370 // conversion Factor used for conversion from CPM to uSv/h units (J305 tube) +#define GEIGER_BUFSIZE 60 // moving sum buffer size (1 sample every 1s * 60 samples = 60s) +#define J305_CONV_FACTOR 0.008120370 // conversion factor used for conversion from CPM to uSv/h units (J305 tube) #endif /*************************************************************** From 9610490acb31c22da89955c4581c19899a72075d Mon Sep 17 00:00:00 2001 From: iw2lsi Date: Thu, 9 Jun 2022 17:15:03 +0200 Subject: [PATCH 11/30] CAJOE example reviewed --- examples/Radiation_CAJOE/Radiation_CAJOE.ino | 122 +++---------------- 1 file changed, 14 insertions(+), 108 deletions(-) diff --git a/examples/Radiation_CAJOE/Radiation_CAJOE.ino b/examples/Radiation_CAJOE/Radiation_CAJOE.ino index 166b3053..39408d00 100644 --- a/examples/Radiation_CAJOE/Radiation_CAJOE.ino +++ b/examples/Radiation_CAJOE/Radiation_CAJOE.ino @@ -1,10 +1,9 @@ /** * @file main.cpp - * @date June 2018 - 2021 + * @author Antonio Vanegas @hpsaturn + * @date June 2018 - 2022 * @brief Radiation sensor example * @license GPL3 - * - * @license GPL3 * * Full documentation: * https://github.com/kike-canaries/canairio_sensorlib#canairio-air-quality-sensors-library @@ -14,10 +13,13 @@ * * CanAirIO project: * https://canair.io + * + * CanAirIO Docs: + * https://canair.io/docs + * */ #include - #include void onSensorDataOk() { @@ -39,17 +41,18 @@ void onSensorDataError(const char* msg) { void setup() { Serial.begin(115200); - delay(200); - Serial.println("\n== Sensor test setup ==\n"); + delay(1000); + Serial.println("\n== Sensor test setup ==\n"); Serial.println("-->[SETUP] Detecting sensors.."); - sensors.setSampleTime(5); // config sensors sample time interval - sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback - sensors.setOnErrorCallBack(&onSensorDataError); // [optional] error callback - sensors.setDebugMode(true); // [optional] debug mode + sensors.setSampleTime(5); // config sensors sample time interval + sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback + sensors.setOnErrorCallBack(&onSensorDataError); // [optional] error callback + sensors.setDebugMode(true); // [optional] debug mode + sensors.detectI2COnly(true); // [optional] skip UART detection - sensors.init(); // forced UAQ sensor. Empty for auto detection + sensors.init(); // forced UAQ sensor. Empty for auto detection delay(500); } @@ -57,102 +60,5 @@ void setup() { void loop() { sensors.loop(); // read sensor data and showed it } -// *************** GEIGER **************** -// *************************************** - - - -// variables shared between main code and interrupt code -hw_timer_t * timer = NULL; -volatile uint32_t updateTime = 0; // time for next update -volatile uint16_t tic_cnt = 0; - -// To calculate a running average over 10 sec, we keep tic counts in 250ms intervals and add all 40 tic_buf values -#define TICBUFSIZE 40 // running average buffer size -volatile uint16_t tic_buf[TICBUFSIZE]; // buffer for 40 times 250ms tick values (to have a running average for 10 seconds) -volatile uint16_t tic_buf_cnt = 0; - -// In order to display a history of the last 7 minutes, we keep the last 50 values of 10sec tics -#define SEC10BUFSIZE 50 // history array for displaymode==true -volatile uint16_t sec10 = 0; // every 10 seconds counter -volatile uint16_t sec10_buf[SEC10BUFSIZE]; // buffer to hold 10 sec history (40*10 = 400 seconds) -volatile bool sec10updated = false; // set to true when sec10_buf is updated - - -// ######################################################################### -// Interrupt routine called on each click from the geiger tube -// -void IRAM_ATTR TicISR() { - tic_cnt++; -} - -// ######################################################################### -// Interrupt timer routine called every 250 ms -// -void IRAM_ATTR onTimer() { - tic_buf[tic_buf_cnt++] = tic_cnt; - tic_cnt = 0; - if (tic_buf_cnt>=TICBUFSIZE) { - uint16_t tot = 0; - for (int i=0; i=SEC10BUFSIZE) sec10 = 0; - sec10updated = true; - } -} - - // Convert tics to mR/hr -float tics2mrem(uint16_t tics) { - return float(tics) * TICFACTOR; -} - - void geigerInit() { - - Serial.begin(115200); // For debug - Serial.println("Geiger counter startup"); - updateTime = millis(); // Next update time - // attach interrupt routine to TIC interface from the geiger counter module - pinMode(PINTIC, INPUT); - attachInterrupt(PINTIC, TicISR, FALLING); - - // attach interrupt routine to internal timer, to fire every 250 ms - timer = timerBegin(0, 80, true); - timerAttachInterrupt(timer, &onTimer, true); - timerAlarmWrite(timer, 250000, true); // 250 ms - timerAlarmEnable(timer); - Serial.println("Geiger counter ready"); -} - -void geigerLoop() { - - // curent mR/hr value to display - add all tics from walking average 10 seconds - uint16_t v=0; - for (int i=0; i Date: Thu, 9 Jun 2022 20:41:38 +0200 Subject: [PATCH 12/30] added new example test and changed src_filter directive to new one --- platformio.ini | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index 03faa573..a77c5128 100644 --- a/platformio.ini +++ b/platformio.ini @@ -65,19 +65,23 @@ lib_deps = [env:esp8266full] extends = esp8266_common -src_filter = -<*> + +build_src_filter = -<*> + [env:esp32sps30i2c] extends = esp32_common -src_filter = -<*> + +build_src_filter = -<*> + [env:esp32full] extends = esp32_common -src_filter = -<*> + +build_src_filter = -<*> + [env:esp32] extends = esp32_common -src_filter = -<*> + +build_src_filter = -<*> + + +[env:esp32_radiation] +extends = esp32_common +build_src_filter = -<*> + ; [env:teensy36] ; platform = teensy From 92422b5fcbd51a25b3be3f1965c276b0eef934f1 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Thu, 9 Jun 2022 20:42:12 +0200 Subject: [PATCH 13/30] renamed the basic example for radiation_CAJOE --- .../Radiation_CAJOE.ino => radiation_CAJOE/radiation_CAJOE.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{Radiation_CAJOE/Radiation_CAJOE.ino => radiation_CAJOE/radiation_CAJOE.cpp} (100%) diff --git a/examples/Radiation_CAJOE/Radiation_CAJOE.ino b/examples/radiation_CAJOE/radiation_CAJOE.cpp similarity index 100% rename from examples/Radiation_CAJOE/Radiation_CAJOE.ino rename to examples/radiation_CAJOE/radiation_CAJOE.cpp From 0a44fd216b53e1f837294f9e9ccdf48226bb5f42 Mon Sep 17 00:00:00 2001 From: iw2lsi Date: Fri, 10 Jun 2022 15:44:52 +0200 Subject: [PATCH 14/30] ESP8266: GEIGER_PINTIC temporarily configured as INPUT_PULLUP; ESP8266 fixed moving-sum evaluation in case geigerEvaluate() is called w/ a freq < 1Hz --- src/Sensors.cpp | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 181eea1c..917cce9e 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -1853,7 +1853,7 @@ void Sensors::geigerInit() { #else - pinMode(GEIGER_PINTIC, INPUT); + pinMode(GEIGER_PINTIC, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); // WARNING! not sure if it's counted twice as on ESP32 or not... @@ -1886,19 +1886,34 @@ void Sensors::geigerEvaluate() { #else unsigned long int second; - unsigned long int secidx; + unsigned long int secidx_curr; static unsigned long int secidx_prev = 0; second = millis() / 1000; - secidx = second % 60; + secidx_curr = second % 60; + + Serial.print("-->[SLIB] sPREV: "); Serial.println(secidx_prev); + Serial.print("-->[SLIB] sCURR: "); Serial.println(secidx_curr); + +// exit in case we are called twice (or more) in a second... + if (secidx_curr == secidx_prev) return; -// update the moving sum every second - if (secidx != secidx_prev){ - cajoe_fms->add(tics_cnt / 2); // WARNING! tics are counted twice... also on ESP8266 ??? needs to be checked... - secidx_prev = secidx; - tics_cnt = 0; +// evaluate the number of missing samples in case geigerEvaluate() is not called every seconds... +// WARNING! we still expects to be called at least once a minute... + int delta = (secidx_prev < secidx_curr) ? secidx_curr-secidx_prev : 60-secidx_prev+secidx_curr; + +// zeroes the missing samples... if any... + for(int i=1; i[SLIB] add 0 @"); Serial.println((secidx_prev+i)%60); + cajoe_fms->add(0); } +// add last sample + Serial.print("-->[SLIB] add "); Serial.print(tics_cnt / 2); Serial.print(" @"); Serial.println(secidx_curr); + cajoe_fms->add(tics_cnt / 2); // WARNING! tics are counted twice... also on ESP8266 ??? needs to be checked... + secidx_prev = secidx_curr; + tics_cnt = 0; + tics_cpm = cajoe_fms->getCurrentSum(); tics_len = cajoe_fms->getCurrentFilterLength(); From d95aa6b97e5527fabe5a73985f9a6c30f513a376 Mon Sep 17 00:00:00 2001 From: Giampaolo Bellini Date: Mon, 13 Jun 2022 19:52:11 +0200 Subject: [PATCH 15/30] CAJOE Geiger counts fixed for ESP8266: pulses are counted onces (as expected) when the IRQ is configured for triggering on the falling edges. --- src/Sensors.cpp | 32 ++++++++++++++++---------------- src/Sensors.hpp | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 917cce9e..550d71f1 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -36,8 +36,8 @@ uint8_t sensors_registered [SCOUNT]; float uSvh = 0.0f; uint32_t tics_cpm = 0U; // tics in last 60s - uint16_t tics_cnt = 0U; // tics in 1000ms x2 - uint32_t tics_tot = 0U; // total tics since boot x2 + uint16_t tics_cnt = 0U; // tics in 1000ms (x1 on ESP8266, x2 on ESP32) + uint32_t tics_tot = 0U; // total tics since boot (x1 on ESP8266, x2 on ESP32) MovingSum* cajoe_fms; #endif @@ -1774,8 +1774,8 @@ Sensors sensors; portENTER_CRITICAL_ISR(geiger_timerMux); #endif - tics_cnt++; // tics in 1000ms x2 - tics_tot++; // total tics since boot x2 + tics_cnt++; // tics in 1000ms (x1 on ESP8266, x2 on ESP32) + tics_tot++; // total tics since boot (x1 on ESP8266, x2 on ESP32) #ifdef ESP32 portEXIT_CRITICAL_ISR(geiger_timerMux); @@ -1793,7 +1793,7 @@ Sensors sensors; void IRAM_ATTR onGeigerTimer() { portENTER_CRITICAL_ISR(geiger_timerMux); - cajoe_fms->add(tics_cnt / 2); // tics are counted twice... on falling and rising edges... + cajoe_fms->add(tics_cnt / 2); // on ESP32 tics are counted twice... on falling and rising edges... tics_cnt = 0; portEXIT_CRITICAL_ISR(geiger_timerMux); } @@ -1820,8 +1820,8 @@ float Sensors::CPM2uSvh(uint32_t cpm) { void Sensors::geigerInit() { - tics_cnt = 0U; // tics in 1000ms x2 - tics_tot = 0U; // total tics since boot x2 + tics_cnt = 0U; // tics in 1000ms (x1 on ESP8266, x2 on ESP32) + tics_tot = 0U; // total tics since boot (x1 on ESP8266, x2 on ESP32) #ifdef ESP32 geiger_timer = NULL; @@ -1853,9 +1853,9 @@ void Sensors::geigerInit() { #else - pinMode(GEIGER_PINTIC, INPUT_PULLUP); + pinMode(GEIGER_PINTIC, INPUT); - attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); // WARNING! not sure if it's counted twice as on ESP32 or not... + attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); // counted once (as expected) #endif @@ -1909,8 +1909,8 @@ void Sensors::geigerEvaluate() { } // add last sample - Serial.print("-->[SLIB] add "); Serial.print(tics_cnt / 2); Serial.print(" @"); Serial.println(secidx_curr); - cajoe_fms->add(tics_cnt / 2); // WARNING! tics are counted twice... also on ESP8266 ??? needs to be checked... + Serial.print("-->[SLIB] add "); Serial.print(tics_cnt); Serial.print(" @"); Serial.println(secidx_curr); + cajoe_fms->add(tics_cnt); secidx_prev = secidx_curr; tics_cnt = 0; @@ -1930,14 +1930,14 @@ void Sensors::geigerEvaluate() { uSvh = 0.0; } - Serial.print("-->[SLIB] tTOT: "); Serial.println(tics_tot / 2); // tics are counted twice... on falling and rising edges... +#ifdef ESP32 + Serial.print("-->[SLIB] tTOT: "); Serial.println(tics_tot / 2); // on ESP32 tics are counted twice... on falling and rising edges... +#else + Serial.print("-->[SLIB] tTOT: "); Serial.println(tics_tot); // on ESP8266 tics are counted only on the falling edge... +#endif Serial.print("-->[SLIB] tLEN: "); Serial.print (tics_len); Serial.println(ready ? " (ready)" : " (not ready)"); Serial.print("-->[SLIB] tCPM: "); Serial.println(tics_cpm); Serial.print("-->[SLIB] uSvh: "); Serial.println(uSvh); - -// WARNING! does this actually print on TFT ??? - Serial.print("uSvh: "); Serial.println(uSvh); - Serial.print("cpm: "); Serial.println(tics_cpm); } #endif diff --git a/src/Sensors.hpp b/src/Sensors.hpp index 69fb9ee7..60e98113 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -35,7 +35,7 @@ #define GEIGER_TIMER 1 // timer0 is already used (at least on TTGO-TDisplay) somewhere ??? #define GEIGER_PINTIC 26 // GPIO27 is busy (used as sensor(s) enable) #else -#define GEIGER_PINTIC D5 // +#define GEIGER_PINTIC D5 // tested on ESP8266 NodeMCU #endif #define GEIGER_BUFSIZE 60 // moving sum buffer size (1 sample every 1s * 60 samples = 60s) #define J305_CONV_FACTOR 0.008120370 // conversion factor used for conversion from CPM to uSv/h units (J305 tube) From c4991eb9e37b3cf6c0e766539888c46acc40863b Mon Sep 17 00:00:00 2001 From: Giampaolo Bellini Date: Tue, 14 Jun 2022 00:13:34 +0200 Subject: [PATCH 16/30] FIX compilation errors by forcing [esp32_common] platform = espressif32 @ 3.2.0 --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index a77c5128..fa26682f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -34,7 +34,7 @@ lib_deps = https://github.com/jcomas/CM1106_UART.git [esp32_common] -platform = espressif32 +platform = espressif32 @ 3.2.0 board = esp32dev framework = ${env.framework} upload_speed = ${env.upload_speed} From ade67b1793497abbfeeb346571a11250005e183e Mon Sep 17 00:00:00 2001 From: Giampaolo Bellini Date: Thu, 16 Jun 2022 00:19:11 +0200 Subject: [PATCH 17/30] double counting issue on TTGO-TDisplay not detected anymore w/ using a Schmitt Trigger (SN74LVC1G17) between CAJOE and ESP32: code cleanup. --- src/Sensors.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 550d71f1..673fca57 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -36,8 +36,8 @@ uint8_t sensors_registered [SCOUNT]; float uSvh = 0.0f; uint32_t tics_cpm = 0U; // tics in last 60s - uint16_t tics_cnt = 0U; // tics in 1000ms (x1 on ESP8266, x2 on ESP32) - uint32_t tics_tot = 0U; // total tics since boot (x1 on ESP8266, x2 on ESP32) + uint16_t tics_cnt = 0U; // tics in 1000ms + uint32_t tics_tot = 0U; // total tics since boot MovingSum* cajoe_fms; #endif @@ -1774,8 +1774,8 @@ Sensors sensors; portENTER_CRITICAL_ISR(geiger_timerMux); #endif - tics_cnt++; // tics in 1000ms (x1 on ESP8266, x2 on ESP32) - tics_tot++; // total tics since boot (x1 on ESP8266, x2 on ESP32) + tics_cnt++; // tics in 1000ms + tics_tot++; // total tics since boot #ifdef ESP32 portEXIT_CRITICAL_ISR(geiger_timerMux); @@ -1793,7 +1793,7 @@ Sensors sensors; void IRAM_ATTR onGeigerTimer() { portENTER_CRITICAL_ISR(geiger_timerMux); - cajoe_fms->add(tics_cnt / 2); // on ESP32 tics are counted twice... on falling and rising edges... + cajoe_fms->add(tics_cnt); tics_cnt = 0; portEXIT_CRITICAL_ISR(geiger_timerMux); } @@ -1820,8 +1820,8 @@ float Sensors::CPM2uSvh(uint32_t cpm) { void Sensors::geigerInit() { - tics_cnt = 0U; // tics in 1000ms (x1 on ESP8266, x2 on ESP32) - tics_tot = 0U; // total tics since boot (x1 on ESP8266, x2 on ESP32) + tics_cnt = 0U; // tics in 1000ms + tics_tot = 0U; // total tics since boot #ifdef ESP32 geiger_timer = NULL; @@ -1841,9 +1841,7 @@ void Sensors::geigerInit() { // attach interrupt routine to the GPI connected to the Geiger counter module pinMode(GEIGER_PINTIC, INPUT); -// attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, RISING); // counted twice (unexpected) -// attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING);// counted twice (unexpected) - attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, CHANGE); // counted twice (as expected) + attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); // attach interrupt routine to internal timer, to fire every 1000 ms geiger_timer = timerBegin(GEIGER_TIMER, 80, true); @@ -1855,7 +1853,7 @@ void Sensors::geigerInit() { pinMode(GEIGER_PINTIC, INPUT); - attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); // counted once (as expected) + attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); #endif @@ -1930,11 +1928,7 @@ void Sensors::geigerEvaluate() { uSvh = 0.0; } -#ifdef ESP32 - Serial.print("-->[SLIB] tTOT: "); Serial.println(tics_tot / 2); // on ESP32 tics are counted twice... on falling and rising edges... -#else - Serial.print("-->[SLIB] tTOT: "); Serial.println(tics_tot); // on ESP8266 tics are counted only on the falling edge... -#endif + Serial.print("-->[SLIB] tTOT: "); Serial.println(tics_tot); Serial.print("-->[SLIB] tLEN: "); Serial.print (tics_len); Serial.println(ready ? " (ready)" : " (not ready)"); Serial.print("-->[SLIB] tCPM: "); Serial.println(tics_cpm); Serial.print("-->[SLIB] uSvh: "); Serial.println(uSvh); From fbe2b23d2f19185232fe704582815c0f4ba29517 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Tue, 13 Sep 2022 11:10:38 +0200 Subject: [PATCH 18/30] set build flag for complete test of Geiger version --- platformio.ini | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 0a51ec1a..295b016a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -67,8 +67,11 @@ build_src_filter = -<*> + extends = esp32_common build_src_filter = -<*> + -[env:esp32_radiation] +[env:CAJOE_GEIGER] extends = esp32_common +build_flags = + ${env.build_flags} + -DCAJOE_GEIGER build_src_filter = -<*> + [env:atmelsam] From a9d2644844192224296b40fec6c542344d5546d2 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Thu, 6 Jul 2023 22:17:20 +0200 Subject: [PATCH 19/30] fixed issue on uncompleted sample for CAJOE --- examples/radiation_CAJOE/radiation_CAJOE.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/radiation_CAJOE/radiation_CAJOE.cpp b/examples/radiation_CAJOE/radiation_CAJOE.cpp index 39408d00..ae440529 100644 --- a/examples/radiation_CAJOE/radiation_CAJOE.cpp +++ b/examples/radiation_CAJOE/radiation_CAJOE.cpp @@ -23,7 +23,7 @@ #include void onSensorDataOk() { - Serial.print(" CO2: " + sensors.getStringCO2()); + Serial.print(" CO2: " + String(sensors.getCO2())); Serial.print(" CO2humi: " + String(sensors.getCO2humi())); Serial.print(" CO2temp: " + String(sensors.getCO2temp())); From 22243825f49b208e1329784af475f0a2d7ef95f2 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Thu, 6 Jul 2023 22:36:04 +0200 Subject: [PATCH 20/30] fixed issues with atmelsam architecture --- src/Sensors.cpp | 67 ------------------------------------------------- src/Sensors.hpp | 2 -- 2 files changed, 69 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 49af0d76..f135c98f 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -28,10 +28,8 @@ uint8_t sensors_registered [SCOUNT]; #ifdef CAJOE_GEIGER -#ifdef ESP32 hw_timer_t* geiger_timer = NULL; portMUX_TYPE* geiger_timerMux = NULL; -#endif float uSvh = 0.0f; uint32_t tics_cpm = 0U; // tics in last 60s @@ -1847,33 +1845,21 @@ Sensors sensors; // WARNING! the ISR is actually called on both the rising and the falling edge even if configure for FALLING or RISING #ifdef CAJOE_GEIGER - -#ifdef ESP32 void IRAM_ATTR GeigerTicISR() { -#else - IRAM_ATTR static void GeigerTicISR() { -#endif -#ifdef ESP32 portENTER_CRITICAL_ISR(geiger_timerMux); -#endif tics_cnt++; // tics in 1000ms tics_tot++; // total tics since boot -#ifdef ESP32 portEXIT_CRITICAL_ISR(geiger_timerMux); -#endif } - #endif // ######################################################################### // Interrupt timer routine called every 1000 ms #ifdef CAJOE_GEIGER -#ifdef ESP32 - void IRAM_ATTR onGeigerTimer() { portENTER_CRITICAL_ISR(geiger_timerMux); @@ -1881,8 +1867,6 @@ void IRAM_ATTR onGeigerTimer() { tics_cnt = 0; portEXIT_CRITICAL_ISR(geiger_timerMux); } - -#endif #endif // ######################################################################### @@ -1891,7 +1875,6 @@ void IRAM_ATTR onGeigerTimer() { #ifdef CAJOE_GEIGER float Sensors::CPM2uSvh(uint32_t cpm) { - return float(cpm) * J305_CONV_FACTOR; } @@ -1907,10 +1890,8 @@ void Sensors::geigerInit() { tics_cnt = 0U; // tics in 1000ms tics_tot = 0U; // total tics since boot -#ifdef ESP32 geiger_timer = NULL; geiger_timerMux = new portMUX_TYPE(portMUX_INITIALIZER_UNLOCKED); -#endif // moving sum for CAJOE Geiger Counter, configured for 60 samples (1 sample every 1s * 60 samples = 60s) cajoe_fms = new MovingSum(GEIGER_BUFSIZE); @@ -1921,8 +1902,6 @@ void Sensors::geigerInit() { unitRegister(UNIT::CPM); unitRegister(UNIT::RAD); -#ifdef ESP32 - // attach interrupt routine to the GPI connected to the Geiger counter module pinMode(GEIGER_PINTIC, INPUT); attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); @@ -1933,14 +1912,6 @@ void Sensors::geigerInit() { timerAlarmWrite(geiger_timer, 1000000, true); // 1000 ms timerAlarmEnable(geiger_timer); -#else - - pinMode(GEIGER_PINTIC, INPUT); - - attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); - -#endif - Serial.println("-->[SLIB] Geiger counter ready"); } @@ -1958,49 +1929,11 @@ void Sensors::geigerEvaluate() { bool ready; uint32_t tics_len; -#ifdef ESP32 - portENTER_CRITICAL(geiger_timerMux); tics_cpm = cajoe_fms->getCurrentSum(); tics_len = cajoe_fms->getCurrentFilterLength(); portEXIT_CRITICAL(geiger_timerMux); -#else - - unsigned long int second; - unsigned long int secidx_curr; - static unsigned long int secidx_prev = 0; - - second = millis() / 1000; - secidx_curr = second % 60; - - Serial.print("-->[SLIB] sPREV: "); Serial.println(secidx_prev); - Serial.print("-->[SLIB] sCURR: "); Serial.println(secidx_curr); - -// exit in case we are called twice (or more) in a second... - if (secidx_curr == secidx_prev) return; - -// evaluate the number of missing samples in case geigerEvaluate() is not called every seconds... -// WARNING! we still expects to be called at least once a minute... - int delta = (secidx_prev < secidx_curr) ? secidx_curr-secidx_prev : 60-secidx_prev+secidx_curr; - -// zeroes the missing samples... if any... - for(int i=1; i[SLIB] add 0 @"); Serial.println((secidx_prev+i)%60); - cajoe_fms->add(0); - } - -// add last sample - Serial.print("-->[SLIB] add "); Serial.print(tics_cnt); Serial.print(" @"); Serial.println(secidx_curr); - cajoe_fms->add(tics_cnt); - secidx_prev = secidx_curr; - tics_cnt = 0; - - tics_cpm = cajoe_fms->getCurrentSum(); - tics_len = cajoe_fms->getCurrentFilterLength(); - -#endif - // check whether the moving sum is full ready = (tics_len == cajoe_fms->getFilterLength()); diff --git a/src/Sensors.hpp b/src/Sensors.hpp index 093fe1e0..eda6d15c 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -18,8 +18,6 @@ #include #include -#define CAJOE_GEIGER - #ifdef CAJOE_GEIGER #include "MovingSum.h" #endif From 808dc88b08c6166310d5cfdb7b6bef37de9f78b7 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Tue, 11 Jul 2023 22:12:38 +0200 Subject: [PATCH 21/30] complete refactor of Geiger implementation --- src/Sensors.cpp | 211 ++++++---------------------------- src/Sensors.hpp | 38 ++---- src/{ => drivers}/MovingSum.h | 0 src/drivers/geiger.cpp | 121 +++++++++++++++++++ src/drivers/geiger.h | 23 ++++ 5 files changed, 188 insertions(+), 205 deletions(-) rename src/{ => drivers}/MovingSum.h (100%) create mode 100644 src/drivers/geiger.cpp create mode 100644 src/drivers/geiger.h diff --git a/src/Sensors.cpp b/src/Sensors.cpp index f135c98f..42d0b2bb 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -23,22 +23,6 @@ int sensors_device_types[] = { SENSORS_TYPES }; uint8_t sensors_registered [SCOUNT]; -// ######################################################################### -// variables shared between main code and interrupt code - -#ifdef CAJOE_GEIGER - - hw_timer_t* geiger_timer = NULL; - portMUX_TYPE* geiger_timerMux = NULL; - - float uSvh = 0.0f; - uint32_t tics_cpm = 0U; // tics in last 60s - uint16_t tics_cnt = 0U; // tics in 1000ms - uint32_t tics_tot = 0U; // total tics since boot - MovingSum* cajoe_fms; - -#endif - /*********************************************************************************** * P U B L I C M E T H O D S * *********************************************************************************/ @@ -93,16 +77,14 @@ bool Sensors::readAllSensors() { aht10Read(); DFRobotCORead(); DFRobotNH3Read(); + geigerRead(); + #ifdef DHT11_ENABLED dhtRead(); #endif disableWire1(); -#ifdef CAJOE_GEIGER - geigerEvaluate(); -#endif - printValues(); printSensorsRegistered(devmode); printUnitsRegistered(devmode); @@ -153,11 +135,7 @@ void Sensors::init(u_int pms_type, int pms_rx, int pms_tx) { #ifdef DHT11_ENABLED dhtInit(); #endif - - #ifdef CAJOE_GEIGER - geigerInit(); - #endif - + printSensorsRegistered(true); } @@ -356,8 +334,6 @@ float Sensors::getCO() { return co; } - - /** * @brief UART only: check if the UART sensor is registered * @return bool true if the UART sensor is registered, false otherwise. @@ -575,14 +551,10 @@ float Sensors::getUnitValue(UNIT unit) { return alt; case GAS: return gas; - -#ifdef CAJOE_GEIGER case CPM: - return tics_cpm; + rad.tics_cpm; case RAD: - return uSvh; -#endif - + rad.uSvh; case NH3: return nh3; case CO: @@ -1633,7 +1605,6 @@ void Sensors::DFRobotNH3Init() { } // Altitude compensation for CO2 sensors without Pressure atm or Altitude compensation - void Sensors::CO2correctionAlt() { DEBUG("-->[SLIB] CO2 altitud original\t:", String(CO2Val).c_str()); float CO2cor = (0.016 * ((1013.25 - hpa) /10 ) * (CO2Val - 400)) + CO2Val; // Increment of 1.6% for every hpa of difference at sea level @@ -1676,17 +1647,40 @@ void Sensors::resetAllVariables() { alt = 0.0; gas = 0.0; pres = 0.0; - -#ifdef CAJOE_GEIGER - if (cajoe_fms != NULL) cajoe_fms->clear(); - tics_cpm = 0; - uSvh = 0.0; -#endif - nh3 = 0; co = 0; + rad.clear(); +} + +// ######################################################################### + +void Sensors::geigerRead(){ + if(rad.read()){ + unitRegister(UNIT::CPM); + unitRegister(UNIT::RAD); + } +} +/** + * @brief Enable Geiger sensor on specific pin + * @param GPIO pin +*/ +void Sensors::enableGeigerSensor(int gpio){ + sensorAnnounce(SENSORS::SCAJOE); + if (rad.init(gpio,devmode)){ + sensorRegister(SENSORS::SCAJOE); + } +} + +uint32_t Sensors::getGeigerCPM(void) { + return rad.tics_cpm; } +float Sensors::getGeigerMicroSievertHour(void) { + return rad.uSvh; +} + +// ######################################################################### + void Sensors::DEBUG(const char *text, const char *textb) { if (devmode) { _debugPort.print(text); @@ -1839,138 +1833,3 @@ bool Sensors::serialInit(u_int pms_type, unsigned long speed_baud, int pms_rx, i #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SENSORSHANDLER) Sensors sensors; #endif - -// ######################################################################### -// Interrupt routine called on each click from the geiger tube -// WARNING! the ISR is actually called on both the rising and the falling edge even if configure for FALLING or RISING - -#ifdef CAJOE_GEIGER - void IRAM_ATTR GeigerTicISR() { - - portENTER_CRITICAL_ISR(geiger_timerMux); - - tics_cnt++; // tics in 1000ms - tics_tot++; // total tics since boot - - portEXIT_CRITICAL_ISR(geiger_timerMux); -} -#endif - -// ######################################################################### -// Interrupt timer routine called every 1000 ms - -#ifdef CAJOE_GEIGER -void IRAM_ATTR onGeigerTimer() { - - portENTER_CRITICAL_ISR(geiger_timerMux); - cajoe_fms->add(tics_cnt); - tics_cnt = 0; - portEXIT_CRITICAL_ISR(geiger_timerMux); -} -#endif - -// ######################################################################### -// Converts CPM to uSv/h units (J305 tube) - -#ifdef CAJOE_GEIGER - -float Sensors::CPM2uSvh(uint32_t cpm) { - return float(cpm) * J305_CONV_FACTOR; -} - -#endif - -// ######################################################################### -// Initialize Geiger counter - -#ifdef CAJOE_GEIGER - -void Sensors::geigerInit() { - - tics_cnt = 0U; // tics in 1000ms - tics_tot = 0U; // total tics since boot - - geiger_timer = NULL; - geiger_timerMux = new portMUX_TYPE(portMUX_INITIALIZER_UNLOCKED); - -// moving sum for CAJOE Geiger Counter, configured for 60 samples (1 sample every 1s * 60 samples = 60s) - cajoe_fms = new MovingSum(GEIGER_BUFSIZE); - - Serial.println("-->[SLIB] Geiger counter startup"); - - sensorRegister(SENSORS::SCAJOE); - unitRegister(UNIT::CPM); - unitRegister(UNIT::RAD); - -// attach interrupt routine to the GPI connected to the Geiger counter module - pinMode(GEIGER_PINTIC, INPUT); - attachInterrupt(digitalPinToInterrupt(GEIGER_PINTIC), GeigerTicISR, FALLING); - -// attach interrupt routine to internal timer, to fire every 1000 ms - geiger_timer = timerBegin(GEIGER_TIMER, 80, true); - timerAttachInterrupt(geiger_timer, &onGeigerTimer, true); - timerAlarmWrite(geiger_timer, 1000000, true); // 1000 ms - timerAlarmEnable(geiger_timer); - - Serial.println("-->[SLIB] Geiger counter ready"); -} - -#endif - -// ######################################################################### -// Geiger counts evaluation -// CAJOE kit comes with a Chinese J305 geiger tube -// Conversion factor used for conversion from CPM to uSv/h is 0.008120370 (J305 tube) - -#ifdef CAJOE_GEIGER - -void Sensors::geigerEvaluate() { - - bool ready; - uint32_t tics_len; - - portENTER_CRITICAL(geiger_timerMux); - tics_cpm = cajoe_fms->getCurrentSum(); - tics_len = cajoe_fms->getCurrentFilterLength(); - portEXIT_CRITICAL(geiger_timerMux); - -// check whether the moving sum is full - ready = (tics_len == cajoe_fms->getFilterLength()); - -// convert CPM (tics in last minute) to uSv/h and put in display buffer for TFT -// moving sum buffer size is 60 (1 sample every 1000 ms * 60 samples): the complete sum cover exactly last 60s - if (ready){ - uSvh = CPM2uSvh(tics_cpm); - }else{ - uSvh = 0.0; - } - - Serial.print("-->[SLIB] tTOT: "); Serial.println(tics_tot); - Serial.print("-->[SLIB] tLEN: "); Serial.print (tics_len); Serial.println(ready ? " (ready)" : " (not ready)"); - Serial.print("-->[SLIB] tCPM: "); Serial.println(tics_cpm); - Serial.print("-->[SLIB] uSvh: "); Serial.println(uSvh); -} - -#endif - -// ######################################################################### - -#ifdef CAJOE_GEIGER - -uint32_t Sensors::getGeigerCPM(void) { - - return tics_cpm; -} - -#endif - -// ######################################################################### - -#ifdef CAJOE_GEIGER - -float Sensors::getGeigerMicroSievertHour(void) { - - return uSvh; -} - -#endif diff --git a/src/Sensors.hpp b/src/Sensors.hpp index eda6d15c..883ecdc6 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -16,12 +16,9 @@ #include #include #include +#include #include -#ifdef CAJOE_GEIGER -#include "MovingSum.h" -#endif - #ifdef DHT11_ENABLED #include #endif @@ -29,21 +26,6 @@ #define CSL_VERSION "0.6.8" #define CSL_REVISION 375 -/************************************************************** - * GEIGER - * ************************************************************/ - -#ifdef CAJOE_GEIGER -#ifdef ESP32 -#define GEIGER_TIMER 1 // timer0 is already used (at least on TTGO-TDisplay) somewhere ??? -#define GEIGER_PINTIC 26 // GPIO27 is busy (used as sensor(s) enable) -#else -#define GEIGER_PINTIC D5 // tested on ESP8266 NodeMCU -#endif -#define GEIGER_BUFSIZE 60 // moving sum buffer size (1 sample every 1s * 60 samples = 60s) -#define J305_CONV_FACTOR 0.008120370 // conversion factor used for conversion from CPM to uSv/h units (J305 tube) -#endif - /*************************************************************** * S E T U P E S P 3 2 B O A R D S A N D F I E L D S ***************************************************************/ @@ -243,6 +225,9 @@ class Sensors { // DFRobot gravity NH3 sensor addr 0x77 DFRobot_GAS_I2C dfrNH3; + // Geiger CAJOE sensor + GEIGER rad; + void init(u_int pms_type = 0, int pms_rx = PMS_RX, int pms_tx = PMS_TX); void loop(); @@ -291,10 +276,11 @@ class Sensors { float getCO(); -#ifdef CAJOE_GEIGER + void enableGeigerSensor(int gpio); + uint32_t getGeigerCPM(void); + float getGeigerMicroSievertHour(void); -#endif void setTempOffset(float offset); @@ -431,14 +417,6 @@ class Sensors { void DFRobotCOInit(); void DFRobotCORead(); - // Geiger methods: - -#ifdef CAJOE_GEIGER - void geigerInit(); - void geigerEvaluate(); - float CPM2uSvh(uint32_t cpm); -#endif - // UART sensors methods: bool sensorSerialInit(u_int pms_type, int rx, int tx); @@ -465,6 +443,8 @@ class Sensors { void sps30Errorloop(char *mess, uint8_t r); void sps30DeviceInfo(); + void geigerRead(); + void onSensorError(const char *msg); void startI2C(); diff --git a/src/MovingSum.h b/src/drivers/MovingSum.h similarity index 100% rename from src/MovingSum.h rename to src/drivers/MovingSum.h diff --git a/src/drivers/geiger.cpp b/src/drivers/geiger.cpp new file mode 100644 index 00000000..8b331025 --- /dev/null +++ b/src/drivers/geiger.cpp @@ -0,0 +1,121 @@ +#include "geiger.h" + +hw_timer_t* geiger_timer = NULL; +portMUX_TYPE* geiger_timerMux = NULL; +uint16_t tics_cnt = 0U; // tics in 1000ms +uint32_t tics_tot = 0U; // total tics since boot + +MovingSum* cajoe_fms; + +// ######################################################################### +// Interrupt routine called on each click from the geiger tube +// WARNING! the ISR is actually called on both the rising and the falling edge even if configure for FALLING or RISING + +void IRAM_ATTR GeigerTicISR() { + portENTER_CRITICAL_ISR(geiger_timerMux); + tics_cnt++; // tics in 1000ms + tics_tot++; // total tics since boot + portEXIT_CRITICAL_ISR(geiger_timerMux); +} + +// ######################################################################### +// Interrupt timer routine called every 1000 ms +void IRAM_ATTR onGeigerTimer() { + portENTER_CRITICAL_ISR(geiger_timerMux); + cajoe_fms->add(tics_cnt); + tics_cnt = 0; + portEXIT_CRITICAL_ISR(geiger_timerMux); +} + +// ######################################################################### +// Initialize Geiger counter +bool GEIGER::init(int gpio, bool debug) { + if (gpio < 0) { + if (debug) Serial.println("[E][SLIB] undefined Geiger pin"); + return false; + } + devmode = debug; + tics_cnt = 0U; // tics in 1000ms + tics_tot = 0U; // total tics since boot + + geiger_timer = NULL; + geiger_timerMux = new portMUX_TYPE(portMUX_INITIALIZER_UNLOCKED); + + // moving sum for CAJOE Geiger Counter, configured for 60 samples (1 sample every 1s * 60 samples = 60s) + cajoe_fms = new MovingSum(GEIGER_BUFSIZE); + + Serial.println("-->[SLIB] Geiger counter startup"); + + // attach interrupt routine to the GPI connected to the Geiger counter module + pinMode(gpio, INPUT); + attachInterrupt(digitalPinToInterrupt(gpio), GeigerTicISR, FALLING); + + // attach interrupt routine to internal timer, to fire every 1000 ms + geiger_timer = timerBegin(GEIGER_TIMER, 80, true); + timerAttachInterrupt(geiger_timer, &onGeigerTimer, true); + timerAlarmWrite(geiger_timer, 1000000, true); // 1000 ms + timerAlarmEnable(geiger_timer); + + Serial.println("-->[SLIB] Geiger counter ready"); + + return true; +} + +// ######################################################################### +// Geiger counts evaluation +// CAJOE kit comes with a Chinese J305 geiger tube +// Conversion factor used for conversion from CPM to uSv/h is 0.008120370 (J305 tube) +bool GEIGER::read() { + if (geiger_timer == NULL) return false; + bool ready; + uint32_t tics_len; + + portENTER_CRITICAL(geiger_timerMux); + tics_cpm = cajoe_fms->getCurrentSum(); + tics_len = cajoe_fms->getCurrentFilterLength(); + portEXIT_CRITICAL(geiger_timerMux); + + // check whether the moving sum is full + ready = (tics_len == cajoe_fms->getFilterLength()); + + // convert CPM (tics in last minute) to uSv/h and put in display buffer for TFT + // moving sum buffer size is 60 (1 sample every 1000 ms * 60 samples): the complete sum cover exactly last 60s + if (ready) { + uSvh = getUSvh(); + } else { + uSvh = 0.0; + } + + if (!devmode) return true; + + Serial.print("-->[SLIB] tTOT: "); + Serial.println(tics_tot); + Serial.print("-->[SLIB] tLEN: "); + Serial.print(tics_len); + Serial.println(ready ? " (ready)" : " (not ready)"); + Serial.print("-->[SLIB] tCPM: "); + Serial.println(tics_cpm); + Serial.print("-->[SLIB] uSvh: "); + Serial.println(uSvh); + + return true; +} + +/** + * Converts CPM to uSv/h units (J305 tube) +*/ +float GEIGER::getUSvh() { + return float(tics_cpm) * J305_CONV_FACTOR; +} +/** + * Returns CPM +*/ +uint32_t GEIGER::getTics() { + return tics_cpm; +} + +void GEIGER::clear() { + tics_cpm = 0; + uSvh = 0.0; + if (cajoe_fms != NULL) cajoe_fms->clear(); +} diff --git a/src/drivers/geiger.h b/src/drivers/geiger.h new file mode 100644 index 00000000..a355893b --- /dev/null +++ b/src/drivers/geiger.h @@ -0,0 +1,23 @@ +#include +#include "MovingSum.h" + +/************************************************************** + * GEIGER + * ************************************************************/ + +#define GEIGER_TIMER 1 // timer0 is already used (at least on TTGO-TDisplay) somewhere ??? +#define GEIGER_BUFSIZE 60 // moving sum buffer size (1 sample every 1s * 60 samples = 60s) +#define J305_CONV_FACTOR 0.008120370 // conversion factor used for conversion from CPM to uSv/h units (J305 tube) + +class GEIGER { + private: + bool devmode = false; + public: + uint32_t tics_cpm = 0U; // tics in last 60s + float uSvh = 0.0f; + bool init(int gpio = -1, bool debugEnable = false); + bool read(); + void clear(); + uint32_t getTics(); + float getUSvh(); +}; \ No newline at end of file From 824a24e2ce9ee163b091ed0e15f4cbff302dbbc1 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Tue, 11 Jul 2023 22:53:47 +0200 Subject: [PATCH 22/30] fixed architecture issue for atmelsam --- src/drivers/geiger.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/drivers/geiger.cpp b/src/drivers/geiger.cpp index 8b331025..dac4e430 100644 --- a/src/drivers/geiger.cpp +++ b/src/drivers/geiger.cpp @@ -1,10 +1,10 @@ #include "geiger.h" +#ifdef ESP32 hw_timer_t* geiger_timer = NULL; portMUX_TYPE* geiger_timerMux = NULL; uint16_t tics_cnt = 0U; // tics in 1000ms uint32_t tics_tot = 0U; // total tics since boot - MovingSum* cajoe_fms; // ######################################################################### @@ -26,10 +26,12 @@ void IRAM_ATTR onGeigerTimer() { tics_cnt = 0; portEXIT_CRITICAL_ISR(geiger_timerMux); } +#endif // ######################################################################### // Initialize Geiger counter bool GEIGER::init(int gpio, bool debug) { +#ifdef ESP32 if (gpio < 0) { if (debug) Serial.println("[E][SLIB] undefined Geiger pin"); return false; @@ -59,6 +61,9 @@ bool GEIGER::init(int gpio, bool debug) { Serial.println("-->[SLIB] Geiger counter ready"); return true; +#else + return false; // TODO: We need an alternative to atmelsam +#endif } // ######################################################################### @@ -66,6 +71,7 @@ bool GEIGER::init(int gpio, bool debug) { // CAJOE kit comes with a Chinese J305 geiger tube // Conversion factor used for conversion from CPM to uSv/h is 0.008120370 (J305 tube) bool GEIGER::read() { +#ifdef ESP32 if (geiger_timer == NULL) return false; bool ready; uint32_t tics_len; @@ -99,6 +105,9 @@ bool GEIGER::read() { Serial.println(uSvh); return true; +#else + return false; +#endif } /** @@ -117,5 +126,7 @@ uint32_t GEIGER::getTics() { void GEIGER::clear() { tics_cpm = 0; uSvh = 0.0; +#ifdef ESP32 if (cajoe_fms != NULL) cajoe_fms->clear(); +#endif } From 9414691dea2476ebb6ef57ea1a19fa6162e0996e Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Thu, 13 Jul 2023 00:30:47 +0200 Subject: [PATCH 23/30] minor fix for possible issue on Geiger getters --- src/Sensors.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 42d0b2bb..ffbacc9d 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -552,9 +552,9 @@ float Sensors::getUnitValue(UNIT unit) { case GAS: return gas; case CPM: - rad.tics_cpm; + rad.getTics(); case RAD: - rad.uSvh; + rad.getUSvh(); case NH3: return nh3; case CO: From 68f1c949200796d9a0a43ad009b22f60c25d0334 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Thu, 13 Jul 2023 18:05:49 +0200 Subject: [PATCH 24/30] added getter instead public access to geiger fields --- src/Sensors.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index ffbacc9d..2272890d 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -1672,11 +1672,11 @@ void Sensors::enableGeigerSensor(int gpio){ } uint32_t Sensors::getGeigerCPM(void) { - return rad.tics_cpm; + return rad.getTics(); } float Sensors::getGeigerMicroSievertHour(void) { - return rad.uSvh; + return rad.getUSvh(); } // ######################################################################### From 0343187206954a358b0f3424772af9c9c19d3ff4 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Thu, 13 Jul 2023 18:32:05 +0200 Subject: [PATCH 25/30] added log notification of pin selected --- src/drivers/geiger.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/drivers/geiger.cpp b/src/drivers/geiger.cpp index dac4e430..07cae68b 100644 --- a/src/drivers/geiger.cpp +++ b/src/drivers/geiger.cpp @@ -46,7 +46,7 @@ bool GEIGER::init(int gpio, bool debug) { // moving sum for CAJOE Geiger Counter, configured for 60 samples (1 sample every 1s * 60 samples = 60s) cajoe_fms = new MovingSum(GEIGER_BUFSIZE); - Serial.println("-->[SLIB] Geiger counter startup"); + Serial.printf("-->[SLIB] Geiger startup on pin\t: %i\r\n",gpio); // attach interrupt routine to the GPI connected to the Geiger counter module pinMode(gpio, INPUT); From 64344a4ecc001f10ccdf2febf2521a67d5affeab Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Fri, 14 Jul 2023 00:22:07 +0200 Subject: [PATCH 26/30] fix issue and object constructor refactor --- src/Sensors.cpp | 17 ++++++++--------- src/Sensors.hpp | 2 +- src/drivers/geiger.cpp | 12 ++++-------- src/drivers/geiger.h | 7 ++++++- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 2272890d..28ac6ce1 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -552,9 +552,9 @@ float Sensors::getUnitValue(UNIT unit) { case GAS: return gas; case CPM: - rad.getTics(); + return rad->getTics(); case RAD: - rad.getUSvh(); + return rad->getUSvh(); case NH3: return nh3; case CO: @@ -1649,13 +1649,13 @@ void Sensors::resetAllVariables() { pres = 0.0; nh3 = 0; co = 0; - rad.clear(); + rad->clear(); } // ######################################################################### void Sensors::geigerRead(){ - if(rad.read()){ + if(rad->read()){ unitRegister(UNIT::CPM); unitRegister(UNIT::RAD); } @@ -1666,17 +1666,16 @@ void Sensors::geigerRead(){ */ void Sensors::enableGeigerSensor(int gpio){ sensorAnnounce(SENSORS::SCAJOE); - if (rad.init(gpio,devmode)){ - sensorRegister(SENSORS::SCAJOE); - } + rad = new GEIGER(gpio,devmode); + sensorRegister(SENSORS::SCAJOE); } uint32_t Sensors::getGeigerCPM(void) { - return rad.getTics(); + return rad->getTics(); } float Sensors::getGeigerMicroSievertHour(void) { - return rad.getUSvh(); + return rad->getUSvh(); } // ######################################################################### diff --git a/src/Sensors.hpp b/src/Sensors.hpp index 883ecdc6..03cd539c 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -226,7 +226,7 @@ class Sensors { DFRobot_GAS_I2C dfrNH3; // Geiger CAJOE sensor - GEIGER rad; + GEIGER *rad; void init(u_int pms_type = 0, int pms_rx = PMS_RX, int pms_tx = PMS_TX); diff --git a/src/drivers/geiger.cpp b/src/drivers/geiger.cpp index 07cae68b..4194e4d3 100644 --- a/src/drivers/geiger.cpp +++ b/src/drivers/geiger.cpp @@ -30,11 +30,11 @@ void IRAM_ATTR onGeigerTimer() { // ######################################################################### // Initialize Geiger counter -bool GEIGER::init(int gpio, bool debug) { +GEIGER::GEIGER(int gpio, bool debug) { #ifdef ESP32 if (gpio < 0) { if (debug) Serial.println("[E][SLIB] undefined Geiger pin"); - return false; + return; } devmode = debug; tics_cnt = 0U; // tics in 1000ms @@ -59,10 +59,6 @@ bool GEIGER::init(int gpio, bool debug) { timerAlarmEnable(geiger_timer); Serial.println("-->[SLIB] Geiger counter ready"); - - return true; -#else - return false; // TODO: We need an alternative to atmelsam #endif } @@ -114,13 +110,13 @@ bool GEIGER::read() { * Converts CPM to uSv/h units (J305 tube) */ float GEIGER::getUSvh() { - return float(tics_cpm) * J305_CONV_FACTOR; + return float(this->tics_cpm) * J305_CONV_FACTOR; } /** * Returns CPM */ uint32_t GEIGER::getTics() { - return tics_cpm; + return this->tics_cpm; } void GEIGER::clear() { diff --git a/src/drivers/geiger.h b/src/drivers/geiger.h index a355893b..72b5bef0 100644 --- a/src/drivers/geiger.h +++ b/src/drivers/geiger.h @@ -15,7 +15,12 @@ class GEIGER { public: uint32_t tics_cpm = 0U; // tics in last 60s float uSvh = 0.0f; - bool init(int gpio = -1, bool debugEnable = false); + /** + * @brief Constructor + * @param gpio attached pin + * @param debug debug mode enable/disable + */ + explicit GEIGER(int gpio = -1, bool debug = false); bool read(); void clear(); uint32_t getTics(); From 7bf37bb054d502b4fb742c54c02867ffc0155daf Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Fri, 14 Jul 2023 22:53:09 +0200 Subject: [PATCH 27/30] removed specif test for Geiger sample. (only architecture tests) --- platformio.ini | 7 ------- 1 file changed, 7 deletions(-) diff --git a/platformio.ini b/platformio.ini index c86cd582..ae2152fc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -80,13 +80,6 @@ board_build.partitions = default_8MB.csv ;board_build.flash_size = 8MB ;board_build.psram_type = opi -[env:CAJOE_GEIGER] -extends = esp32_common -build_flags = - ${env.build_flags} - -DCAJOE_GEIGER -build_src_filter = -<*> + - [env:atmelsam] extends = atmelsam_common build_src_filter = -<*> + From a4541b68071f8061b03dcfbd29e749248cf99119 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Fri, 14 Jul 2023 22:53:55 +0200 Subject: [PATCH 28/30] added missing init Geiger sample call --- examples/radiation_CAJOE/radiation_CAJOE.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/examples/radiation_CAJOE/radiation_CAJOE.cpp b/examples/radiation_CAJOE/radiation_CAJOE.cpp index ae440529..09d1ed39 100644 --- a/examples/radiation_CAJOE/radiation_CAJOE.cpp +++ b/examples/radiation_CAJOE/radiation_CAJOE.cpp @@ -23,12 +23,8 @@ #include void onSensorDataOk() { - Serial.print(" CO2: " + String(sensors.getCO2())); - Serial.print(" CO2humi: " + String(sensors.getCO2humi())); - Serial.print(" CO2temp: " + String(sensors.getCO2temp())); - - Serial.print(" H: " + String(sensors.getHumidity())); - Serial.println(" T: " + String(sensors.getTemperature())); + Serial.print(" CPM: " + String(sensors.getGeigerCPM())); + Serial.print(" uSvh: " + String(sensors.getGeigerMicroSievertHour())); } void onSensorDataError(const char* msg) { @@ -51,6 +47,7 @@ void setup() { sensors.setOnErrorCallBack(&onSensorDataError); // [optional] error callback sensors.setDebugMode(true); // [optional] debug mode sensors.detectI2COnly(true); // [optional] skip UART detection + sensors.enableGeigerSensor(27); // Geiger in sensor pin 27 sensors.init(); // forced UAQ sensor. Empty for auto detection From 552a2974d6f7419109da3554436422154ef69715 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Sun, 16 Jul 2023 09:24:15 +0200 Subject: [PATCH 29/30] fixed some header issues and added contributors --- examples/advanced_multivariable/src/main.cpp | 4 ++-- examples/radiation_CAJOE/radiation_CAJOE.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/advanced_multivariable/src/main.cpp b/examples/advanced_multivariable/src/main.cpp index 7b7c6afd..232507f9 100644 --- a/examples/advanced_multivariable/src/main.cpp +++ b/examples/advanced_multivariable/src/main.cpp @@ -62,9 +62,9 @@ void setup() { Serial.println("\n== Sensor test setup ==\n"); Serial.println("-->[SETUP] Detecting sensors.."); - sensors.setSampleTime(10); // config sensors sample time interval + sensors.setSampleTime(10); // config sensors sample time interval sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback - sensors.setDebugMode(true); // [optional] debug mode + sensors.setDebugMode(true); // [optional] debug mode sensors.detectI2COnly(false); // disable force to only i2c sensors sensors.init(); // Auto detection to UART and i2c sensors delay(1000); diff --git a/examples/radiation_CAJOE/radiation_CAJOE.cpp b/examples/radiation_CAJOE/radiation_CAJOE.cpp index 09d1ed39..a0ae7ba8 100644 --- a/examples/radiation_CAJOE/radiation_CAJOE.cpp +++ b/examples/radiation_CAJOE/radiation_CAJOE.cpp @@ -1,7 +1,7 @@ /** * @file main.cpp - * @author Antonio Vanegas @hpsaturn - * @date June 2018 - 2022 + * @authors @roberbike @iw2lsi @hpsaturn + * @date June 2018 - 2023 * @brief Radiation sensor example * @license GPL3 * @@ -11,6 +11,10 @@ * Full implementation for WiFi and Bluetooth Air Quality fixed and mobile station: * https://github.com/kike-canaries/canairio_firmware#canairio-firmware * + * Main pull requests and discussions: + * https://github.com/kike-canaries/canairio_sensorlib/pull/144 + * https://github.com/kike-canaries/canairio_firmware/pull/226 + * * CanAirIO project: * https://canair.io * From 95a6a88036452fad685cd1210452150b6cc7372e Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Sun, 16 Jul 2023 09:25:29 +0200 Subject: [PATCH 30/30] v0.6.9r376 Geiger sensor RC1. Thanks to @roberbike @iw2lsi --- library.json | 2 +- library.properties | 2 +- src/Sensors.hpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/library.json b/library.json index ddd0ec80..e9215e07 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "CanAirIO Air Quality Sensors Library", - "version": "0.6.8", + "version": "0.6.9", "homepage":"https://canair.io", "keywords": [ diff --git a/library.properties b/library.properties index ae26f78e..4cf29075 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=CanAirIO Air Quality Sensors Library -version=0.6.8 +version=0.6.9 author=@hpsaturn, CanAirIO project maintainer=Antonio Vanegas url=https://github.com/kike-canaries/canairio_sensorlib diff --git a/src/Sensors.hpp b/src/Sensors.hpp index 03cd539c..f57500d9 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -23,8 +23,8 @@ #include #endif -#define CSL_VERSION "0.6.8" -#define CSL_REVISION 375 +#define CSL_VERSION "0.6.9" +#define CSL_REVISION 376 /*************************************************************** * S E T U P E S P 3 2 B O A R D S A N D F I E L D S