-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor_read.ino
155 lines (144 loc) · 4.4 KB
/
sensor_read.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
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
void sensor_read()
{
if (!check_xchips()) {
//Serial.println("chip not there");
xchip_setup();
}
//#ifdef _MICROBIT_V2_
Wire.begin();
//#endif
if (ack._sg35) {
//Serial.println("SG35 ACK");
if (ack._sn01_gps_ready || mode == 1) {
//Serial.println("SN01 GPS READY");
if (!ack._sg35_awake) {
SG35.begin();
//Serial.println("SG35 ENABLE");
SG35.enable();
ack._sg35_awake = true;
}
uint8_t sg35Count = 0;
while (!SG35.read(sg35Data)) {
sg35Count++;
if (sg35Count == 20) break;
}
if (sg35Count < 20) {
data.pm10 = String(sg35Data.PM_AE_UG_1_0);
data.pm25 = String(sg35Data.PM_AE_UG_2_5);
data.pm100 = String(sg35Data.PM_AE_UG_10_0);
}
} else {
SG35.disable();
ack._sg35_awake = false;
data.pm10 = "";
data.pm25 = "";
data.pm100 = "";
}
} else {
data.pm10 = "";
data.pm25 = "";
data.pm100 = "";
}
if (mode == 2) {
if (ack._sn01) {
SN01.poll();
data.day = String(SN01.getDay());
data.month = String(SN01.getMonth());
data.year = String(SN01.getYear());
data.hours = String(SN01.getHour());
data.minutes = String(SN01.getMinute());
data.seconds = String(SN01.getSecond());
data.sats = String(SN01.getSatelitesConnected());
float _lat = SN01.getLatitude();
float _long = SN01.getLongitude();
data.latitude = String(_lat, 6);
data.longitude = String(_long, 6);
data.latitudedms = DegreesToDegMinSec(_lat);
data.longitudedms = DegreesToDegMinSec(_long);
data.altitude = String(SN01.getAltitude());
data.hdop = String((int)SN01.getHDOP());
data.speed = String(SN01.getCOG());
if ((atof(data.hdop.c_str()) < 20 && atof(data.hdop.c_str()) > 0 ) || (millis() > 600000UL)) {
////Serial.println("HDOP: " + data.hdop);
ack._sn01_gps_ready = true;
} else {
data.latitude = "";
data.longitude = "";
data.latitudedms = "";
data.longitudedms = "";
data.altitude = "";
data.speed = "";
}
} else {
data.latitude = "";
data.longitude = "";
data.latitudedms = "";
data.longitudedms = "";
data.altitude = "";
data.hdop = "";
data.speed = "";
data.day = "";
data.month = "";
data.year = "";
data.hours = "";
data.minutes = "";
data.seconds = "";
}
}
if (ack._sw01) {
SW01.poll();
data.temperature = String(SW01.getTempC());
data.humidity = String(SW01.getHumidity());
data.pressure = String(SW01.getPressure() / 100);
} else {
data.temperature = "";
data.humidity = "";
data.pressure = "";
}
if (ack._sg33) {
if (xCore.ping(_sg33_i2c)) {
if (SG33.dataAvailable())
{
SG33.getAlgorithmResults();
if (ack._sw01) {
SG33.setEnvironmentData(atof(data.humidity.c_str()), atof(data.temperature.c_str()));
}
data.co2 = String(SG33.getCO2());
data.tvoc = String(SG33.getTVOC());
} else {
// data.co2 = "";
// data.tvoc = "";
}
} else if (xCore.ping(_sg33_i2c_1)) {
if (ack._sw01) {
sgp.setHumidity(getAbsoluteHumidity(atof(data.temperature.c_str()), atof(data.humidity.c_str())));
}
if (sgp.IAQmeasure()) {
data.co2 = String(sgp.eCO2);
data.tvoc = String(sgp.TVOC);
} else {
// data.co2 = "";
// data.tvoc = "";
}
}
} else {
data.co2 = "";
data.tvoc = "";
}
}
String DegreesToDegMinSec(float x)
{
double decimal_degrees = (double)x;
double fractional_degrees, integer_degrees;
fractional_degrees = modf(decimal_degrees, &integer_degrees);
int degrees = (int)integer_degrees;
int minutes = (int)(fractional_degrees * 60);
double seconds = minutes * 60;
return String(degrees) + "°" + String(minutes) + "'" + String(seconds, 2) + "\"";
}
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
// approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
return absoluteHumidityScaled;
}