move ml8511 to class

master
interfisch 1 year ago
parent ea0f2183d2
commit 90b1185795
  1. 79
      include/sensor_ml8511.cpp
  2. 36
      include/sensor_ml8511.h
  3. 16
      platformio.ini
  4. 91
      src/main.cpp

@ -0,0 +1,79 @@
//value as uvIntensity (mW/cm^2)
#include "sensor_ml8511.h"
Sensor_ML8511::Sensor_ML8511(int pin)
{
ml8511pin=pin;
}
void Sensor_ML8511::init() //Things to be done during setup()
{
Serial.println("initializing ml8511");
pinMode(ml8511pin, INPUT);
analogRead(ml8511pin); //first read adc. just to avoid problems
init_ok=true;
}
//Also called during setup()
void Sensor_ML8511::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data.minchange=minchange;
data.senddelaymax=senddelaymax;
data.readdelay=readdelay;
}
//Called during setup
void Sensor_ML8511::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
sensorNode->advertise("uv");
}
void Sensor_ML8511::sensorloop()
{
if (init_ok) {
sensordata &d=data;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
Serial.print("analogRead="); Serial.println(analogRead(ml8511pin));
d.value = getUV_ML8511(ml8511pin); //uvIntensity (mW/cm^2)
if (fabs(d.lastsentvalue-d.value)>=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");
Homie.getLogger() << "uv " << ": " << d.value << endl;
sensorNode->setProperty("uv").send(String(d.value));
d.lastsentvalue=d.value;
d.lastsent=millis();
}
}
}
float Sensor_ML8511::getUV_ML8511(int pin) {
float uvadc=0;
#define UVADCSAMPLES 32
for (uint16_t _s=0;_s<UVADCSAMPLES;_s++) {
uvadc += 3.06 / 1023 * analogRead(pin) ; //assumes 1023 = 3.069V (10-bit adc on esp8266)
}
uvadc=uvadc/UVADCSAMPLES; //average
return max(mapfloat(uvadc, 0.99, 2.8, 0.0, 15.0), 0.0F); //uvIntensity (mW/cm^2)
}
//The Arduino Map function but for floats
//From: http://forum.arduino.cc/index.php?topic=3922.0
float Sensor_ML8511::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;
}

@ -0,0 +1,36 @@
#ifndef SENSOR_ML8511_H
#define SENSOR_ML8511_H
#include "sensordata.h"
#include <Homie.h>
class Sensor_ML8511
{
private:
HomieNode *sensorNode; //reference to HomieNode
int ml8511pin;
struct sensordata data; //struct values are changed in setup()
bool init_ok;
float getUV_ML8511(int pin);
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
public:
Sensor_ML8511(int pin);
void init();
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void sensorloop();
};
#endif

@ -43,12 +43,12 @@ build_flags =
-D TCS34725_LUXFACTOR=0.3 #measured with luxmeter. with half tennis ball was 1.7ev less. 1/2^1.7=0,3078
-D SENSOR_HS1101
-D HS1101PIN=D5
-D SENSOR_HS1101_PIN=D5
-D SENSOR_HS1101_humidity_minchange=10
-D SENSOR_ML8511
-D ML8511PIN=A0
-D dataML8511_minchange=0.2
-D SENSOR_ML8511_PIN=A0
-D SENSOR_ML8511_minchange=0.2
-D SENSOR_ANEMOMETER
-D ANEMOMETERPIN=D6 #Light Blue thicker cable (in distribution box)
@ -84,13 +84,17 @@ monitor_speed = 115200
build_flags =
-D SENSOR_HS1101
-D HS1101PIN=D5
-D SENSOR_HS1101_PIN=D5
-D SENSOR_HS1101_humidity_minchange=10
-D SENSOR_BH1750
-D dataBH1750_minchange=10.0
-D dataBH1750_senddelaymax=1000*60*2
-D SENSOR_BH1750_light_minchange=10.0
-D SENSOR_BH1750_light_senddelaymax=1000*60*2
-D SENSOR_ML8511
-D SENSOR_ML8511_PIN=A0
-D SENSOR_ML8511_minchange=0.2

@ -104,7 +104,7 @@
#ifdef SENSOR_HS1101
#include "sensor_hs1101.cpp"
Sensor_HS1101 sensor_hs1101(HS1101PIN);
Sensor_HS1101 sensor_hs1101(SENSOR_HS1101_PIN);
#ifndef SENSOR_HS1101_humidity_minchange
#define SENSOR_HS1101_humidity_minchange 2.0
@ -122,30 +122,38 @@
#include "sensor_bh1750.cpp"
Sensor_BH1750 sensor_bh1750;
#ifndef SENSOR_BH1750_humidity_minchange
#define SENSOR_BH1750_humidity_minchange 10
#ifndef SENSOR_BH1750_light_minchange
#define SENSOR_BH1750_light_minchange 10
#endif
#ifndef SENSOR_BH1750_humidity_senddelaymax
#define SENSOR_BH1750_humidity_senddelaymax 5*60*1000
#ifndef SENSOR_BH1750_light_senddelaymax
#define SENSOR_BH1750_light_senddelaymax 5*60*1000
#endif
#ifndef SENSOR_BH1750_humidity_readdelay
#define SENSOR_BH1750_humidity_readdelay 1000*10
#ifndef SENSOR_BH1750_light_readdelay
#define SENSOR_BH1750_light_readdelay 1000*10
#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; //uvIntensity (mW/cm^2)
#include "sensor_ml8511.cpp"
Sensor_ML8511 sensor_ml8511(SENSOR_ML8511_PIN);
#ifndef SENSOR_ML8511_minchange
#define SENSOR_ML8511_minchange 0.2
#endif
#ifndef SENSOR_ML8511_senddelaymax
#define SENSOR_ML8511_senddelaymax 5*60*1000
#endif
#ifndef SENSOR_ML8511_readdelay
#define SENSOR_ML8511_readdelay 1000*10
#endif
#endif
#ifdef SENSOR_PIR
// PIR Sensors HC-SR501
// pir sensor needs 5v through an inductor for filtering. output level is 3.3v
// 100nF capacitor should be soldered between pins 12 and 13 of BISS0001 to stop interference from esp causing false triggers (in some setups). source: https://www.letscontrolit.com/forum/viewtopic.php?t=671
// 100nF capacitor SENSOR_ML8511_minchangeshould be soldered between pins 12 and 13 of BISS0001 to stop interference from esp causing false triggers (in some setups). source: https://www.letscontrolit.com/forum/viewtopic.php?t=671
// hc-sr501 should also be a few cm away from the esp. interference can cause false triggering
// poti closer to jumper is sensitivity (cw increases). other poti is pulse time (cw increases).
// time set to output around 30s pulse
@ -373,15 +381,12 @@ void setup() {
#ifdef SENSOR_BH1750
sensor_bh1750.init();
sensor_bh1750.setSettings(SENSOR_BH1750_humidity_minchange,SENSOR_BH1750_humidity_senddelaymax,SENSOR_BH1750_humidity_readdelay);
sensor_bh1750.setSettings(SENSOR_BH1750_light_minchange,SENSOR_BH1750_light_senddelaymax,SENSOR_BH1750_light_readdelay);
#endif
#ifdef SENSOR_ML8511
Serial.println("initializing ml8511");
pinMode(ML8511PIN, INPUT);
#ifdef dataML8511_minchange
dataML8511.minchange=dataML8511_minchange;
#endif
sensor_ml8511.init();
sensor_ml8511.setSettings(SENSOR_ML8511_minchange,SENSOR_ML8511_senddelaymax,SENSOR_ML8511_readdelay);
#endif
@ -559,8 +564,7 @@ void setup() {
#endif
#ifdef SENSOR_ML8511
sensorNode.advertise("uv");
analogRead(ML8511PIN); //first read adc. just to avoid problems
sensor_ml8511.advertise(sensorNode);
#endif
#ifdef SENSOR_LDR
@ -628,39 +632,6 @@ void loop() {
}
#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()
{
@ -1124,7 +1095,7 @@ void loopHandler() {
#endif
#ifdef SENSOR_ML8511
loop_ML8511();
sensor_ml8511.sensorloop();
#endif
#ifdef SENSOR_LDR
@ -1305,18 +1276,6 @@ void readSDS018()
#endif
#ifdef SENSOR_ML8511
float getUV_ML8511(int pin) {
float uvadc=0;
#define UVADCSAMPLES 32
for (uint16_t _s=0;_s<UVADCSAMPLES;_s++) {
uvadc += 3.06 / 1023 * analogRead(pin) ; //assumes 1023 = 3.069V (10-bit adc on esp8266)
}
uvadc=uvadc/UVADCSAMPLES; //average
return max(mapfloat(uvadc, 0.99, 2.8, 0.0, 15.0), 0.0F); //uvIntensity (mW/cm^2)
}
#endif
#ifdef SENSOR_ANEMOMETER
void ICACHE_RAM_ATTR interrupt_anemometer()

Loading…
Cancel
Save