aboutsummaryrefslogtreecommitdiff
path: root/main.ino
blob: ce9f981ce8355521fcb5f133ed9d428723ef1a63 (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
#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>
 
#include "battery.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;
 
Battery batt;
 
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.print(co2);
 
  display.setCursor(100,20);
  display.print("t");
  display.print(temperature,1);
 
  display.setCursor(150,20);
  display.print(" h");
  display.print(humidity,0);
  display.print("%");
 
  display.setCursor(0,45);
  display.println("   PM");
  display.print(" 1.0 "); display.println(m.mc_1p0,1);
  display.print(" 2.5 "); display.println(m.mc_2p5,1);
  display.print(" 4.0 "); display.println(m.mc_4p0,1);
  display.print("10.0 "); display.println(m.mc_10p0,1);
 
  display.setCursor(100,45);
  display.print("Batt "); display.print(batt.voltage());
 
  // "partial" update 
  display.updateWindow(00, GxEPD_WIDTH, GxEPD_HEIGHT, 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);
    }
 
    // 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);
 
    error = scd4x.readMeasurement(co2, temperature, humidity);
 
    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 || co2 ==0{
      return;
    }
 
    showThings();
}