move hs1101 to class

This commit is contained in:
interfisch 2021-11-04 17:52:39 +01:00
parent 05907eb59d
commit b88fa10d53
4 changed files with 169 additions and 87 deletions

105
include/sensor_hs1101.cpp Normal file
View File

@ -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; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, LOW, 1000);
for(unsigned int j=0; j<HS1101_SAMPLES; j++) freq+= 1000000/(pulseIn(pin, HIGH, 1000)+pulseIn(pin, LOW, 1000)); //both high and low because signal has not 50% duty cycle
freq=freq / HS1101_SAMPLES;
if (freq>2000 && 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]);
}

42
include/sensor_hs1101.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef SENSOR_HS1101_H
#define SENSOR_HS1101_H
#include "sensordata.h"
#include <Homie.h>
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

View File

@ -44,7 +44,7 @@ build_flags =
-D SENSOR_HS1101 -D SENSOR_HS1101
-D HS1101PIN=D5 -D HS1101PIN=D5
-D dataHS1101_minchange=10 -D SENSOR_HS1101_humidity_minchange=10
-D SENSOR_ML8511 -D SENSOR_ML8511
-D ML8511PIN=A0 -D ML8511PIN=A0
@ -82,30 +82,16 @@ monitor_port = /dev/ttyUSB0
monitor_speed = 115200 monitor_speed = 115200
build_flags = 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_HS1101
-D SENSOR_BMP180 -D HS1101PIN=D5
-D SENSOR_BMP180_temperature_minchange=0.2 -D SENSOR_HS1101_humidity_minchange=10
-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
lib_deps = 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 ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
Homie@3.0.0 Homie@3.0.0
Adafruit BMP085 Library@1.1.0
adafruit/Adafruit HTU21DF Library @ ^1.0.5
#Arbeitszimmer #Arbeitszimmer
[env:sensoresp1] [env:sensoresp1]

View File

@ -103,14 +103,19 @@
#endif #endif
#ifdef SENSOR_HS1101 #ifdef SENSOR_HS1101
struct sensordata dataHS1101; #include "sensor_hs1101.cpp"
float value_humidityHS1101=0; Sensor_HS1101 sensor_hs1101(HS1101PIN);
#define HUMARRAYSIZE 11
//from HS1101 datasheet https://www.parallax.com/sites/default/files/downloads/27920-Humidity-Sensor-Datasheet.pdf #ifndef SENSOR_HS1101_humidity_minchange
static const unsigned int out_humidity[] = {1000,900,800,700,600,500,400,300,200,100,0}; //*10, gets later devided by 10 #define SENSOR_HS1101_humidity_minchange 2.0
static const unsigned int in_hs1101frequency[] = {6033,6186,6330,6468,6600,6728,6853,6976,7100,7224,7351}; #endif
float getHumidity_HS1101(int pin); #ifndef SENSOR_HS1101_humidity_senddelaymax
int get_mapped(const unsigned int* _in, const unsigned int* _out, byte size,int val); #define SENSOR_HS1101_humidity_senddelaymax 10*60*1000
#endif
#ifndef SENSOR_HS1101_humidity_readdelay
#define SENSOR_HS1101_humidity_readdelay 10000
#endif
#endif #endif
#ifdef SENSOR_BH1750 #ifdef SENSOR_BH1750
@ -360,10 +365,8 @@ void setup() {
#ifdef SENSOR_HS1101 #ifdef SENSOR_HS1101
Serial.println("initializing hs1101"); sensor_hs1101.init();
#ifdef dataHS1101_minchange sensor_hs1101.setSettings(SENSOR_HS1101_humidity_minchange,SENSOR_HS1101_humidity_senddelaymax,SENSOR_HS1101_humidity_readdelay);
dataHS1101.minchange=dataHS1101_minchange;
#endif
#endif #endif
#ifdef SENSOR_BH1750 #ifdef SENSOR_BH1750
@ -558,11 +561,7 @@ void setup() {
#endif #endif
#ifdef SENSOR_HS1101 #ifdef SENSOR_HS1101
#ifdef SENSOR_DHT22 sensor_hs1101.advertise(sensorNode);
sensorNode.advertise("humidity_hs1101");
#else
sensorNode.advertise("humidity");
#endif
#endif #endif
#ifdef SENSOR_BH1750 #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 #ifdef SENSOR_BH1750
void loop_BH1750() void loop_BH1750()
{ {
@ -1191,7 +1155,7 @@ void loopHandler() {
#endif #endif
#ifdef SENSOR_HS1101 #ifdef SENSOR_HS1101
loop_HS1101(); sensor_hs1101.sensorloop();
#endif #endif
#ifdef SENSOR_BH1750 #ifdef SENSOR_BH1750
@ -1381,21 +1345,6 @@ void readSDS018()
} }
#endif #endif
#ifdef SENSOR_HS1101
float getHumidity_HS1101(int pin) {
#define HS1101_SAMPLES 512
double freq = 0;
//for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, LOW, 1000);
for(unsigned int j=0; j<HS1101_SAMPLES; j++) freq+= 1000000/(pulseIn(pin, HIGH, 1000)+pulseIn(pin, LOW, 1000)); //both high and low because signal has not 50% duty cycle
freq=freq / HS1101_SAMPLES;
if (freq>2000 && 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 #ifdef SENSOR_ML8511
float getUV_ML8511(int pin) { float getUV_ML8511(int pin) {