-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuminosityNode.cpp
More file actions
55 lines (45 loc) · 1.21 KB
/
LuminosityNode.cpp
File metadata and controls
55 lines (45 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "LuminosityNode.hpp"
LuminosityNode::LuminosityNode(const char* name)
:
m_name(name),
m_intervalRead(0),
m_intervalPublish(0),
m_lastLuxValue(-1),
m_lastLuxValuePublish(-1),
m_timeLastPublish(0)
{
m_node = new HomieNode(name, "luminosity");
}
void LuminosityNode::begin(unsigned long intervalRead, unsigned long intervalPublish)
{
m_intervalRead = intervalRead;
m_intervalPublish = intervalPublish;
m_sensor.begin();
}
void LuminosityNode::setup()
{
m_node->setProperty("unit").send("lux");
}
void LuminosityNode::loop()
{
unsigned long now = millis();
if(now - m_timeLastRead >= m_intervalRead * 1000UL || m_timeLastRead == 0)
{
m_lastLuxValue = m_sensor.readLightLevel();
m_timeLastRead = millis();
if(now - m_timeLastPublish >= m_intervalPublish * 1000UL || m_timeLastPublish == 0)
{
Homie.getLogger() << m_name << ": Luminosity is" << m_lastLuxValue << " lux" << endl;
if(m_lastLuxValue != m_lastLuxValuePublish)
{
m_node->setProperty("lux").send(String(m_lastLuxValue));
m_lastLuxValuePublish = m_lastLuxValue;
}
m_timeLastPublish = millis();
}
}
}
float LuminosityNode::getLastValue() const
{
return m_lastLuxValue;
}