forked from melkati/CO2-Gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCO2_Gadget_Sensors.h
118 lines (97 loc) · 3.98 KB
/
CO2_Gadget_Sensors.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
#include <Sensors.hpp>
bool firstCO2SensorInit = true;
bool pendingCalibration = false;
bool newReadingsAvailable = false;
uint16_t calibrationValue = 415;
uint16_t customCalibrationValue = 415;
bool pendingAmbientPressure = false;
uint16_t ambientPressureValue = 0;
uint16_t altidudeMeters = 600;
bool autoSelfCalibration = false;
float tempOffset = 0.0f;
uint16_t co2 = 0;
float temp, tempFahrenheit, hum = 0;
uint16_t co2OrangeRange =
700; // Default CO2 ppm concentration threshold to display values in orange
// (user can change on menu and save on preferences)
uint16_t co2RedRange =
1000; // Default CO2 ppm concentration threshold to display values in red
// (user can change on menu and save on preferences)
void onSensorDataOk() {
if (!inMenu) {
Serial.print("-->[SENS] CO2: " + sensors.getStringCO2());
Serial.print(" CO2humi: " + String(sensors.getCO2humi()));
Serial.print(" CO2temp: " + String(sensors.getCO2temp()));
Serial.print(" H: " + String(sensors.getHumidity()));
Serial.println(" T: " + String(sensors.getTemperature()));
}
co2 = sensors.getCO2();
hum = sensors.getHumidity();
if (hum == 0.0) hum = sensors.getCO2humi();
temp = sensors.getTemperature();
if (temp == 0.0) temp = sensors.getCO2temp(); // TO-DO: temp could be 0.0
tempFahrenheit = (temp * 1.8 + 32);
newReadingsAvailable = true;
}
void onSensorDataError(const char *msg) { Serial.println(msg); }
void initSensors() {
const int8_t None = -1, AUTO = 0, MHZ19 = 4, CM1106 = 5, SENSEAIRS8 = 6, DEMO=127;
if (firstCO2SensorInit) {
Serial.printf("-->[SENS] Using sensorlib v%s Rev:%d\n", CSL_VERSION, CSL_REVISION);
firstCO2SensorInit = false;
}
displayNotification("Init sensors", notifyInfo);
#ifdef ENABLE_PIN
// Turn On the Sensor (reserved for future use)
Serial.println("-->[SENS] Turning on sensor..");
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, ENABLE_PIN_HIGH);
delay(50);
#endif
// Initialize sensors
Wire.begin(I2C_SDA, I2C_SCL);
Serial.println("-->[SENS] Detecting sensors..");
uint16_t defaultCO2MeasurementInterval = 5; // TO-DO: Move to preferences
// Breaking change: https://github.com/kike-canaries/canairio_sensorlib/pull/110
// CanAirIO Sensorlib was multipliying sample time by two until rev 340 (inclusive). Adjust to avoid need for recalibration.
#ifdef CSL_REVISION // CanAirIO Sensorlib Revision > 340 (341 where CSL_REVISION was included)
if (sensors.getLibraryRevision() > 340) {
sensors.setSampleTime(defaultCO2MeasurementInterval * 2);
} else {
sensors.setSampleTime(defaultCO2MeasurementInterval);
}
#else
sensors.setSampleTime(defaultCO2MeasurementInterval);
#endif
sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback
sensors.setOnErrorCallBack(&onSensorDataError); // [optional] error callback
sensors.setDebugMode(debugSensors); // [optional] debug mode
sensors.setTempOffset(tempOffset);
// sensors.setAutoSelfCalibration(false); // TO-DO: Implement in CanAirIO
// Sensors Lib
Serial.printf("-->[SENS] Selected CO2 Sensor: %d\n", selectedCO2Sensor);
if (selectedCO2Sensor == AUTO) {
Serial.println("-->[SENS] Trying to init CO2 sensor: Auto (I2C)");
sensors.detectI2COnly(true);
sensors.init();
} else if (selectedCO2Sensor == MHZ19) {
Serial.println("-->[SENS] Trying to init CO2 sensor: MHZ19(A/B/C/D)");
sensors.detectI2COnly(false);
sensors.init(MHZ19);
} else if (selectedCO2Sensor == CM1106) {
Serial.println("-->[SENS] Trying to init CO2 sensor: CM1106");
sensors.detectI2COnly(false);
sensors.init(CM1106);
} else if (selectedCO2Sensor == SENSEAIRS8) {
Serial.println("-->[SENS] Trying to init CO2 sensor: SENSEAIRS8");
sensors.detectI2COnly(false);
sensors.init(SENSEAIRS8);
}
if (!sensors.getMainDeviceSelected().isEmpty()) {
Serial.println("-->[SENS] Sensor configured: " +
sensors.getMainDeviceSelected());
}
}
void sensorsLoop() {
sensors.loop();
}