-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSensor.cpp
More file actions
116 lines (102 loc) · 3.5 KB
/
Sensor.cpp
File metadata and controls
116 lines (102 loc) · 3.5 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
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
#include "Sensor.hpp"
#include "config.h"
#include <esp_adc_cal.h>
bool Sensor::getSCD40MeasurementResult() {
// Read Measurement SCD40
// uint16_t co2 = 0;
// float temperature = 0.0f;
// float humidity = 0.0f;
bool isDataReady = false;
uint16_t error = _scd4x.getDataReadyFlag(isDataReady);
if (error) {
errorToString(error, _errorMessage, 256);
log_w("Error trying to execute getDataReadyFlag(): %s", _errorMessage);
return false;
}
if (!isDataReady) {
return false;
}
error = _scd4x.readMeasurement(scd40.co2, scd40.temperature, scd40.humidity);
if (error) {
errorToString(error, _errorMessage, 256);
log_w("Error trying to execute readMeasurement(): %s", _errorMessage);
return false;
} else if (scd40.co2 == 0) {
log_w("Invalid sample detected, skipping.");
return false;
} else {
log_d("SCD40 Measurement Result:");
log_d(" Co2: %d ppm", scd40.co2);
log_d(" Temperature: %f °C", scd40.temperature);
log_d(" Humidity: %f %RH", scd40.humidity);
return true;
}
return false;
}
bool Sensor::getSEN55MeasurementResult() {
// Read Measurement SEN55
uint16_t error = _sen5x.readMeasuredValues(
sen55.massConcentrationPm1p0,
sen55.massConcentrationPm2p5,
sen55.massConcentrationPm4p0,
sen55.massConcentrationPm10p0,
sen55.ambientHumidity,
sen55.ambientTemperature,
sen55.vocIndex,
sen55.noxIndex
);
if (error) {
errorToString(error, _errorMessage, 256);
log_w("Error trying to execute readMeasuredValues(): %s", _errorMessage);
return false;
} else {
log_d("SEN55 Measurement Result:");
log_d(" PM1.0: %f µg/m³", sen55.massConcentrationPm1p0);
log_d(" PM2.5: %f µg/m³", sen55.massConcentrationPm2p5);
log_d(" PM4.0: %f µg/m³", sen55.massConcentrationPm4p0);
log_d(" PM10.0: %f µg/m³", sen55.massConcentrationPm10p0);
if (isnan(sen55.ambientHumidity)) {
log_d(" AmbientHumidity: n/a");
} else {
log_d(" AmbientHumidity: %f %RH",sen55.ambientHumidity);
}
if (isnan(sen55.ambientTemperature)) {
log_d(" AmbientTemperature: n/a");
} else {
log_d(" AmbientTemperature: %f °C", sen55.ambientTemperature);
}
if (isnan(sen55.vocIndex)) {
log_d(" VOC Index: n/a");
} else {
log_d(" VOC Index: %f", sen55.vocIndex);
}
if (isnan(sen55.noxIndex)) {
log_d(" NOx Index: n/a");
} else {
log_d(" NOx Index: %f", sen55.noxIndex);
}
return true;
}
return false;
}
void Sensor::getBatteryVoltageRaw() {
esp_adc_cal_characteristics_t adc_chars;
esp_adc_cal_characterize(
ADC_UNIT_1,
ADC_ATTEN_DB_11,
ADC_WIDTH_BIT_12,
1100,
&adc_chars
);
battery.raw = esp_adc_cal_raw_to_voltage(analogRead(BAT_ADC_PIN), &adc_chars);
}
void Sensor::getTimeString() {
I2C_BM8563_TimeTypeDef time;
_bm8563.getTime(&time);
sprintf(this->time.time, "%02d:%02d", time.hours, time.minutes);
}
void Sensor::getDateString() {
I2C_BM8563_DateTypeDef date;
_bm8563.getDate(&date);
sprintf(this->time.date, "%04d-%02d-%02d", date.year, date.month, date.date);
}