-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMP180Device.cpp
126 lines (111 loc) · 4.06 KB
/
BMP180Device.cpp
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
#include "BMP180Device.h"
#include <Arduino.h>
#define LOGGING 1 // delete this line to turn logging off
BMP180Device::BMP180Device() :
baselinePressure(0.0),
altitudeMeters(0.0),
pressureMb(0.0),
temperatureC(0.0)
{
if (sensor.begin())
{
logmsg("BMP180 init success");
baselinePressure = getPressure();
logmsg("baseline pressure: " + String(baselinePressure, 3) + " mb.");
}
else
{
// Oops, something went wrong, this is usually a connection problem,
// see the comments at the top of this sketch for the proper connections.
logmsg("BMP180 init fail (disconnected?)\n\n");
while(1); // Pause forever.
}
}
BMP180Device::~BMP180Device()
{}
void BMP180Device::logmsg(String msg)
{
#ifdef LOGGING
Serial.println(classname + " " + msg);
#endif
}
double BMP180Device::getPressure()
{
char status;
double P,p0;
// You must first get a temperature measurement to perform a pressure reading.
// Start a temperature measurement:
// If request is successful, the number of ms to wait is returned.
// If request is unsuccessful, 0 is returned.
status = sensor.startTemperature();
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
// Retrieve the completed temperature measurement:
// Note that the measurement is stored in the variable T.
// Use '&T' to provide the address of T to the function.
// Function returns 1 if successful, 0 if failure.
status = sensor.getTemperature(temperatureC);
if (status != 0)
{
logmsg("temperature: " + String(temperatureC, 2) + " deg C, " + String((9.0/5.0)*temperatureC+32.0, 2) + " deg F");
// Start a pressure measurement:
// The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
// If request is successful, the number of ms to wait is returned.
// If request is unsuccessful, 0 is returned.
status = sensor.startPressure(3);
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
// Retrieve the completed pressure measurement:
// Note that the measurement is stored in the variable P.
// Use '&P' to provide the address of P.
// Note also that the function requires the previous temperature measurement (T).
// (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
// Function returns 1 if successful, 0 if failure.
status = sensor.getPressure(P,temperatureC);
if (status != 0)
{
// Print out the measurement:
logmsg("absolute pressure: " + String(P,2) + " mb, " + String(P*0.0295333727, 2) + " inHg");
return(P);
}
else logmsg("error retrieving pressure measurement\n");
}
else logmsg("error starting pressure measurement\n");
}
else logmsg("error retrieving temperature measurement\n");
}
else logmsg("error starting temperature measurement\n");
}
void BMP180Device::getData()
{
// Get a new pressure reading:
pressureMb = getPressure();
// Show the relative altitude difference between
// the new reading and the baseline reading:
double relativeAltitudeMeters = sensor.altitude(pressureMb,baselinePressure);
altitudeMeters = sensor.altitude(pressureMb, CONST_AVG_PRS_MBAR_AT_SEA_LEVEL);
logmsg("relative altitude: " + String(relativeAltitudeMeters, 1) + " meters, " + String(relativeAltitudeMeters*3.28084,0) + " feet.");
logmsg("absolute altitude: " + String(altitudeMeters, 1) + " meters, " + String(altitudeMeters*3.28084,0) + " feet.");
}
String BMP180Device::getDisplayablePressureMb()
{
String retval = String(pressureMb, 2);
//logmsg("returning prs " + retval);
return retval;
}
String BMP180Device::getDisplayableTemperatureF()
{
String retval = String((9.0/5.0)*temperatureC+32.0, 2);
//logmsg("returning tmp " + retval);
return retval;
}
String BMP180Device::getDisplayableAltitudeFt()
{
String retval = String(altitudeMeters*3.28084, 0);
//logmsg("returning alt " + retval);
return retval;
}