-
Notifications
You must be signed in to change notification settings - Fork 4
/
co2.cpp
92 lines (68 loc) · 2.27 KB
/
co2.cpp
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
/*****************
Get CO2 value
*****************/
#include <Arduino.h>
#include "s8_uart.h"
/* BEGIN CONFIGURATION */
#define DEBUG_BAUDRATE 115200
#if (defined USE_SOFTWARE_SERIAL || defined ARDUINO_ARCH_RP2040)
#define S8_RX_PIN 5 // Rx pin which the S8 Tx pin is attached to (change if it is needed)
#define S8_TX_PIN 4 // Tx pin which the S8 Rx pin is attached to (change if it is needed)
#else
#define S8_UART_PORT 1 // Change UART port if it is needed
#endif
/* END CONFIGURATION */
#ifdef USE_SOFTWARE_SERIAL
SoftwareSerial S8_serial(S8_RX_PIN, S8_TX_PIN);
#else
#if defined(ARDUINO_ARCH_RP2040)
REDIRECT_STDOUT_TO(Serial) // to use printf (Serial.printf not supported)
UART S8_serial(S8_TX_PIN, S8_RX_PIN, NC, NC);
#else
HardwareSerial S8_serial(S8_UART_PORT);
#endif
#endif
S8_UART *sensor_S8;
S8_sensor sensor;
void setup() {
// Configure serial port, we need it for debug
Serial.begin(DEBUG_BAUDRATE);
// Wait port is open or timeout
int i = 0;
while (!Serial && i < 50) {
delay(10);
i++;
}
// First message, we are alive
Serial.println("");
Serial.println("Init");
// Initialize S8 sensor
S8_serial.begin(S8_BAUDRATE);
sensor_S8 = new S8_UART(S8_serial);
// Check if S8 is available
sensor_S8->get_firmware_version(sensor.firm_version);
int len = strlen(sensor.firm_version);
if (len == 0) {
Serial.println("SenseAir S8 CO2 sensor not found!");
while (1) { delay(1); };
}
// Show basic S8 sensor info
Serial.println(">>> SenseAir S8 NDIR CO2 sensor <<<");
printf("Firmware version: %s\n", sensor.firm_version);
sensor.sensor_id = sensor_S8->get_sensor_ID();
Serial.print("Sensor ID: 0x"); printIntToHex(sensor.sensor_id, 4); Serial.println("");
Serial.println("Setup done!");
Serial.flush();
}
void loop() {
//printf("Millis: %lu\n", millis());
// Get CO2 measure
sensor.co2 = sensor_S8->get_co2();
printf("CO2 value = %d ppm\n", sensor.co2);
//Serial.printf("/*%u*/\n", sensor.co2); // Format to use with Serial Studio program
// Compare with PWM output
//sensor.pwm_output = sensor_S8->get_PWM_output();
//printf("PWM output = %0.0f ppm\n", (sensor.pwm_output / 16383.0) * 2000.0);
// Wait 5 second for next measure
delay(5000);
}