105 lines
3.0 KiB
C++
105 lines
3.0 KiB
C++
|
|
||
|
#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]);
|
||
|
}
|