move ldr to class

This commit is contained in:
interfisch 2021-11-06 18:24:40 +01:00
parent bd25dc9b7c
commit 53471a9c8a
4 changed files with 154 additions and 96 deletions

89
include/sensor_ldr.cpp Normal file
View File

@ -0,0 +1,89 @@
// High/Low Output LDR Sensor
// For example: RCWL-0516 (needs 5v input (gnd, vin), 3.3v output level. high for 2seconds when movement detected)
#include "sensor_ldr.h"
Sensor_LDR::Sensor_LDR(int p)
{
pin=p;
sensordata data;
}
void Sensor_LDR::init() //Things to be done during setup()
{
Serial.println("initializing LDR");
pinMode(pin, INPUT);
analogRead(pin); //first reading could be false
init_ok=true;
}
//Also called during setup()
void Sensor_LDR::setSettings(unsigned long senddelaymax, unsigned long readdelay)
{
data.senddelaymax=senddelaymax;
data.readdelay=readdelay;
}
//Called during setup
void Sensor_LDR::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
sensorNode->advertise("light");
}
void Sensor_LDR::sensorloop()
{
if (init_ok) {
sensordata &d=data;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
d.value = get_lux(in_ldr, out_ldr, LDR_ARRAYSIZE)/10.0; //read light level in lux
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending LDR. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
Homie.getLogger() << "light " << ": " << d.value << endl;
sensorNode->setProperty("light").send(String(d.value));
d.lastsentvalue=d.value;
d.lastsent=millis();
}
}
}
//////////////////////////////////////////////////////////////////////////////
// Calculate lux based on rawADC reading from LDR returns value in lux/10
//////////////////////////////////////////////////////////////////////////////
//quelle: https://groups.google.com/forum/#!topic/souliss/1kMAltPB2ME[1-25]
int Sensor_LDR::get_lux(const unsigned int* _in, const unsigned int* _out, byte size)
{
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
unsigned int val = analogRead(pin);
#ifdef DEBUG //DEBUG++++++++++++++++
Serial.print("LDR RAW=: ");
Serial.println(val);
#endif
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]);
}

43
include/sensor_ldr.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef SENSOR_LDR_H
#define SENSOR_LDR_H
#include "sensordata.h"
#include <Homie.h>
class Sensor_LDR
{
private:
HomieNode *sensorNode; //reference to HomieNode
int pin;
struct sensordata data; //struct values are changed in setup()
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size);
//wemos d1 mini, black wire of ldr connects to A0 with 10k to gnd. red wire connects with 1k to gnd and 2k2 to 3v3
#define LDRARRAYSIZE 18
const unsigned int out_ldr[18] = {0, 30, 50, 60, 130, 170, 250, 420, 780, 1300,2600, 5000, 5350, 7700, 10900, 12000, 17000,20000}; // x10 (i.e. gets later divided by 10)
const unsigned int in_ldr[18] = {0, 12, 100, 150, 350, 400, 450, 650, 730, 780, 840, 930, 948 , 970, 993, 1005, 1019, 1023}; // 0 - 1023
bool init_ok;
public:
Sensor_LDR(int p);
void init();
void setSettings(unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void sensorloop();
};
#endif

View File

@ -82,8 +82,6 @@ monitor_port = /dev/ttyUSB0
monitor_speed = 115200 monitor_speed = 115200
build_flags = build_flags =
-D SENSOR_HCSR501
-D SENSOR_HCSR501_PIN=D0
-D SENSOR_RADAR -D SENSOR_RADAR
@ -91,6 +89,9 @@ build_flags =
-D SENSOR_RADAR_readdelay=100 -D SENSOR_RADAR_readdelay=100
-D SENSOR_RADAR_senddelaymax=1000*60*10 -D SENSOR_RADAR_senddelaymax=1000*60*10
-D SENSOR_LDR
-D SENSOR_LDR_PIN=A0
lib_deps = lib_deps =
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
@ -120,10 +121,10 @@ build_flags =
-D SENSOR_LDR -D SENSOR_LDR
-D SENSOR_LDR_CALIB1 -D SENSOR_LDR_CALIB1
-D LDR_PIN=A0 -D SENSOR_LDR_PIN=A0
-D dataLDR_minchange=10.0 -D SENSOR_LDR_minchange=10.0
-D dataLDR_readdelay=1000*2 -D SENSOR_LDR_readdelay=1000*2
-D dataLDR_senddelaymax=1000*60*1 -D SENSOR_LDR_senddelaymax=1000*60*1
lib_deps = lib_deps =

View File

@ -173,17 +173,19 @@
#endif #endif
#endif #endif
#ifdef SENSOR_LDR #ifdef SENSOR_LDR
struct sensordata dataLDR; #include "sensor_ldr.cpp"
float value_ldr=0; Sensor_LDR sensor_ldr(SENSOR_LDR_PIN);
#ifdef SENSOR_LDR_CALIB1
#define LDRARRAYSIZE 18 #ifndef SENSOR_LDR_minchange
//black wire of ldr connects to A0 with 10k to gnd. red wire connects with 1k to gnd and 2k2 to 3v3 #define SENSOR_LDR_minchange 10.0
static const unsigned int out_ldr[] = {0, 30, 50, 60, 130, 170, 250, 420, 780, 1300,2600, 5000, 5350, 7700, 10900, 12000, 17000,20000}; // x10 (i.e. gets later divided by 10) #endif
static const unsigned int in_ldr[] = {0, 12, 100, 150, 350, 400, 450, 650, 730, 780, 840, 930, 948 , 970, 993, 1005, 1019, 1023}; // 0 - 1023 #ifndef SENSOR_LDR_senddelaymax
#define SENSOR_LDR_senddelaymax 1000*60*1
#endif
#ifndef SENSOR_LDR_readdelay
#define SENSOR_LDR_readdelay 1000*2
#endif #endif
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size); //for analog ldr light calculation
#endif #endif
#ifdef SENSOR_MHZ19 #ifdef SENSOR_MHZ19
@ -406,17 +408,8 @@ void setup() {
#endif #endif
#ifdef SENSOR_LDR #ifdef SENSOR_LDR
Serial.println("initializing ldr"); sensor_ldr.init();
pinMode(LDR_PIN, INPUT); //ldr sensor_ldr.setSettings(SENSOR_RADAR_senddelaymax,SENSOR_RADAR_readdelay);
#ifdef dataLDR_readdelay
dataLDR.readdelay=dataLDR_readdelay;
#endif
#ifdef dataLDR_senddelaymax
dataLDR.senddelaymax=dataLDR_senddelaymax;
#endif
#ifdef dataLDR_minchange
dataLDR.minchange=dataLDR_minchange;
#endif
#endif #endif
#ifdef SENSOR_MHZ19 #ifdef SENSOR_MHZ19
@ -570,8 +563,7 @@ void setup() {
#endif #endif
#ifdef SENSOR_LDR #ifdef SENSOR_LDR
sensorNode.advertise("light"); sensor_ldr.advertise(sensorNode);
analogRead(LDR_PIN); //first reading could be false
#endif #endif
@ -627,36 +619,6 @@ void loop() {
Homie.loop(); Homie.loop();
} }
#ifdef SENSOR_LDR
void loop_LDR()
{
sensordata &d=dataLDR;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_ldr = get_lux(in_ldr, out_ldr, LDRARRAYSIZE)/10.0; //read light level in lux
if (fabs(d.lastsentvalue-value_ldr)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending LDR. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "light " << ": " << value_ldr << endl;
sensorNode.setProperty("light").send(String(value_ldr));
d.lastsentvalue=value_ldr;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_MHZ19 #ifdef SENSOR_MHZ19
void loop_MHZ19() void loop_MHZ19()
{ {
@ -1045,7 +1007,7 @@ void loopHandler() {
#endif #endif
#ifdef SENSOR_LDR #ifdef SENSOR_LDR
loop_LDR(); sensor_ldr.sensorloop();
#endif #endif
@ -1093,43 +1055,6 @@ void checkESPStatus()
} }
//////////////////////////////////////////////////////////////////////////////
// Calculate lux based on rawADC reading from LDR returns value in lux/10
//////////////////////////////////////////////////////////////////////////////
//quelle: https://groups.google.com/forum/#!topic/souliss/1kMAltPB2ME[1-25]
#ifdef SENSOR_LDR
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size)
{
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
unsigned int val = analogRead(LDR_PIN);
#ifdef DEBUG //DEBUG++++++++++++++++
Serial.print("LDR RAW=: ");
Serial.println(val);
#endif
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
#ifdef SENSOR_MHZ19 #ifdef SENSOR_MHZ19
byte mhz19_getCheckSum(byte* packet) { byte mhz19_getCheckSum(byte* packet) {