-
Notifications
You must be signed in to change notification settings - Fork 0
/
pressure.h
127 lines (109 loc) · 3.79 KB
/
pressure.h
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// pressure.h
// adapted from:
// https://github.com/adafruit/Adafruit_BMP085_Unified/blob/master/examples/sensorapi/sensorapi.pde
#pragma once
#include "scheduler.h"
#include "sensors.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <home_assistant.h>
namespace ustd {
class Pressure {
public:
String PRESSURE_VERSION = "0.1.0";
Scheduler *pSched;
int tID;
String name;
double temperatureSensorVal;
double pressureSensorVal;
bool bActive = false;
ustd::sensorprocessor temperatureSensor = ustd::sensorprocessor(4, 600, 0.1);
ustd::sensorprocessor pressureSensor = ustd::sensorprocessor(4, 600, 1.0);
Adafruit_BMP085_Unified *pPressure;
#ifdef __ESP__
HomeAssistant *pHA;
#endif
Pressure(String name) : name(name) {
pPressure = new Adafruit_BMP085_Unified(10085);
}
~Pressure() {
}
double getTemp() {
return temperatureSensorVal;
}
double getPressure() {
return pressureSensorVal;
}
void begin(Scheduler *_pSched) {
pSched = _pSched;
if (pPressure->begin()) {
bActive = true;
}
// give a c++11 lambda as callback scheduler task registration of
// this.loop():
/* std::function<void()> */
auto ft = [=]() { this->loop(); };
tID = pSched->add(ft, name, 5000000);
/* std::function<void(String, String, String)> */
auto fnall = [=](String topic, String msg, String originator) {
this->subsMsg(topic, msg, originator);
};
pSched->subscribe(tID, name + "/sensor/temperature/get", fnall);
pSched->subscribe(tID, name + "/sensor/pressure/get", fnall);
}
#ifdef __ESP__
void registerHomeAssistant(String homeAssistantFriendlyName, String projectName = "",
String homeAssistantDiscoveryPrefix = "homeassistant") {
pHA = new HomeAssistant(name, tID, homeAssistantFriendlyName, projectName, PRESSURE_VERSION,
homeAssistantDiscoveryPrefix);
pHA->addSensor("temperature", "Temperature", "\\u00B0C", "temperature", "mdi:thermometer");
pHA->addSensor("pressure", "Pressure", "hPa", "pressure", "mdi:altimeter");
pHA->begin(pSched);
}
#endif
void publishPressure() {
char buf[32];
pSched->publish(name + "/sensor/result", "OK");
sprintf(buf, "%5.1f", pressureSensorVal);
pSched->publish(name + "/sensor/pressure", buf);
}
void publishTemperature() {
char buf[32];
pSched->publish(name + "/sensor/result", "OK");
sprintf(buf, "%5.1f", temperatureSensorVal);
pSched->publish(name + "/sensor/temperature", buf);
}
void loop() {
if (bActive) {
sensors_event_t event;
pPressure->getEvent(&event);
if (event.pressure) {
double p, t;
float tf;
/* Display atmospheric pressue in hPa */
p = event.pressure; // hPa
pPressure->getTemperature(&tf);
t = (double)tf;
if (temperatureSensor.filter(&t)) {
temperatureSensorVal = t;
publishTemperature();
}
if (pressureSensor.filter(&p)) {
pressureSensorVal = p;
publishPressure();
}
}
} else {
pSched->publish(name + "/sensor/result", "hardware not initialized");
}
}
void subsMsg(String topic, String msg, String originator) {
if (topic == name + "/sensor/temperature/get") {
publishTemperature();
}
if (topic == name + "/sensor/pressure/get") {
publishPressure();
}
};
}; // Pressure
} // namespace ustd