-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCO2SensorStore.ino
130 lines (107 loc) · 3.63 KB
/
CO2SensorStore.ino
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
#include "SoftwareSerial.h"
SoftwareSerial K_30_Serial(12,13); //Sets up a virtual serial port. Using pin 12 for Rx and pin 13 for Tx
byte readCO2[] = {0xFE, 0X44, 0X00, 0X08, 0X02, 0X9F, 0X25}; //Command packet to read Co2 (see app note)
byte response[] = {0,0,0,0,0,0,0}; //create an array to store the response
//multiplier for value. default is 1. set to 3 for K-30 3% and 10 for K-33 ICB
int valMultiplier = 1;
#include <Wire.h>
#include "SparkFunMPL3115A2.h"
#include <SPI.h>
#include <SD.h>
MPL3115A2 myPressure;
const int chipSelect = 10;
File myFile;
void setup() // put your setup code here, to run once:
{
Serial.begin(9600); //Opens the main serial port to communicate with the computer
K_30_Serial.begin(9600); //Opens the virtual serial port with a baud of 9600
myPressure.begin(); // Get sensor online
myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
Serial.print("Initializing SD card...");
if (SD.exists("example.txt"))
{
Serial.println("example.txt exists.");
} else
{
Serial.println("example.txt doesn't exist.");
}
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close();
}
void loop()
{
sendRequest(readCO2);
unsigned long valCO2 = getValue(response);
Serial.print("Co2 ppm = ");
Serial.println(valCO2);
delay(2000);
float altitude = myPressure.readAltitudeFt();
Serial.print(" Altitude(ft):");
Serial.print(altitude, 2);
float pressure = myPressure.readPressure();
Serial.print("Pressure(Pa):");
Serial.print(pressure, 2);
float temperature = myPressure.readTempF();
Serial.print(" Temp(f):");
Serial.print(temperature, 2);
Serial.println();
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++)
{
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(2000);
}
void sendRequest(byte packet[])
{
while(!K_30_Serial.available()) //keep sending request until we start to get a response
{
K_30_Serial.write(readCO2,7);
delay(50);
}
int timeout=0; //set a timeoute counter
while(K_30_Serial.available() < 7 ) //Wait to get a 7 byte response
{
timeout++;
if(timeout > 10) //if it takes to long there was probably an error
{
while(K_30_Serial.available()) //flush whatever we have
K_30_Serial.read();
break; //exit and try again
}
delay(50);
}
for (int i=0; i < 7; i++)
{
response[i] = K_30_Serial.read();
}
}
unsigned long getValue(byte packet[])
{
int high = packet[3]; //high byte for value is 4th byte in packet in the packet
int low = packet[4]; //low byte for value is 5th byte in the packet
unsigned long val = high*256 + low; //Combine high byte and low byte with this formula to get value
return val* valMultiplier;
}