-
Notifications
You must be signed in to change notification settings - Fork 30
/
_P006_BMP085.ino
289 lines (244 loc) · 9.56 KB
/
_P006_BMP085.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//#######################################################################################################
//######################## Plugin 006 BMP0685 I2C Barometric Pressure Sensor ###########################
//#######################################################################################################
#define PLUGIN_006
#define PLUGIN_ID_006 6
#define PLUGIN_NAME_006 "Temperature & Pressure - BMP085"
#define PLUGIN_VALUENAME1_006 "Temperature"
#define PLUGIN_VALUENAME2_006 "Pressure"
boolean Plugin_006_init = false;
boolean Plugin_006(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_006;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_TEMP_BARO;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 2;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_006);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_006));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_006));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
string += F("<TR><TD>Altitude [m]:<TD><input type='text' title='Set Altitude to 0 to get measurement without altitude adjustment' name='");
string += F("_p006_bmp085_elev' value='");
string += Settings.TaskDevicePluginConfig[event->TaskIndex][1];
string += F("'>");
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String elev = WebServer.arg(F("_p006_bmp085_elev"));
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = elev.toInt();
success = true;
break;
}
case PLUGIN_READ:
{
if (!Plugin_006_init)
{
if (Plugin_006_bmp085_begin())
Plugin_006_init = true;
}
if (Plugin_006_init)
{
UserVar[event->BaseVarIndex] = Plugin_006_bmp085_readTemperature();
int elev = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
if (elev)
{
UserVar[event->BaseVarIndex + 1] = Plugin_006_pressureElevation((float)Plugin_006_bmp085_readPressure() / 100, elev);
} else {
UserVar[event->BaseVarIndex + 1] = ((float)Plugin_006_bmp085_readPressure()) / 100;
}
String log = F("BMP : Temperature: ");
log += UserVar[event->BaseVarIndex];
addLog(LOG_LEVEL_INFO, log);
log = F("BMP : Barometric Pressure: ");
log += UserVar[event->BaseVarIndex + 1];
addLog(LOG_LEVEL_INFO, log);
success = true;
}
break;
}
}
return success;
}
#define BMP085_I2CADDR 0x77
#define BMP085_ULTRAHIGHRES 3
#define BMP085_CAL_AC1 0xAA // R Calibration data (16 bits)
#define BMP085_CAL_AC2 0xAC // R Calibration data (16 bits)
#define BMP085_CAL_AC3 0xAE // R Calibration data (16 bits)
#define BMP085_CAL_AC4 0xB0 // R Calibration data (16 bits)
#define BMP085_CAL_AC5 0xB2 // R Calibration data (16 bits)
#define BMP085_CAL_AC6 0xB4 // R Calibration data (16 bits)
#define BMP085_CAL_B1 0xB6 // R Calibration data (16 bits)
#define BMP085_CAL_B2 0xB8 // R Calibration data (16 bits)
#define BMP085_CAL_MB 0xBA // R Calibration data (16 bits)
#define BMP085_CAL_MC 0xBC // R Calibration data (16 bits)
#define BMP085_CAL_MD 0xBE // R Calibration data (16 bits)
#define BMP085_CONTROL 0xF4
#define BMP085_TEMPDATA 0xF6
#define BMP085_PRESSUREDATA 0xF6
#define BMP085_READTEMPCMD 0x2E
#define BMP085_READPRESSURECMD 0x34
uint8_t oversampling = BMP085_ULTRAHIGHRES;
int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
uint16_t ac4, ac5, ac6;
/*********************************************************************/
boolean Plugin_006_bmp085_begin()
/*********************************************************************/
{
if (Plugin_006_bmp085_read8(0xD0) != 0x55) return false;
/* read calibration data */
ac1 = Plugin_006_bmp085_read16(BMP085_CAL_AC1);
ac2 = Plugin_006_bmp085_read16(BMP085_CAL_AC2);
ac3 = Plugin_006_bmp085_read16(BMP085_CAL_AC3);
ac4 = Plugin_006_bmp085_read16(BMP085_CAL_AC4);
ac5 = Plugin_006_bmp085_read16(BMP085_CAL_AC5);
ac6 = Plugin_006_bmp085_read16(BMP085_CAL_AC6);
b1 = Plugin_006_bmp085_read16(BMP085_CAL_B1);
b2 = Plugin_006_bmp085_read16(BMP085_CAL_B2);
mb = Plugin_006_bmp085_read16(BMP085_CAL_MB);
mc = Plugin_006_bmp085_read16(BMP085_CAL_MC);
md = Plugin_006_bmp085_read16(BMP085_CAL_MD);
}
/*********************************************************************/
uint16_t Plugin_006_bmp085_readRawTemperature(void)
/*********************************************************************/
{
Plugin_006_bmp085_write8(BMP085_CONTROL, BMP085_READTEMPCMD);
delay(5);
return Plugin_006_bmp085_read16(BMP085_TEMPDATA);
}
/*********************************************************************/
uint32_t Plugin_006_bmp085_readRawPressure(void)
/*********************************************************************/
{
uint32_t raw;
Plugin_006_bmp085_write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));
delay(26);
raw = Plugin_006_bmp085_read16(BMP085_PRESSUREDATA);
raw <<= 8;
raw |= Plugin_006_bmp085_read8(BMP085_PRESSUREDATA + 2);
raw >>= (8 - oversampling);
return raw;
}
/*********************************************************************/
int32_t Plugin_006_bmp085_readPressure(void)
/*********************************************************************/
{
int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
uint32_t B4, B7;
UT = Plugin_006_bmp085_readRawTemperature();
UP = Plugin_006_bmp085_readRawPressure();
// do temperature calculations
X1 = (UT - (int32_t)(ac6)) * ((int32_t)(ac5)) / pow(2, 15);
X2 = ((int32_t)mc * pow(2, 11)) / (X1 + (int32_t)md);
B5 = X1 + X2;
// do pressure calcs
B6 = B5 - 4000;
X1 = ((int32_t)b2 * ( (B6 * B6) >> 12 )) >> 11;
X2 = ((int32_t)ac2 * B6) >> 11;
X3 = X1 + X2;
B3 = ((((int32_t)ac1 * 4 + X3) << oversampling) + 2) / 4;
X1 = ((int32_t)ac3 * B6) >> 13;
X2 = ((int32_t)b1 * ((B6 * B6) >> 12)) >> 16;
X3 = ((X1 + X2) + 2) >> 2;
B4 = ((uint32_t)ac4 * (uint32_t)(X3 + 32768)) >> 15;
B7 = ((uint32_t)UP - B3) * (uint32_t)( 50000UL >> oversampling );
if (B7 < 0x80000000)
{
p = (B7 * 2) / B4;
}
else
{
p = (B7 / B4) * 2;
}
X1 = (p >> 8) * (p >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * p) >> 16;
p = p + ((X1 + X2 + (int32_t)3791) >> 4);
return p;
}
/*********************************************************************/
float Plugin_006_bmp085_readTemperature(void)
/*********************************************************************/
{
int32_t UT, X1, X2, B5; // following ds convention
float temp;
UT = Plugin_006_bmp085_readRawTemperature();
// step 1
X1 = (UT - (int32_t)ac6) * ((int32_t)ac5) / pow(2, 15);
X2 = ((int32_t)mc * pow(2, 11)) / (X1 + (int32_t)md);
B5 = X1 + X2;
temp = (B5 + 8) / pow(2, 4);
temp /= 10;
return temp;
}
/*********************************************************************/
uint8_t Plugin_006_bmp085_read8(uint8_t a)
/*********************************************************************/
{
uint8_t ret;
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
Wire.write(a); // sends register address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
Wire.requestFrom(BMP085_I2CADDR, 1);// send data n-bytes read
ret = Wire.read(); // receive DATA
Wire.endTransmission(); // end transmission
return ret;
}
/*********************************************************************/
uint16_t Plugin_006_bmp085_read16(uint8_t a)
/*********************************************************************/
{
uint16_t ret;
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
Wire.write(a); // sends register address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
Wire.requestFrom(BMP085_I2CADDR, 2);// send data n-bytes read
ret = Wire.read(); // receive DATA
ret <<= 8;
ret |= Wire.read(); // receive DATA
Wire.endTransmission(); // end transmission
return ret;
}
/*********************************************************************/
boolean Plugin_006_bmp085_write8(uint8_t a, uint8_t d)
/*********************************************************************/
{
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
Wire.write(a); // sends register address to read from
Wire.write(d); // write data
if(Wire.endTransmission() != 0)
return false;
return true;
}
/*********************************************************************/
float Plugin_006_pressureElevation(float atmospheric, int altitude) {
/*********************************************************************/
return atmospheric / pow(1.0 - (altitude/44330.0), 5.255);
}