diff --git a/platformio.ini b/platformio.ini index 9adc4d0..15fe179 100644 --- a/platformio.ini +++ b/platformio.ini @@ -37,6 +37,10 @@ build_flags = -D dataTCS34725_lux_minchange=20 -D dataTCS34725_colortemp_minchange=100 + -D SENSOR_HS1101 + -D HS1101PIN=D5 + -D dataHS1101_minchange=2 + lib_deps = Adafruit BMP085 Library@1.1.0 https://github.com/adafruit/Adafruit_TCS34725 diff --git a/src/main.cpp b/src/main.cpp index c5ac3ae..dc37c9d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -51,6 +51,17 @@ struct sensordata float value_pressureBMP=0; #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); +#endif + #ifdef SENSOR_BH1750 //SCL=D1, SDA=D2 #include @@ -154,6 +165,7 @@ struct sensordata #ifdef SENSOR_TCS34725 #include "Adafruit_TCS34725.h" + //Connect SCL to D1, SDA to D2, GND and 3v3 Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); bool tcs34725init_ok=false; struct sensordata dataTCS34725_lux; @@ -212,6 +224,13 @@ void setup() { #endif #endif + #ifdef SENSOR_HS1101 + Serial.println("initializing hs1101"); + #ifdef dataHS1101_minchange + dataHS1101.minchange=dataHS1101_minchange; + #endif + #endif + #ifdef SENSOR_BH1750 Serial.println("initializing bh1750"); Wire.begin(); @@ -321,6 +340,14 @@ void setup() { sensorNode.advertise("humidity"); #endif + #ifdef SENSOR_HS1101 + #if defined(SENSOR_DHT22) + sensorNode.advertise("humidity_hs1101"); + #else + sensorNode.advertise("humidity"); + #endif + #endif + #ifdef SENSOR_BH1750 sensorNode.advertise("light"); lightMeter.readLightLevel(); //make first reading, could be 0 @@ -494,6 +521,41 @@ void loop_BMP180_pressure() } #endif +#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() { @@ -768,6 +830,10 @@ void loopHandler() { } #endif + #ifdef SENSOR_HS1101 + loop_HS1101(); + #endif + #ifdef SENSOR_BH1750 if (bh1750init_ok) { loop_BH1750(); @@ -933,4 +999,40 @@ void readSDS018() sds018_swSerial.flush(); } } -#endif \ No newline at end of file +#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; + } +} + + +//quelle: https://groups.google.com/forum/#!topic/souliss/1kMAltPB2ME[1-25] +int 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]); +} +#endif