-
Notifications
You must be signed in to change notification settings - Fork 1
/
*T_P_H_Sensor
184 lines (151 loc) · 3.51 KB
/
*T_P_H_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
181
182
183
184
/////////TO DO///////////
//Call full system power cycle in event of fatal sensor error?
#include <Wire.h>
#include <BMx280I2C.h>
#define I2C_ADDRESS 0x76
#define TEMP_PWR_PIN 22
BMx280I2C bmx280(I2C_ADDRESS);
char pressure[7];
char temperature[6];
char humidity[4];
char dewpoint[6];
unsigned int pres;
int temp;
int hum;
int dew;
byte tempInitError = 0;
byte tempReadError = 0;
byte tempStatus = 0;
byte tempFatal = 0;
//Disable the temp sensor and write zeros to APRS WX strings
void tempDisable() {
digitalWrite(TEMP_PWR_PIN, LOW);
pres = "b00000";
temp = "t000";
hum = "h00";
dew = "dp000";
tempStatus = "0";
tempFatal = 1;
}
//Reset routine in case of initialization failure
void tempInitReset() {
for (int e = 0; e <= 2; e++) {
digitalWrite(TEMP_PWR_PIN, LOW);
delay(300 * e);
digitalWrite(TEMP_PWR_PIN, HIGH);
delay(300 * e);
if (bmx280.begin()) {
tempSetup();
tempInitError = 0;
return;
}
else if (tempInitError < 2){
e++;
tempInitError++;
}
else if (tempInitError >= 2) {
tempDisable();
return;
}
}
}
//Retry and reset routine for sensor read failure
void tempReadReset() {
tempStatus = 0;
delay(500);
if (bmx280.measure()) {
tempRead();
if (tempStatus == 1) {
return;
}
}
else {
for (int x = 0; x <= 2; x++) {
if (tempReadError < 2) {
digitalWrite(TEMP_PWR_PIN, LOW);
delay(1000);
tempSetup();
delay(1000);
tempRead();
if (tempStatus == 1) {
tempReadError = 0;
return;
}
tempReadError++;
}
else {
tempDisable();
return;
}
}
}
}
//Setup routine, call in void setup()
void tempSetup() {
pinMode(TEMP_PWR_PIN, OUTPUT);
digitalWrite(TEMP_PWR_PIN, HIGH);
//begin() checks the Interface, reads the sensor ID (to differentiate between BMP280 and BME280)
//and reads compensation parameters.
delay(500);
if (!bmx280.begin()) {
Serial.println("TEMP SETUP FAILED");
tempInitReset();
}
//reset sensor to default parameters.
bmx280.resetToDefaults();
//set oversampling to enable pressure, temperature, and humidity measurements.
bmx280.writeOversamplingPressure(BMx280MI::OSRS_P_x16);
bmx280.writeOversamplingTemperature(BMx280MI::OSRS_T_x16);
bmx280.writeOversamplingHumidity(BMx280MI::OSRS_H_x16);
}
void tempRead() {
//start a measurement
if (!bmx280.measure()) {
tempReadReset();
}
else {
tempStatus = 1;
}
//wait for the measurement to finish
do {
delay(100);
} while (!bmx280.hasValue());
//poll sensors and format data for APRS
float p = bmx280.getPressure64();
float p1 = p / 10;
pres = (int)p1;
sprintf(pressure, "b%05d", pres);
//Serial.print("Pressure: ");
//Serial.println(pressure);
float t = bmx280.getTemperature();
float t1 = (t * 1.8) + 32;
temp = (int)t1;
sprintf(temperature, "t%03d", temp);
//Serial.print("Temp: ");
//Serial.println(temperature);
float h = bmx280.getHumidity();
hum = (int)h;
if (hum == 100) {
hum = 0;
}
sprintf(humidity, "h%02d", hum);
//Serial.print("Humidity: ");
//Serial.println(humidity);
// calculate dewpoint
float d = log(h/100) + (17.62 * t) / (243.12 + t);
float d1 = 243.12 * d / (17.62 - d);
d = (d1 * 1.8) + 32;
dew = (int)d;
sprintf(dewpoint, "dp%03d", dew);
//Serial.print("Dewpoint: ");
//Serial.println(dewpoint);
}
void setup() {
Serial.begin(9600);
Wire.begin();
tempSetup();
}
void loop() {
tempRead();
delay(2000);
}