forked from melkati/CO2-Gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CO2_Gadget_BLE.h
151 lines (139 loc) · 4.91 KB
/
CO2_Gadget_BLE.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef CO2_Gadget_BLE_h
#define CO2_Gadget_BLE_h
#ifdef SUPPORT_BLE
#include "Sensirion_Gadget_BLE.h"
#include "WifiMultiLibraryWrapper.h"
// clang-format on
NimBLELibraryWrapper lib;
WifiMultiLibraryWrapper wifi;
DataProvider provider(lib, DataType::T_RH_CO2, true, true, true, &wifi);
#endif
void setBLEHistoryInterval(uint64_t interval) {
#ifdef SUPPORT_BLE
if (provider.getHistoryInterval() != interval * 1000) {
#ifdef DEBUG_BLE
Serial.flush();
Serial.println("-->[BLE ] Actual history interval: " + String(provider.getHistoryInterval() / 1000) + " seconds");
Serial.println("-->[BLE ] Setting history interval to: " + String(interval) + " seconds");
delay(20);
#endif
provider.setHistoryInterval(interval * 1000);
}
#endif
}
void initBLE() {
#ifdef SUPPORT_BLE
if (activeBLE) {
if (bleInitialized) {
Serial.print(
"-->[BLE ] Sensirion Gadget BLE Lib already initialized with deviceId = ");
Serial.println(provider.getDeviceIdString());
return;
} else {
setBLEHistoryInterval(sampleInterval);
provider.begin();
Serial.print("-->[BLE ] Sensirion Gadget BLE Lib initialized with deviceId = ");
Serial.println(provider.getDeviceIdString());
Serial.print("-->[BLE ] History interval set to: ");
Serial.print(provider.getHistoryInterval() / 1000);
Serial.println(" seconds");
// Set initial battery level
provider.setBatteryLevel(batteryLevel);
bleInitialized = true;
}
}
#endif
}
/**
* @brief Publishes sensor data over BLE (Bluetooth Low Energy).
*
* This function is responsible for publishing sensor data over BLE if the BLE support is enabled and the sensor readings are within the valid range.
* The sensor data includes CO2 level, temperature, and humidity.
*
* @note This function should be called periodically to publish the sensor data.
*/
void publishBLE() {
static int64_t lastMeasurementTimeMs = 0;
static int measurementIntervalMs = 1000;
static int64_t lastBatteryLevelUpdateMs = 0;
static int batteryLevelUpdateIntervalMs = 60000;
#ifdef SUPPORT_BLE
if (isDownloadingBLE) {
return;
}
if (millis() - lastMeasurementTimeMs >= measurementIntervalMs) {
if ((activeBLE) && (co2 >= 400) && (co2 <= 5000) && (temp >= -40) && (temp <= 85) && (hum >= 0) && (hum <= 100)) {
provider.writeValueToCurrentSample(co2, SignalType::CO2_PARTS_PER_MILLION);
provider.writeValueToCurrentSample(temp, SignalType::TEMPERATURE_DEGREES_CELSIUS);
provider.writeValueToCurrentSample(hum, SignalType::RELATIVE_HUMIDITY_PERCENTAGE);
provider.commitSample();
lastMeasurementTimeMs = millis();
}
#ifdef DEBUG_BLE
Serial.println("-->[BLE ] Sent CO2: " + String(co2) + " ppm, Temp: " + String(temp) + " C, Hum: " + String(hum) + " %");
publishMQTTLogData("-->[BLE ] Sent CO2: " + String(co2) + " ppm, Temp: " + String(temp) + " C, Hum: " + String(hum) + " %");
delay(20);
#endif
}
if (millis() - lastBatteryLevelUpdateMs >= batteryLevelUpdateIntervalMs) {
lastBatteryLevelUpdateMs = millis();
if (batteryLevel == 0) {
batteryLevel = 100;
}
provider.setBatteryLevel(batteryLevel);
#ifdef DEBUG_BLE
Serial.println("-->[BLE ] Sent Battery Level: " + String(batteryLevel) + "%");
publishMQTTLogData("-->[BLE ] Sent Battery Level: " + String(batteryLevel) + "%");
delay(20);
#endif
}
#endif
}
void handleBLEwifiChanged() {
#ifdef SUPPORT_BLE
wifiSSID = provider.getWifiSSID();
wifiPass = provider.getWifiPassword();
wifiSSID.trim();
wifiPass.trim();
wifiChanged = true;
activeWIFI = true;
Serial.println("-->[BLE ] Wifi SSID changed to: #" + wifiSSID + "#");
#ifndef WIFI_PRIVACY
Serial.println("-->[BLE ] Wifi password changed to: #" + wifiPass + "#");
#endif
delay(5);
#endif
}
void handleFrcRequest() {
#ifdef SUPPORT_BLE
if (!provider.isFRCRequested()) {
return;
}
calibrationValue = provider.getReferenceCO2Level();
pendingCalibration = true;
Serial.print("[BLE ] Received FRC request (calibration) with reference CO2 level: ");
Serial.println(calibrationValue);
delay(5);
provider.completeFRCRequest();
#endif
}
void BLELoop() {
#ifdef SUPPORT_BLE
int connectTries = 0;
if (!activeBLE) {
return;
}
provider.handleDownload();
isDownloadingBLE = provider.isDownloading();
if (isDownloadingBLE) return;
// delay(3);
if (provider.wifiChanged()) handleBLEwifiChanged();
if (provider.historyIntervalChanged()) {
Serial.print("-->[BLE ] History interval changed to: ");
Serial.print(provider.getHistoryInterval() / 1000);
Serial.println(" seconds");
}
handleFrcRequest();
#endif
}
#endif // CO2_Gadget_BLE_h