implement uv sensor ML8511

This commit is contained in:
interfisch 2020-10-30 09:23:05 +01:00
parent fcfd626dbe
commit 913a586eeb
2 changed files with 79 additions and 1 deletions

View File

@ -41,6 +41,10 @@ build_flags =
-D HS1101PIN=D5 -D HS1101PIN=D5
-D dataHS1101_minchange=2 -D dataHS1101_minchange=2
-D SENSOR_ML8511
-D ML8511PIN=A0
-D dataML8511_minchange=0.05
lib_deps = lib_deps =
Adafruit BMP085 Library@1.1.0 Adafruit BMP085 Library@1.1.0
https://github.com/adafruit/Adafruit_TCS34725 https://github.com/adafruit/Adafruit_TCS34725

View File

@ -72,6 +72,14 @@ struct sensordata
float value_lightBH1750=0; float value_lightBH1750=0;
#endif #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 #ifdef SENSOR_PIR
// PIR Sensors HC-SR501 (modified to put out shortest pulse time short pins 5 and 6 of ic) // 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
#endif #endif
#ifdef SENSOR_ML8511
Serial.println("initializing ml8511");
pinMode(ML8511PIN, INPUT);
#ifdef dataML8511_minchange
dataML8511.minchange=dataML8511_minchange;
#endif
#endif
#ifdef SENSOR_PIR #ifdef SENSOR_PIR
Serial.println("initializing pir"); Serial.println("initializing pir");
@ -353,6 +369,11 @@ void setup() {
lightMeter.readLightLevel(); //make first reading, could be 0 lightMeter.readLightLevel(); //make first reading, could be 0
#endif #endif
#ifdef SENSOR_ML8511
sensorNode.advertise("uv");
analogRead(ML8511PIN); //first read adc. just to avoid problems
#endif
#ifdef SENSOR_LDR #ifdef SENSOR_LDR
sensorNode.advertise("light"); sensorNode.advertise("light");
analogRead(LDR_PIN); //first reading could be false analogRead(LDR_PIN); //first reading could be false
@ -584,6 +605,36 @@ void loop_BH1750()
} }
#endif #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 #ifdef SENSOR_LDR
void loop_LDR() void loop_LDR()
{ {
@ -840,6 +891,10 @@ void loopHandler() {
} }
#endif #endif
#ifdef SENSOR_ML8511
loop_ML8511();
#endif
#ifdef SENSOR_LDR #ifdef SENSOR_LDR
loop_LDR(); loop_LDR();
#endif #endif
@ -1014,8 +1069,21 @@ float getHumidity_HS1101(int pin) {
return -1; 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] //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 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 // interpolate in the right segment for the rest
return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]); 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;
}