-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead_Sensor
180 lines (158 loc) · 4 KB
/
Read_Sensor
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
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
// int i = 0;
int C02H;
int C02L;
int formaldehydeH;
int formaldehydeL;
int TVOCH;
int TVOCL;
int PM25H;
int PM25L;
int PM10H;
int PM10L;
int TempH;
int TempL;
int HumdH;
int HumdL;
int myval;
String combineTwoIntIntoOneString(byte value1,byte value2){
String string1 = String(value1);
String string2 = String(value2);
String string3;
string3 = string1 + "," + string2;
//Serial.println(string1);
//Serial.println(string2);
//Serial.println(string3);
String y;
//char *endptr;
y = string3;
// Serial.println(x / 10, 2);
return y;
}
int combineTwoHexIntoOneInt(byte value1,byte value2){
String string1 = String(value1, HEX);
String string2 = String(value2, HEX);
String string3;
long int li1;
long int li2;
long int y;
char *endptr;
li1 = strtol(string1.c_str(), &endptr, 16);
li2 = strtol(string2.c_str(), &endptr, 16);
//string3 = string4 + string2;
y = (li1*256)+li2;
// Serial.println(x / 10, 2);
return y;
}
void parseDataStream(byte X, int i) {
if (i == 2) {
C02H = X;
}
if (i == 3) {
C02L = X;
// Serial.print("C02 HEX Value: ");
// Serial.print(C02H, HEX);
// Serial.print(" ");
// Serial.println(C02L, HEX);
Serial.print("C02 : ");
Serial.println(combineTwoHexIntoOneInt(C02H,C02L));
}
if (i == 4) {
formaldehydeH = X;
}
if (i == 5) {
formaldehydeL = X;
// Serial.print("FormaldehydeH HEX Value: ");
// Serial.print(formaldehydeH, HEX);
// Serial.print(" ");
// Serial.println(formaldehydeL, HEX);
Serial.print("Formaldehyde : ");
// Serial.println(formaldehydeH * 256 + formaldehydeL);
Serial.println(combineTwoHexIntoOneInt(formaldehydeH,formaldehydeL));
}
if (i == 6) {
TVOCH = X;
}
if (i == 7) {
TVOCL = X;
// Serial.print("TVOC HEX Value: ");
// Serial.print(TVOCH, HEX);
// Serial.print(" ");
// Serial.println(TVOCL, HEX);
Serial.print("TVOC : ");
// Serial.println(TVOCH * 256 + TVOCL);
Serial.println(combineTwoHexIntoOneInt(TVOCH,TVOCL));
}
if (i == 8) {
PM25H = X;
}
if (i == 9) {
PM25L = X;
// Serial.print("PM2.5 HEX Value: ");
// Serial.print(PM25H, HEX);
// Serial.print(" ");
// Serial.println(PM25L, HEX);
Serial.print("PM2.5 : ");
// Serial.println(PM25H * 256 + PM25L);
Serial.println(combineTwoHexIntoOneInt(PM25H,PM25L));
}
if (i == 10) {
PM10H = X;
}
if (i == 11) {
PM10L = X;
// Serial.print("PM10 HEX Value: ");
// Serial.print(PM10H, HEX);
// Serial.print(" ");
// Serial.println(PM10L, HEX);
Serial.print("PM10 : ");
// Serial.println(PM10H * 256 + PM10L);
Serial.println(combineTwoHexIntoOneInt(PM10H,PM10L));
}
if (i == 12) {
TempH = X;
}
if (i == 13) {
TempL = X;
//Serial.print("temph");
//Serial.println(TempH);
//Serial.print("templ");
//Serial.println(TempL);
// Serial.print("Temperature HEX Value: ");
// Serial.print(TempH, HEX);
// Serial.print(" ");
// Serial.println(TempL, HEX);
Serial.print("Temperature : ");
// Serial.println((TempH * 256 + TempL));
Serial.println(combineTwoIntIntoOneString(TempH,TempL));
//For Negative Values, a bit buggy tho
// Serial.println((TempH << 8) | TempL);
}
if (i == 14) {
HumdH = X;
}
if (i == 15) {
HumdL = X;
// Serial.print("Humidity HEX Value: ");
// Serial.print(HumdH, HEX);
// Serial.print(" ");
// Serial.println(HumdL, HEX);
Serial.print("Humidity : ");
// Serial.println((HumdH+ HumdL));
Serial.println(combineTwoIntIntoOneString(HumdH,HumdL));
}
}
void loop() {
for (int k = 0; k < 17; k++) {
while (!Serial1.available())
; // wait for a character
int incomingByte = Serial1.read();
// Serial.print(incomingByte,HEX);
// Serial.print(' ');
parseDataStream(incomingByte, k);
}
Serial.println();
}