From b88fa10d534128a8eb093a54d70ea0c70039d8b9 Mon Sep 17 00:00:00 2001 From: Fisch Date: Thu, 4 Nov 2021 17:52:39 +0100 Subject: [PATCH] move hs1101 to class --- include/sensor_hs1101.cpp | 105 ++++++++++++++++++++++++++++++++++++++ include/sensor_hs1101.h | 42 +++++++++++++++ platformio.ini | 24 ++------- src/main.cpp | 85 ++++++------------------------ 4 files changed, 169 insertions(+), 87 deletions(-) create mode 100644 include/sensor_hs1101.cpp create mode 100644 include/sensor_hs1101.h diff --git a/include/sensor_hs1101.cpp b/include/sensor_hs1101.cpp new file mode 100644 index 0000000..da5bdc7 --- /dev/null +++ b/include/sensor_hs1101.cpp @@ -0,0 +1,105 @@ + +#include "sensor_hs1101.h" + + +Sensor_HS1101::Sensor_HS1101(int pin) +{ + hs1101pin=pin; + struct sensordata data; + + //out_humidity[] = {1000,900,800,700,600,500,400,300,200,100,0}; //*10, gets later devided by 10 + //in_hs1101frequency[] = {6033,6186,6330,6468,6600,6728,6853,6976,7100,7224,7351}; +} + +void Sensor_HS1101::init() //Things to be done during setup() +{ + Serial.println("initializing HS1101"); + init_ok=true; +} + + +//Also called during setup() +void Sensor_HS1101::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay) +{ + data.minchange=minchange; + data.senddelaymax=senddelaymax; + data.readdelay=readdelay; +} + +//Called during setup +void Sensor_HS1101::advertise(HomieNode& p_sensorNode) +{ + sensorNode = &p_sensorNode; + #ifdef SENSOR_DHT22 + sensorNode->advertise("humidity_hs1101"); + #else + sensorNode->advertise("humidity"); + #endif +} + +void Sensor_HS1101::sensorloop() +{ + if (init_ok) { + + sensordata &d=data; + + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + d.value = getHumidity_HS1101(hs1101pin); //hum % + if (fabs(d.lastsentvalue-d.value)>=d.minchange){ + _changed=true; + } + d.lastreadtime=millis(); + } + + if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { + Serial.print("Sending HS1101. reason="); + if (_changed) Serial.println("change"); else Serial.println("time"); + + #if defined(SENSOR_DHT22) + Homie.getLogger() << "humidity hs1101 " << ": " << d.value << endl; + sensorNode->setProperty("humidity_hs1101").send(String(d.value)); + #else + Homie.getLogger() << "humidity " << ": " << d.value << endl; + sensorNode->setProperty("humidity").send(String(d.value)); + #endif + + d.lastsentvalue=d.value; + + d.lastsent=millis(); + } + } +} + +float Sensor_HS1101::getHumidity_HS1101(int pin) { + #define HS1101_SAMPLES 512 + double freq = 0; + //for(unsigned int j=0; j2000 && freq<10000){ //in roughly valid range + return get_mapped(in_hs1101frequency,out_humidity,HUMARRAYSIZE, freq )/10.0; + }else{ //error + return -1; + } +} + +//quelle: https://groups.google.com/forum/#!topic/souliss/1kMAltPB2ME[1-25] +int Sensor_HS1101::get_mapped(const unsigned int* _in, const unsigned int* _out, byte size,int val) //map with constrain +{ + // take care the value is within range + // val = constrain(val, _in[0], _in[size-1]); + + if (val <= _in[0]) return _out[0]; + if (val >= _in[size-1]) return _out[size-1]; + + // search right interval + byte pos = 1; // _in[0] allready tested + while(val > _in[pos]) pos++; + + // this will handle all exact "points" in the _in array + if (val == _in[pos]) return _out[pos]; + + // interpolate in the right segment for the rest + return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]); +} \ No newline at end of file diff --git a/include/sensor_hs1101.h b/include/sensor_hs1101.h new file mode 100644 index 0000000..a5ab826 --- /dev/null +++ b/include/sensor_hs1101.h @@ -0,0 +1,42 @@ +#ifndef SENSOR_HS1101_H +#define SENSOR_HS1101_H + +#include "sensordata.h" +#include + + + +class Sensor_HS1101 +{ + +private: + HomieNode *sensorNode; //reference to HomieNode + + struct sensordata data; //struct values are changed in setup() + + bool init_ok; + int hs1101pin; + float getHumidity_HS1101(int pin); + int get_mapped(const unsigned int* _in, const unsigned int* _out, byte size,int val); + + #define HUMARRAYSIZE 11 + //from HS1101 datasheet https://www.parallax.com/sites/default/files/downloads/27920-Humidity-Sensor-Datasheet.pdf + const unsigned int out_humidity[HUMARRAYSIZE] = {1000,900,800,700,600,500,400,300,200,100,0}; //*10, gets later devided by 10 + const unsigned int in_hs1101frequency[HUMARRAYSIZE] = {6033,6186,6330,6468,6600,6728,6853,6976,7100,7224,7351}; + //unsigned int out_humidity[HUMARRAYSIZE]; + //unsigned int in_hs1101frequency[HUMARRAYSIZE]; + +public: + Sensor_HS1101(int pin); + + + void init(); + void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay); + void advertise(HomieNode& p_sensorNode); + void sensorloop(); + + +}; + +#endif + diff --git a/platformio.ini b/platformio.ini index 0efcbbe..472d809 100644 --- a/platformio.ini +++ b/platformio.ini @@ -44,7 +44,7 @@ build_flags = -D SENSOR_HS1101 -D HS1101PIN=D5 - -D dataHS1101_minchange=10 + -D SENSOR_HS1101_humidity_minchange=10 -D SENSOR_ML8511 -D ML8511PIN=A0 @@ -82,30 +82,16 @@ monitor_port = /dev/ttyUSB0 monitor_speed = 115200 build_flags = - -D SENSOR_DHT22 - -D SENSOR_DHT22_PIN=D7 - -D SENSOR_DHT22_temperature_minchange=0.2 - -D SENSOR_DHT22_humidity_minchange=2.0 - - - -D SENSOR_BMP180 - -D SENSOR_BMP180_temperature_minchange=0.2 - -D SENSOR_BMP180_pressure_minchange=0.5 - - -D SENSOR_HTU21D - -D dataHTU21D_temperature_minchange=0.2 - -D dataHTU21D_temperature_senddelaymax=1000*60*20 - -D dataHTU21D_humidity_minchange=1.0 - -D dataHTU21D_humidity_senddelaymax=1000*60*30 + + -D SENSOR_HS1101 + -D HS1101PIN=D5 + -D SENSOR_HS1101_humidity_minchange=10 lib_deps = - DHT sensor library@1.3.10 ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error Homie@3.0.0 - Adafruit BMP085 Library@1.1.0 - adafruit/Adafruit HTU21DF Library @ ^1.0.5 #Arbeitszimmer [env:sensoresp1] diff --git a/src/main.cpp b/src/main.cpp index 957c1d8..eb1c869 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -103,14 +103,19 @@ #endif #ifdef SENSOR_HS1101 - struct sensordata dataHS1101; - float value_humidityHS1101=0; - #define HUMARRAYSIZE 11 - //from HS1101 datasheet https://www.parallax.com/sites/default/files/downloads/27920-Humidity-Sensor-Datasheet.pdf - static const unsigned int out_humidity[] = {1000,900,800,700,600,500,400,300,200,100,0}; //*10, gets later devided by 10 - static const unsigned int in_hs1101frequency[] = {6033,6186,6330,6468,6600,6728,6853,6976,7100,7224,7351}; - float getHumidity_HS1101(int pin); - int get_mapped(const unsigned int* _in, const unsigned int* _out, byte size,int val); + #include "sensor_hs1101.cpp" + Sensor_HS1101 sensor_hs1101(HS1101PIN); + + #ifndef SENSOR_HS1101_humidity_minchange + #define SENSOR_HS1101_humidity_minchange 2.0 + #endif + #ifndef SENSOR_HS1101_humidity_senddelaymax + #define SENSOR_HS1101_humidity_senddelaymax 10*60*1000 + #endif + #ifndef SENSOR_HS1101_humidity_readdelay + #define SENSOR_HS1101_humidity_readdelay 10000 + #endif + #endif #ifdef SENSOR_BH1750 @@ -360,10 +365,8 @@ void setup() { #ifdef SENSOR_HS1101 - Serial.println("initializing hs1101"); - #ifdef dataHS1101_minchange - dataHS1101.minchange=dataHS1101_minchange; - #endif + sensor_hs1101.init(); + sensor_hs1101.setSettings(SENSOR_HS1101_humidity_minchange,SENSOR_HS1101_humidity_senddelaymax,SENSOR_HS1101_humidity_readdelay); #endif #ifdef SENSOR_BH1750 @@ -558,11 +561,7 @@ void setup() { #endif #ifdef SENSOR_HS1101 - #ifdef SENSOR_DHT22 - sensorNode.advertise("humidity_hs1101"); - #else - sensorNode.advertise("humidity"); - #endif + sensor_hs1101.advertise(sensorNode); #endif #ifdef SENSOR_BH1750 @@ -643,41 +642,6 @@ void loop() { -#ifdef SENSOR_HS1101 -void loop_HS1101() { - - - sensordata &d=dataHS1101; - - bool _changed=false; - if (millis() >= (d.lastreadtime+d.readdelay)) { - value_humidityHS1101 = getHumidity_HS1101(HS1101PIN); //hum % - if (fabs(d.lastsentvalue-value_humidityHS1101)>=d.minchange){ - _changed=true; - } - d.lastreadtime=millis(); - } - - if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { - Serial.print("Sending HS1101. reason="); - if (_changed) Serial.println("change"); else Serial.println("time"); - checkESPStatus(); - - #if defined(SENSOR_DHT22) - Homie.getLogger() << "humidity hs1101 " << ": " << value_humidityHS1101 << endl; - sensorNode.setProperty("humidity_hs1101").send(String(value_humidityHS1101)); - #else - Homie.getLogger() << "humidity " << ": " << value_humidityHS1101 << endl; - sensorNode.setProperty("humidity").send(String(value_humidityHS1101)); - #endif - - d.lastsentvalue=value_humidityHS1101; - - d.lastsent=millis(); - } -} -#endif - #ifdef SENSOR_BH1750 void loop_BH1750() { @@ -1191,7 +1155,7 @@ void loopHandler() { #endif #ifdef SENSOR_HS1101 - loop_HS1101(); + sensor_hs1101.sensorloop(); #endif #ifdef SENSOR_BH1750 @@ -1381,21 +1345,6 @@ void readSDS018() } #endif -#ifdef SENSOR_HS1101 -float getHumidity_HS1101(int pin) { - #define HS1101_SAMPLES 512 - double freq = 0; - //for(unsigned int j=0; j2000 && freq<10000){ //in roughly valid range - return get_mapped(in_hs1101frequency,out_humidity,HUMARRAYSIZE, freq )/10.0; - }else{ //error - return -1; - } -} -#endif - #ifdef SENSOR_ML8511 float getUV_ML8511(int pin) {