aboutsummaryrefslogtreecommitdiff
path: root/main.ino
blob: 604bdef9425915aa99b2c5b119a037abadaf2d66 (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
#include <Arduino.h>
#include <SensirionI2CScd4x.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/FreeMonoBold12pt7b.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? maybe? 
 
 
SensirionI2CScd4x scd4x;
 
void printUint16Hex(uint16_t value) {
    Serial.print(value < 4096 ? "0" : "");
    Serial.print(value < 256 ? "0" : "");
    Serial.print(value < 16 ? "0" : "");
    Serial.print(value, HEX);
}
 
void printSerialNumber(uint16_t serial0, uint16_t serial1, uint16_t serial2) {
    Serial.print("Serial: 0x");
    printUint16Hex(serial0);
    printUint16Hex(serial1);
    printUint16Hex(serial2);
    Serial.println();
}
 
void setup() {
 
    Serial.begin(115200);
    while (!Serial) {
        delay(100);
    }
 
    Wire.begin(21,22);
 
    SPI.begin(EPD_SCLK, EPD_MISO, EPD_MOSI);
 
    display.init();
    display.setTextColor(GxEPD_BLACK);
    display.setRotation(3);
    display.setFont(&FreeMonoBold12pt7b);
    display.fillScreen(GxEPD_WHITE);
    display.update();
 
    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);
    }
 
    uint16_t serial0;
    uint16_t serial1;
    uint16_t serial2;
    error = scd4x.getSerialNumber(serial0, serial1, serial2);
    if (error) {
        Serial.print("Error trying to execute getSerialNumber(): ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    } else {
        printSerialNumber(serial0, serial1, serial2);
    }
 
    // Start Measurement 
    error = scd4x.startPeriodicMeasurement();
    if (error) {
        Serial.print("Error trying to execute startPeriodicMeasurement(): ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    }
 
    Serial.println("Waiting for first measurement... (5 sec)");
}
 
void loop() {
    uint16_t error;
    char errorMessage[256];
 
    delay(5000);
 
    // Read Measurement 
    uint16_t co2 = 0;
    float temperature = 0.0f;
    float humidity = 0.0f;
    error = scd4x.readMeasurement(co2, temperature, humidity);
 
    display.fillScreen(GxEPD_WHITE);
    display.setCursor(0,20);
 
    if (error) {
        display.print("read error: ");
        errorToString(error, errorMessage, 256);
        display.println(errorMessage);
    } else if (co2 == 0{
        display.println("Invalid sample detected, skipping.");
    } else {
        display.print("Co2:");
        display.println(co2);
        display.print("Temperature:");
        display.println(temperature);
        display.print("Humidity:");
        display.println(humidity);
    }
 
    display.update();
}