move bh1750 to class

master
interfisch 1 year ago
parent b88fa10d53
commit ea0f2183d2
  1. 65
      include/sensor_bh1750.cpp
  2. 34
      include/sensor_bh1750.h
  3. 9
      platformio.ini
  4. 71
      src/main.cpp

@ -0,0 +1,65 @@
//Connect SCL to D1, SDA to D2, GND and 3v3
#include "sensor_bh1750.h"
Sensor_BH1750::Sensor_BH1750()
{
BH1750 bh1750(0x23); //0x23 if addr connected to ground (=pin open), 0x5c if addr pulled high
}
void Sensor_BH1750::init() //Things to be done during setup()
{
Serial.println("initializing bh1750");
Wire.begin();
if (bh1750->begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F("BH1750 Advanced begin"));
init_ok=true;
} else {
Serial.println(F("Error initialising BH1750"));
}
bh1750->readLightLevel(); //make first reading, could be 0
}
//Also called during setup()
void Sensor_BH1750::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data.minchange=minchange;
data.senddelaymax=senddelaymax;
data.readdelay=readdelay;
}
//Called during setup
void Sensor_BH1750::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
sensorNode->advertise("light");
}
void Sensor_BH1750::sensorloop()
{
if (init_ok) {
sensordata &d=data;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
d.value = bh1750->readLightLevel(); // [lux]
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending BH1750. 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();
}
}
}

@ -0,0 +1,34 @@
#ifndef SENSOR_BH1750_H
#define SENSOR_BH1750_H
#include "sensordata.h"
#include <Homie.h>
#include <BH1750.h>
#include <Wire.h>
class Sensor_BH1750
{
private:
BH1750 *bh1750;
HomieNode *sensorNode; //reference to HomieNode
struct sensordata data; //struct values are changed in setup()
bool init_ok;
public:
Sensor_BH1750();
void init();
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void sensorloop();
};
#endif

@ -88,10 +88,17 @@ build_flags =
-D SENSOR_HS1101_humidity_minchange=10
-D SENSOR_BH1750
-D dataBH1750_minchange=10.0
-D dataBH1750_senddelaymax=1000*60*2
lib_deps =
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
Homie@3.0.0
claws/BH1750@1.1.4
#Arbeitszimmer
[env:sensoresp1]
@ -122,7 +129,7 @@ build_flags =
-D LDR_PIN=A0
-D dataLDR_minchange=10.0
-D dataLDR_readdelay=1000*2
-D dataBH1750_senddelaymax=1000*60*1
-D dataLDR_senddelaymax=1000*60*1
lib_deps =

@ -119,16 +119,18 @@
#endif
#ifdef SENSOR_BH1750
//SCL=D1, SDA=D2
#ifndef WIRE_H
#include <Wire.h>
#define WIRE_H
#include "sensor_bh1750.cpp"
Sensor_BH1750 sensor_bh1750;
#ifndef SENSOR_BH1750_humidity_minchange
#define SENSOR_BH1750_humidity_minchange 10
#endif
#ifndef SENSOR_BH1750_humidity_senddelaymax
#define SENSOR_BH1750_humidity_senddelaymax 5*60*1000
#endif
#ifndef SENSOR_BH1750_humidity_readdelay
#define SENSOR_BH1750_humidity_readdelay 1000*10
#endif
#include <BH1750.h>
BH1750 lightMeter(0x23); //0x23 if addr connected to ground (=pin open), 0x5c if addr pulled high
bool bh1750init_ok=false;
sensordata dataBH1750;
float value_lightBH1750=0;
#endif
#ifdef SENSOR_ML8511
@ -370,20 +372,8 @@ void setup() {
#endif
#ifdef SENSOR_BH1750
Serial.println("initializing bh1750");
Wire.begin();
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F("BH1750 Advanced begin"));
bh1750init_ok=true;
} else {
Serial.println(F("Error initialising BH1750"));
}
#ifdef dataBH1750_minchange
dataBH1750.minchange=dataBH1750_minchange;
#endif
#ifdef dataBH1750_senddelaymax
dataBH1750.senddelaymax=dataBH1750_senddelaymax;
#endif
sensor_bh1750.init();
sensor_bh1750.setSettings(SENSOR_BH1750_humidity_minchange,SENSOR_BH1750_humidity_senddelaymax,SENSOR_BH1750_humidity_readdelay);
#endif
#ifdef SENSOR_ML8511
@ -565,8 +555,7 @@ void setup() {
#endif
#ifdef SENSOR_BH1750
sensorNode.advertise("light");
lightMeter.readLightLevel(); //make first reading, could be 0
sensor_bh1750.advertise(sensorNode);
#endif
#ifdef SENSOR_ML8511
@ -642,34 +631,6 @@ void loop() {
#ifdef SENSOR_BH1750
void loop_BH1750()
{
sensordata &d=dataBH1750;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_lightBH1750 = lightMeter.readLightLevel(); // [lux]
if (fabs(d.lastsentvalue-value_lightBH1750)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending BH1750. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "light " << ": " << value_lightBH1750 << endl;
sensorNode.setProperty("light").send(String(value_lightBH1750));
d.lastsentvalue=value_lightBH1750;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_ML8511
void loop_ML8511()
@ -1159,9 +1120,7 @@ void loopHandler() {
#endif
#ifdef SENSOR_BH1750
if (bh1750init_ok) {
loop_BH1750();
}
sensor_bh1750.sensorloop();
#endif
#ifdef SENSOR_ML8511

Loading…
Cancel
Save