supla-device
Toggle main menu visibility
Loading...
Searching...
No Matches
src
supla
sensor
bh1750.h
1
/*
2
Copyright (C) AC SOFTWARE SP. Z O.O
3
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License
6
as published by the Free Software Foundation; either version 2
7
of the License, or (at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
*/
18
19
// Dependency: Please install "BH1750" by Christopher Laws library in Arduino:
20
// https://github.com/claws/BH1750
21
22
#ifndef SRC_SUPLA_SENSOR_BH1750_H_
23
#define SRC_SUPLA_SENSOR_BH1750_H_
24
25
#include <supla/sensor/general_purpose_measurement.h>
26
27
#include <math.h>
28
#include <BH1750.h>
29
#include <supla/log_wrapper.h>
30
31
namespace
Supla {
32
namespace
Sensor {
33
class
Bh1750 :
public
GeneralPurposeMeasurement {
34
public
:
35
explicit
Bh1750(
int
i2cAddress = 0x23)
36
: GeneralPurposeMeasurement(
nullptr
,
false
), sensor(i2cAddress) {
37
setDefaultUnitAfterValue
(
"lx"
);
38
getChannel
()->
setDefaultIcon
(14);
39
}
40
41
void
initializeSensor() {
42
if
(sensor.begin()) {
43
SUPLA_LOG_DEBUG(
"BH1750[%d]: initialized"
,
getChannelNumber
());
44
}
else
{
45
SUPLA_LOG_WARNING(
"BH1750[%d]: not found or other error"
,
46
getChannelNumber
());
47
}
48
}
49
50
void
onInit
()
override
{
51
initializeSensor();
52
53
channel.setNewValue(
getValue
());
54
}
55
56
protected
:
57
double
getValue
()
override
{
58
double
value = sensor.readLightLevel();
59
if
(isnan(value) || value < 0) {
60
invalidCounter++;
61
if
(invalidCounter < 4) {
62
return
lastValue;
63
}
else
{
64
// try to init again
65
invalidCounter = 0;
66
initializeSensor();
67
}
68
lastValue = NAN;
69
return
lastValue;
70
}
71
invalidCounter = 0;
72
lastValue = value;
73
return
value;
74
}
75
76
BH1750 sensor;
77
78
double
lastValue = NAN;
79
int
invalidCounter = 0;
80
};
81
82
}
// namespace Sensor
83
}
// namespace Supla
84
85
#endif
// SRC_SUPLA_SENSOR_BH1750_H_
Supla::ChannelElement::getChannel
const Channel * getChannel() const override
Returns pointer to main channel.
Definition
channel_element.cpp:29
Supla::Channel::setDefaultIcon
void setDefaultIcon(uint8_t iconId)
Sets default icon.
Definition
channel.cpp:1530
Supla::Element::getChannelNumber
int getChannelNumber() const
Returns channel number.
Definition
element.cpp:177
Supla::Sensor::Bh1750::getValue
double getValue() override
Method used to obtain new value for channel.
Definition
bh1750.h:57
Supla::Sensor::Bh1750::onInit
void onInit() override
Third method called on element in SuplaDevice.begin().
Definition
bh1750.h:50
Supla::Sensor::GeneralPurposeChannelBase::setDefaultUnitAfterValue
void setDefaultUnitAfterValue(const char *unit)
Sets default unit which is displayed after value.
Definition
general_purpose_channel_base.cpp:212
Generated by
1.17.0