aboutsummaryrefslogtreecommitdiff
path: root/co2.h
blob: a6a14808a23da45a0d5d44f37e34ac2fe3958f20 (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
#pragma once
 
#include <Arduino.h>
 
#include <SensirionI2CScd4x.h>
#include <Wire.h>
 
#include "data.h"
 
class CO2 {
private:
  SensirionI2CScd4x scd4x;
 
public:
  CO2() : scd4x() {}
 
  void start() {
    uint16_t error;
    char errorMessage[256];
 
    scd4x.begin(Wire);
 
    // stop potentially previously started measurement 
    error = scd4x.stopPeriodicMeasurement();
    if (error) {
        Serial.print("! CO2 stopPeriodicMeasurement() error: ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    }
 
    error = scd4x.setAutomaticSelfCalibration(1);
    if (error) {
        Serial.print("! CO2 setAutomaticSelfCalibration() error: ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    }
    
    // Start Measurement 
    error = scd4x.startLowPowerPeriodicMeasurement();
    if (error) {
        Serial.print("! CO2 startLowPowerPeriodicMeasurement() error: ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    }
  }
 
  bool dataReady() {
    uint16_t ready;
    uint16_t error;
    char errorMessage[256];
 
    error = scd4x.getDataReadyStatus(ready);
    if (error) {
        Serial.print("! CO2 getDataReadyStatus() error: ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
        return false;
    }
 
    // "If last 11 bits are 0 data not ready, else data ready" 
    return (ready & 0x7FFF) != 0;
  }
  
  void read(SensorData *data) {
    uint16_t error;
    char errorMessage[256];
 
    error = scd4x.readMeasurement(data->co2, data->temperature, data->humidity);
    if (error) {
        Serial.print("! CO2 readMeasurement() error: ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    }
  }
 
  bool serialCommand(const String &tag, const String &command) {
    if (command.startsWith("setco ")) {
      uint16_t error;
      char errorMessage[256];
 
      uint16_t target = command.substring(6).toInt();
      uint16_t correction;
 
      scd4x.stopPeriodicMeasurement();
      delay(500);
      error = scd4x.performForcedRecalibration(target, correction);
      if (error) {
        Serial.print("! CO2 performForcedRecalibration() error: ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
      }
 
      Serial.print(tag);Serial.print(" setco ");
      if (correction == 0xFFFF{
        Serial.println("failed");
      }
      else {
        Serial.println(correction - 0x8000);
      }
 
      error = scd4x.startLowPowerPeriodicMeasurement();
      if (error) {
        Serial.print("! CO2 startLowPowerPeriodicMeasurement() error: ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
      }
 
      return true;
    }
 
    return false;
  }
};