aboutsummaryrefslogtreecommitdiff
path: root/main.ino
blob: 85977029581619ec6e413dd0476ef1de80e4155c (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <Arduino.h>
#include <SensirionI2CScd4x.h>
#include <sps30.h>
#include <Wire.h>
 
#define LILYGO_T5_V213 // needed for the display libraries 
 
#include <boards.h>
#include <GxEPD.h>
#include <GxGDEH0213B73/GxGDEH0213B73.h>  // screen model, probably correct 
#include <Fonts/FreeMonoBold9pt7b.h>
#include <GxIO/GxIO_SPI/GxIO_SPI.h>
#include <GxIO/GxIO.h>
 
GxIO_Class io(SPI, EPD_CS, EPD_DC, EPD_RSET);
GxEPD_Class display(io, EPD_RSET, EPD_BUSY);
SPIClass SDSPI(VSPI); // for sdcard 
 
SensirionI2CScd4x scd4x;
 
uint16_t co2 = 0;
float temperature = 0.0f;
float humidity = 0.0f;
struct sps30_measurement m;
 
void showThings(bool all=false) {
  display.fillScreen(GxEPD_WHITE);
  display.setCursor(0,20);
 
  display.print("Co2:");
  display.println(co2);
  display.print("T:");
  display.print(temperature);
  display.print(" H:");
  display.println(humidity);
 
  display.print("PM 1:");
  display.print(m.mc_1p0);
  display.print(" PM 2.5:");
  display.println(m.mc_2p5);
 
  display.print("PM 4:");
  display.print(m.mc_4p0);
  display.print(" PM 10:");
  display.println(m.mc_10p0);
 
  // "partial" update 
  display.updateWindow(00, GxEPD_WIDTH, GxEPD_HEIGHT, false);
}
 
unsigned long startTime;
bool frcDone = false;
 
void setup() {
 
    Serial.begin(115200);
    while (!Serial) {
        delay(100);
    }
 
    // the board's definition says SCL=23, but I have SCL=22 
    Wire.begin(21,22);
 
    SPI.begin(EPD_SCLK, EPD_MISO, EPD_MOSI);
 
    display.init();
    display.setTextColor(GxEPD_BLACK);
    display.setRotation(3);
    display.setFont(&FreeMonoBold9pt7b);
 
    showThings();
 
    uint16_t error;
    char errorMessage[256];
 
    scd4x.begin(Wire);
 
    // stop potentially previously started measurement 
    error = scd4x.stopPeriodicMeasurement();
    if (error) {
        Serial.print("Error trying to execute stopPeriodicMeasurement(): ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    }
 
    error = scd4x.setAutomaticSelfCalibration(1);
    if (error) {
        Serial.print("Error trying to execute setAutomaticSelfCalibration(): ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    }
    
    // Start Measurement 
    error = scd4x.startPeriodicMeasurement();
    if (error) {
        Serial.print("Error trying to execute startPeriodicMeasurement(): ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    }
 
    startTime = millis();
 
    // this will try to re-init Wire, emit a warning, and carry on 
    sensirion_i2c_init();
 
    while (sps30_probe() != 0{
      Serial.print("SPS sensor probing failed\n");
      delay(500);
    }
 
    int16_t ret;
    uint8_t auto_clean_days = 4;
    uint32_t auto_clean;
 
    ret = sps30_set_fan_auto_cleaning_interval_days(auto_clean_days);
    if (ret) {
      Serial.print("error setting the auto-clean interval: ");
      Serial.println(ret);
    }
 
    ret = sps30_start_measurement();
    if (ret < 0{
      Serial.print("error starting measurement\n");
    }
 
    Serial.println("Waiting for first measurement... (5 sec)");
}
 
void loop() {
    uint16_t error;
    char errorMessage[256];
 
    delay(5000);
    Serial.println(millis());
 
    if (!frcDone && (millis() - startTime > (3*60 + 30)*1000)) { // 3½ minutes 
      Serial.println("calibration");
      frcDone=true;
      display.fillScreen(GxEPD_WHITE);
      display.setCursor(0,20);
      display.print("FRC");
      display.updateWindow(00, GxEPD_WIDTH, GxEPD_HEIGHT, false);
 
      scd4x.stopPeriodicMeasurement();
      delay(500);
 
      uint16_t frcCorrection;
      error = scd4x.performForcedRecalibration(420,frcCorrection);
      if (error != 0{
        Serial.print("scd4x error ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
      }
      Serial.println(frcCorrection);
      Serial.println(frcCorrection - 0x8000);
 
      display.fillScreen(GxEPD_WHITE);
      display.setCursor(0,20);
      display.print("FRC ");display.print(frcCorrection);
      display.updateWindow(00, GxEPD_WIDTH, GxEPD_HEIGHT, false);
      delay(500);
      scd4x.startPeriodicMeasurement();
      delay(2000);
    }
    
    error = scd4x.readMeasurement(co2, temperature, humidity);
    if (error != 0{
      Serial.print("scd4x error ");
      errorToString(error, errorMessage, 256);
      Serial.println(errorMessage);
    }
 
    char serial[SPS30_MAX_SERIAL_LEN];
    uint16_t data_ready;
    int16_t ret;
 
    do {
      ret = sps30_read_data_ready(&data_ready);
      if (ret < 0{
        Serial.print("error reading data-ready flag: ");
        Serial.println(ret);
      } else if (!data_ready)
        Serial.print("data not ready, no new measurement available\n");
      else
        break;
      delay(100); /* retry in 100ms */
    } while (1);
 
    ret = sps30_read_measurement(&m);
 
    if (error || ret <0{
      return;
    }
 
    showThings();
}