From 913a586eebc27c7d2943c16f8bd420dfde6dd9c3 Mon Sep 17 00:00:00 2001 From: Fisch Date: Fri, 30 Oct 2020 09:23:05 +0100 Subject: [PATCH] implement uv sensor ML8511 --- platformio.ini | 4 +++ src/main.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 15fe179..3a50f74 100644 --- a/platformio.ini +++ b/platformio.ini @@ -41,6 +41,10 @@ build_flags = -D HS1101PIN=D5 -D dataHS1101_minchange=2 + -D SENSOR_ML8511 + -D ML8511PIN=A0 + -D dataML8511_minchange=0.05 + 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 dc37c9d..eacab84 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -72,6 +72,14 @@ struct sensordata float value_lightBH1750=0; #endif +#ifdef SENSOR_ML8511 + //ML8511 UV Sensor outputs an analog voltage. ML8511PIN needs to be an ADC pin + sensordata dataML8511; + float getUV_ML8511(int pin); + float mapfloat(float x, float in_min, float in_max, float out_min, float out_max); + float value_uvML8511=0; +#endif + #ifdef SENSOR_PIR // PIR Sensors HC-SR501 (modified to put out shortest pulse time short pins 5 and 6 of ic) @@ -248,6 +256,14 @@ void setup() { #endif #endif + #ifdef SENSOR_ML8511 + Serial.println("initializing ml8511"); + pinMode(ML8511PIN, INPUT); + #ifdef dataML8511_minchange + dataML8511.minchange=dataML8511_minchange; + #endif + #endif + #ifdef SENSOR_PIR Serial.println("initializing pir"); @@ -353,6 +369,11 @@ void setup() { lightMeter.readLightLevel(); //make first reading, could be 0 #endif + #ifdef SENSOR_ML8511 + sensorNode.advertise("uv"); + analogRead(ML8511PIN); //first read adc. just to avoid problems + #endif + #ifdef SENSOR_LDR sensorNode.advertise("light"); analogRead(LDR_PIN); //first reading could be false @@ -584,6 +605,36 @@ void loop_BH1750() } #endif + +#ifdef SENSOR_ML8511 +void loop_ML8511() +{ + sensordata &d=dataML8511; + + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + Serial.print("analogRead="); Serial.println(analogRead(ML8511PIN)); + value_uvML8511 = getUV_ML8511(ML8511PIN); //uvIntensity (mW/cm^2) + if (fabs(d.lastsentvalue-value_uvML8511)>=d.minchange){ + _changed=true; + } + d.lastreadtime=millis(); + } + + if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { + Serial.print("Sending uv ML8511. reason="); + if (_changed) Serial.println("change"); else Serial.println("time"); + checkESPStatus(); + + Homie.getLogger() << "uv " << ": " << value_uvML8511 << endl; + sensorNode.setProperty("uv").send(String(value_uvML8511)); + d.lastsentvalue=value_uvML8511; + + d.lastsent=millis(); + } +} +#endif + #ifdef SENSOR_LDR void loop_LDR() { @@ -840,6 +891,10 @@ void loopHandler() { } #endif + #ifdef SENSOR_ML8511 + loop_ML8511(); + #endif + #ifdef SENSOR_LDR loop_LDR(); #endif @@ -1014,8 +1069,21 @@ float getHumidity_HS1101(int pin) { return -1; } } +#endif +#ifdef SENSOR_ML8511 +float getUV_ML8511(int pin) { + float uvadc = 3.06 / 1023 * analogRead(pin) ; //assumes 1023 = 3.069V (10-bit adc on esp8266) + return max(mapfloat(uvadc, 0.99, 2.8, 0.0, 15.0), 0.0F); //uvIntensity (mW/cm^2) +} +#endif + + +/*################################## +* ######## HELPER FUNCTIONS ######## +*/ + //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 { @@ -1035,4 +1103,10 @@ int get_mapped(const unsigned int* _in, const unsigned int* _out, byte size,int // interpolate in the right segment for the rest return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]); } -#endif + +//The Arduino Map function but for floats +//From: http://forum.arduino.cc/index.php?topic=3922.0 +float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) +{ + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} \ No newline at end of file