diff --git a/Esp8266EasyIoT/examples/esp_pressure/esp_pressure.ino b/Esp8266EasyIoT/examples/esp_pressure/esp_pressure.ino new file mode 100644 index 0000000..b6fcdf9 --- /dev/null +++ b/Esp8266EasyIoT/examples/esp_pressure/esp_pressure.ino @@ -0,0 +1,342 @@ +#include +#include +#include + + +#define ALTITUDE 301.0 // Altitude of my home +#define ESP_RESET_PIN 12 + +#define MILS_IN_MIN 1000 + +#define CHILD_ID_TEMP 0 +#define CHILD_ID_BARO 1 + + +int minuteCount = 0; +double pressureSamples[9][6]; +double pressureAvg[9]; +double dP_dt; + +const char *weather[] = { + "stable","sunny","cloudy","unstable","thunderstorm","unknown"}; + +int forecast = 5; + +unsigned long startTime; + +SFE_BMP180 bmp180; +Esp8266EasyIoT esp; + + +Esp8266EasyIoTMsg msgTemp(CHILD_ID_TEMP, V_TEMP); +Esp8266EasyIoTMsg msgPress(CHILD_ID_BARO, V_PRESSURE); +Esp8266EasyIoTMsg msgForec(CHILD_ID_BARO, V_FORECAST); + +void setup() +{ + Serial1.begin(9600); // ESP + Serial.begin(115200); // debug + + if (bmp180.begin()) + Serial.println("BMP180 init success"); + else + { + // Oops, something went wrong, this is usually a connection problem, + // see the comments at the top of this sketch for the proper connections. + + Serial.println("BMP180 init fail\n\n"); + while(1); // Pause forever. + } + + startTime = -1; + + esp.begin(NULL, ESP_RESET_PIN, &Serial1, &Serial); + + esp.present(CHILD_ID_TEMP, S_TEMP); + esp.present(CHILD_ID_BARO, S_BARO); +} + + +void loop() +{ + + for(int i =0; i<10;i++) + { + if (esp.process()) + break; + } + + + if (IsTimeout()) + { + char status; + double T,P,p0,a; + + // Loop here getting pressure readings every 60 seconds. + + // If you want sea-level-compensated pressure, as used in weather reports, + // you will need to know the altitude at which your measurements are taken. + // We're using a constant called ALTITUDE in this sketch: + + // If you want to measure altitude, and not pressure, you will instead need + // to provide a known baseline pressure. This is shown at the end of the sketch. + + // 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 = bmp180.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. + // Function returns 1 if successful, 0 if failure. + + status = bmp180.getTemperature(T); + if (status != 0) + { + // Print out the measurement: + Serial.print("temperature: "); + Serial.print(T,2); + Serial.print(" deg C, "); + Serial.print((9.0/5.0)*T+32.0,2); + Serial.println(" deg F"); + + + static int lastSendTempInt; + int temp = round(T *10); + + if (temp != lastSendTempInt) + { + lastSendTempInt = temp; + esp.send(msgTemp.set((float)T, 1)); + } + + // 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 = bmp180.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. + // 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 = bmp180.getPressure(P,T); + if (status != 0) + { + // The pressure sensor returns abolute pressure, which varies with altitude. + // To remove the effects of altitude, use the sealevel function and your current altitude. + // This number is commonly used in weather reports. + // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m. + // Result: p0 = sea-level compensated pressure in mb + + p0 = bmp180.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO) + Serial.print("relative (sea-level) pressure: "); + Serial.print(p0,2); + Serial.print(" mb, "); + Serial.print(p0*0.0295333727,2); + Serial.println(" inHg"); + + + static int lastSendPresInt; + int pres = round(p0 *10); + + if (pres != lastSendPresInt) + { + lastSendPresInt = pres; + esp.send(msgPress.set((float)p0, 1)); + } + + forecast = calculateForecast(p0); + static int lastSendForeInt = -1; + + + if (forecast != lastSendForeInt) + { + lastSendForeInt = forecast; + esp.send(msgForec.set(weather[forecast])); + } + } + else Serial.println("error retrieving pressure measurement\n"); + } + else Serial.println("error starting pressure measurement\n"); + } + else Serial.println("error retrieving temperature measurement\n"); + } + else Serial.println("error starting temperature measurement\n"); + + startTime = millis(); +} + + //delay(5000); // Pause for 5 seconds. +} + +boolean IsTimeout() +{ + unsigned long now = millis(); + if (startTime <= now) + { + if ( (unsigned long)(now - startTime ) < MILS_IN_MIN ) + return false; + } + else + { + if ( (unsigned long)(startTime - now) < MILS_IN_MIN ) + return false; + } + + return true; +} + + +int calculateForecast(double pressure) { + //From 0 to 5 min. + if (minuteCount <= 5){ + pressureSamples[0][minuteCount] = pressure; + } + //From 30 to 35 min. + else if ((minuteCount >= 30) && (minuteCount <= 35)){ + pressureSamples[1][minuteCount - 30] = pressure; + } + //From 60 to 65 min. + else if ((minuteCount >= 60) && (minuteCount <= 65)){ + pressureSamples[2][minuteCount - 60] = pressure; + } + //From 90 to 95 min. + else if ((minuteCount >= 90) && (minuteCount <= 95)){ + pressureSamples[3][minuteCount - 90] = pressure; + } + //From 120 to 125 min. + else if ((minuteCount >= 120) && (minuteCount <= 125)){ + pressureSamples[4][minuteCount - 120] = pressure; + } + //From 150 to 155 min. + else if ((minuteCount >= 150) && (minuteCount <= 155)){ + pressureSamples[5][minuteCount - 150] = pressure; + } + //From 180 to 185 min. + else if ((minuteCount >= 180) && (minuteCount <= 185)){ + pressureSamples[6][minuteCount - 180] = pressure; + } + //From 210 to 215 min. + else if ((minuteCount >= 210) && (minuteCount <= 215)){ + pressureSamples[7][minuteCount - 210] = pressure; + } + //From 240 to 245 min. + else if ((minuteCount >= 240) && (minuteCount <= 245)){ + pressureSamples[8][minuteCount - 240] = pressure; + } + + + if (minuteCount == 5) { + // Avg pressure in first 5 min, value averaged from 0 to 5 min. + pressureAvg[0] = ((pressureSamples[0][0] + pressureSamples[0][1] + + pressureSamples[0][2] + pressureSamples[0][3] + + pressureSamples[0][4] + pressureSamples[0][5]) / 6); + } + else if (minuteCount == 35) { + // Avg pressure in 30 min, value averaged from 0 to 5 min. + pressureAvg[1] = ((pressureSamples[1][0] + pressureSamples[1][1] + + pressureSamples[1][2] + pressureSamples[1][3] + + pressureSamples[1][4] + pressureSamples[1][5]) / 6); + float change = (pressureAvg[1] - pressureAvg[0]); + dP_dt = change / 5; + } + else if (minuteCount == 65) { + // Avg pressure at end of the hour, value averaged from 0 to 5 min. + pressureAvg[2] = ((pressureSamples[2][0] + pressureSamples[2][1] + + pressureSamples[2][2] + pressureSamples[2][3] + + pressureSamples[2][4] + pressureSamples[2][5]) / 6); + float change = (pressureAvg[2] - pressureAvg[0]); + dP_dt = change / 10; + } + else if (minuteCount == 95) { + // Avg pressure at end of the hour, value averaged from 0 to 5 min. + pressureAvg[3] = ((pressureSamples[3][0] + pressureSamples[3][1] + + pressureSamples[3][2] + pressureSamples[3][3] + + pressureSamples[3][4] + pressureSamples[3][5]) / 6); + float change = (pressureAvg[3] - pressureAvg[0]); + dP_dt = change / 15; + } + else if (minuteCount == 125) { + // Avg pressure at end of the hour, value averaged from 0 to 5 min. + pressureAvg[4] = ((pressureSamples[4][0] + pressureSamples[4][1] + + pressureSamples[4][2] + pressureSamples[4][3] + + pressureSamples[4][4] + pressureSamples[4][5]) / 6); + float change = (pressureAvg[4] - pressureAvg[0]); + dP_dt = change / 20; + } + else if (minuteCount == 155) { + // Avg pressure at end of the hour, value averaged from 0 to 5 min. + pressureAvg[5] = ((pressureSamples[5][0] + pressureSamples[5][1] + + pressureSamples[5][2] + pressureSamples[5][3] + + pressureSamples[5][4] + pressureSamples[5][5]) / 6); + float change = (pressureAvg[5] - pressureAvg[0]); + dP_dt = change / 25; + } + else if (minuteCount == 185) { + // Avg pressure at end of the hour, value averaged from 0 to 5 min. + pressureAvg[6] = ((pressureSamples[6][0] + pressureSamples[6][1] + + pressureSamples[6][2] + pressureSamples[6][3] + + pressureSamples[6][4] + pressureSamples[6][5]) / 6); + float change = (pressureAvg[6] - pressureAvg[0]); + dP_dt = change / 30; + } + else if (minuteCount == 215) { + // Avg pressure at end of the hour, value averaged from 0 to 5 min. + pressureAvg[7] = ((pressureSamples[7][0] + pressureSamples[7][1] + + pressureSamples[7][2] + pressureSamples[7][3] + + pressureSamples[7][4] + pressureSamples[7][5]) / 6); + float change = (pressureAvg[7] - pressureAvg[0]); + dP_dt = change / 35; + } + else if (minuteCount == 245) { + // Avg pressure at end of the hour, value averaged from 0 to 5 min. + pressureAvg[8] = ((pressureSamples[8][0] + pressureSamples[8][1] + + pressureSamples[8][2] + pressureSamples[8][3] + + pressureSamples[8][4] + pressureSamples[8][5]) / 6); + float change = (pressureAvg[8] - pressureAvg[0]); + dP_dt = change / 40; // note this is for t = 4 hour + + minuteCount -= 30; + pressureAvg[0] = pressureAvg[1]; + pressureAvg[1] = pressureAvg[2]; + pressureAvg[2] = pressureAvg[3]; + pressureAvg[3] = pressureAvg[4]; + pressureAvg[4] = pressureAvg[5]; + pressureAvg[5] = pressureAvg[6]; + pressureAvg[6] = pressureAvg[7]; + pressureAvg[7] = pressureAvg[8]; + } + + minuteCount++; + + if (minuteCount < 36) //if time is less than 35 min + return 5; // Unknown, more time needed + else if (dP_dt < (-0.25)) + return 4; // Quickly falling LP, Thunderstorm, not stable + else if (dP_dt > 0.25) + return 3; // Quickly rising HP, not stable weather + else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05))) + return 2; // Slowly falling Low Pressure System, stable rainy weather + else if ((dP_dt > 0.05) && (dP_dt < 0.25)) + return 1; // Slowly rising HP stable good weather + else if ((dP_dt > (-0.05)) && (dP_dt < 0.05)) + return 0; // Stable weather + else + return 5; // Unknown +} + diff --git a/external_libraries/SFE_BMP180/SFE_BMP180.cpp b/external_libraries/SFE_BMP180/SFE_BMP180.cpp new file mode 100644 index 0000000..4383fc5 --- /dev/null +++ b/external_libraries/SFE_BMP180/SFE_BMP180.cpp @@ -0,0 +1,387 @@ +/* + SFE_BMP180.cpp + Bosch BMP180 pressure sensor library for the Arduino microcontroller + Mike Grusin, SparkFun Electronics + + Uses floating-point equations from the Weather Station Data Logger project + http://wmrx00.sourceforge.net/ + http://wmrx00.sourceforge.net/Arduino/BMP085-Calcs.pdf + + Forked from BMP085 library by M.Grusin + + version 1.0 2013/09/20 initial version + + Our example code uses the "beerware" license. You can do anything + you like with this code. No really, anything. If you find it useful, + buy me a (root) beer someday. +*/ + +#include +#include +#include +#include + + +SFE_BMP180::SFE_BMP180() +// Base library type +{ +} + + +char SFE_BMP180::begin() +// Initialize library for subsequent pressure measurements +{ + double c3,c4,b1; + + // Start up the Arduino's "wire" (I2C) library: + + Wire.begin(); + + // The BMP180 includes factory calibration data stored on the device. + // Each device has different numbers, these must be retrieved and + // used in the calculations when taking pressure measurements. + + // Retrieve calibration data from device: + + if (readInt(0xAA,AC1) && + readInt(0xAC,AC2) && + readInt(0xAE,AC3) && + readUInt(0xB0,AC4) && + readUInt(0xB2,AC5) && + readUInt(0xB4,AC6) && + readInt(0xB6,VB1) && + readInt(0xB8,VB2) && + readInt(0xBA,MB) && + readInt(0xBC,MC) && + readInt(0xBE,MD)) + { + + // All reads completed successfully! + + // If you need to check your math using known numbers, + // you can uncomment one of these examples. + // (The correct results are commented in the below functions.) + + // Example from Bosch datasheet + // AC1 = 408; AC2 = -72; AC3 = -14383; AC4 = 32741; AC5 = 32757; AC6 = 23153; + // B1 = 6190; B2 = 4; MB = -32768; MC = -8711; MD = 2868; + + // Example from http://wmrx00.sourceforge.net/Arduino/BMP180-Calcs.pdf + // AC1 = 7911; AC2 = -934; AC3 = -14306; AC4 = 31567; AC5 = 25671; AC6 = 18974; + // VB1 = 5498; VB2 = 46; MB = -32768; MC = -11075; MD = 2432; + + /* + Serial.print("AC1: "); Serial.println(AC1); + Serial.print("AC2: "); Serial.println(AC2); + Serial.print("AC3: "); Serial.println(AC3); + Serial.print("AC4: "); Serial.println(AC4); + Serial.print("AC5: "); Serial.println(AC5); + Serial.print("AC6: "); Serial.println(AC6); + Serial.print("VB1: "); Serial.println(VB1); + Serial.print("VB2: "); Serial.println(VB2); + Serial.print("MB: "); Serial.println(MB); + Serial.print("MC: "); Serial.println(MC); + Serial.print("MD: "); Serial.println(MD); + */ + + // Compute floating-point polynominals: + + c3 = 160.0 * pow(2,-15) * AC3; + c4 = pow(10,-3) * pow(2,-15) * AC4; + b1 = pow(160,2) * pow(2,-30) * VB1; + c5 = (pow(2,-15) / 160) * AC5; + c6 = AC6; + mc = (pow(2,11) / pow(160,2)) * MC; + md = MD / 160.0; + x0 = AC1; + x1 = 160.0 * pow(2,-13) * AC2; + x2 = pow(160,2) * pow(2,-25) * VB2; + y0 = c4 * pow(2,15); + y1 = c4 * c3; + y2 = c4 * b1; + p0 = (3791.0 - 8.0) / 1600.0; + p1 = 1.0 - 7357.0 * pow(2,-20); + p2 = 3038.0 * 100.0 * pow(2,-36); + + /* + Serial.println(); + Serial.print("c3: "); Serial.println(c3); + Serial.print("c4: "); Serial.println(c4); + Serial.print("c5: "); Serial.println(c5); + Serial.print("c6: "); Serial.println(c6); + Serial.print("b1: "); Serial.println(b1); + Serial.print("mc: "); Serial.println(mc); + Serial.print("md: "); Serial.println(md); + Serial.print("x0: "); Serial.println(x0); + Serial.print("x1: "); Serial.println(x1); + Serial.print("x2: "); Serial.println(x2); + Serial.print("y0: "); Serial.println(y0); + Serial.print("y1: "); Serial.println(y1); + Serial.print("y2: "); Serial.println(y2); + Serial.print("p0: "); Serial.println(p0); + Serial.print("p1: "); Serial.println(p1); + Serial.print("p2: "); Serial.println(p2); + */ + + // Success! + return(1); + } + else + { + // Error reading calibration data; bad component or connection? + return(0); + } +} + + +char SFE_BMP180::readInt(char address, int &value) +// Read a signed integer (two bytes) from device +// address: register to start reading (plus subsequent register) +// value: external variable to store data (function modifies value) +{ + unsigned char data[2]; + + data[0] = address; + if (readBytes(data,2)) + { + value = (((int)data[0]<<8)|(int)data[1]); + //if (*value & 0x8000) *value |= 0xFFFF0000; // sign extend if negative + return(1); + } + value = 0; + return(0); +} + + +char SFE_BMP180::readUInt(char address, unsigned int &value) +// Read an unsigned integer (two bytes) from device +// address: register to start reading (plus subsequent register) +// value: external variable to store data (function modifies value) +{ + unsigned char data[2]; + + data[0] = address; + if (readBytes(data,2)) + { + value = (((unsigned int)data[0]<<8)|(unsigned int)data[1]); + return(1); + } + value = 0; + return(0); +} + + +char SFE_BMP180::readBytes(unsigned char *values, char length) +// Read an array of bytes from device +// values: external array to hold data. Put starting register in values[0]. +// length: number of bytes to read +{ + char x; + + Wire.beginTransmission(BMP180_ADDR); + Wire.write(values[0]); + _error = Wire.endTransmission(); + if (_error == 0) + { + Wire.requestFrom(BMP180_ADDR,length); + while(Wire.available() != length) ; // wait until bytes are ready + for(x=0;x= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +class SFE_BMP180 +{ + public: + SFE_BMP180(); // base type + + char begin(); + // call pressure.begin() to initialize BMP180 before use + // returns 1 if success, 0 if failure (bad component or I2C bus shorted?) + + char startTemperature(void); + // command BMP180 to start a temperature measurement + // returns (number of ms to wait) for success, 0 for fail + + char getTemperature(double &T); + // return temperature measurement from previous startTemperature command + // places returned value in T variable (deg C) + // returns 1 for success, 0 for fail + + char startPressure(char oversampling); + // command BMP180 to start a pressure measurement + // oversampling: 0 - 3 for oversampling value + // returns (number of ms to wait) for success, 0 for fail + + char getPressure(double &P, double &T); + // return absolute pressure measurement from previous startPressure command + // note: requires previous temperature measurement in variable T + // places returned value in P variable (mbar) + // returns 1 for success, 0 for fail + + double sealevel(double P, double A); + // convert absolute pressure to sea-level pressure (as used in weather data) + // P: absolute pressure (mbar) + // A: current altitude (meters) + // returns sealevel pressure in mbar + + double altitude(double P, double P0); + // convert absolute pressure to altitude (given baseline pressure; sea-level, runway, etc.) + // P: absolute pressure (mbar) + // P0: fixed baseline pressure (mbar) + // returns signed altitude in meters + + char getError(void); + // If any library command fails, you can retrieve an extended + // error code using this command. Errors are from the wire library: + // 0 = Success + // 1 = Data too long to fit in transmit buffer + // 2 = Received NACK on transmit of address + // 3 = Received NACK on transmit of data + // 4 = Other error + + private: + + char readInt(char address, int &value); + // read an signed int (16 bits) from a BMP180 register + // address: BMP180 register address + // value: external signed int for returned value (16 bits) + // returns 1 for success, 0 for fail, with result in value + + char readUInt(char address, unsigned int &value); + // read an unsigned int (16 bits) from a BMP180 register + // address: BMP180 register address + // value: external unsigned int for returned value (16 bits) + // returns 1 for success, 0 for fail, with result in value + + char readBytes(unsigned char *values, char length); + // read a number of bytes from a BMP180 register + // values: array of char with register address in first location [0] + // length: number of bytes to read back + // returns 1 for success, 0 for fail, with read bytes in values[] array + + char writeBytes(unsigned char *values, char length); + // write a number of bytes to a BMP180 register (and consecutive subsequent registers) + // values: array of char with register address in first location [0] + // length: number of bytes to write + // returns 1 for success, 0 for fail + + int AC1,AC2,AC3,VB1,VB2,MB,MC,MD; + unsigned int AC4,AC5,AC6; + double c5,c6,mc,md,x0,x1,x2,y0,y1,y2,p0,p1,p2; + char _error; +}; + +#define BMP180_ADDR 0x77 // 7-bit address + +#define BMP180_REG_CONTROL 0xF4 +#define BMP180_REG_RESULT 0xF6 + +#define BMP180_COMMAND_TEMPERATURE 0x2E +#define BMP180_COMMAND_PRESSURE0 0x34 +#define BMP180_COMMAND_PRESSURE1 0x74 +#define BMP180_COMMAND_PRESSURE2 0xB4 +#define BMP180_COMMAND_PRESSURE3 0xF4 + +#endif diff --git a/external_libraries/SFE_BMP180/examples/BMP180_altitude_example/BMP180_altitude_example.ino b/external_libraries/SFE_BMP180/examples/BMP180_altitude_example/BMP180_altitude_example.ino new file mode 100644 index 0000000..fdb8358 --- /dev/null +++ b/external_libraries/SFE_BMP180/examples/BMP180_altitude_example/BMP180_altitude_example.ino @@ -0,0 +1,168 @@ +/* SFE_BMP180 altitude example sketch + +This sketch shows how to use the Bosch BMP180 pressure sensor +as an altimiter. +https://www.sparkfun.com/products/11824 + +Like most pressure sensors, the BMP180 measures absolute pressure. +Since absolute pressure varies with altitude, you can use the pressure +to determine your altitude. + +Because pressure also varies with weather, you must first take a pressure +reading at a known baseline altitude. Then you can measure variations +from that pressure + +Hardware connections: + +- (GND) to GND ++ (VDD) to 3.3V + +(WARNING: do not connect + to 5V or the sensor will be damaged!) + +You will also need to connect the I2C pins (SCL and SDA) to your +Arduino. The pins are different on different Arduinos: + +Any Arduino pins labeled: SDA SCL +Uno, Redboard, Pro: A4 A5 +Mega2560, Due: 20 21 +Leonardo: 2 3 + +Leave the IO (VDDIO) pin unconnected. This pin is for connecting +the BMP180 to systems with lower logic levels such as 1.8V + +Have fun! -Your friends at SparkFun. + +The SFE_BMP180 library uses floating-point equations developed by the +Weather Station Data Logger project: http://wmrx00.sourceforge.net/ + +Our example code uses the "beerware" license. You can do anything +you like with this code. No really, anything. If you find it useful, +buy me a beer someday. + +V10 Mike Grusin, SparkFun Electronics 10/24/2013 +*/ + +// Your sketch must #include this library, and the Wire library. +// (Wire is a standard library included with Arduino.): + +#include +#include + +// You will need to create an SFE_BMP180 object, here called "pressure": + +SFE_BMP180 pressure; + +double baseline; // baseline pressure + +void setup() +{ + Serial.begin(9600); + Serial.println("REBOOT"); + + // Initialize the sensor (it is important to get calibration values stored on the device). + + if (pressure.begin()) + Serial.println("BMP180 init success"); + else + { + // Oops, something went wrong, this is usually a connection problem, + // see the comments at the top of this sketch for the proper connections. + + Serial.println("BMP180 init fail (disconnected?)\n\n"); + while(1); // Pause forever. + } + + // Get the baseline pressure: + + baseline = getPressure(); + + Serial.print("baseline pressure: "); + Serial.print(baseline); + Serial.println(" mb"); +} + +void loop() +{ + double a,P; + + // Get a new pressure reading: + + P = getPressure(); + + // Show the relative altitude difference between + // the new reading and the baseline reading: + + a = pressure.altitude(P,baseline); + + Serial.print("relative altitude: "); + if (a >= 0.0) Serial.print(" "); // add a space for positive numbers + Serial.print(a,1); + Serial.print(" meters, "); + if (a >= 0.0) Serial.print(" "); // add a space for positive numbers + Serial.print(a*3.28084,0); + Serial.println(" feet"); + + delay(500); +} + + +double getPressure() +{ + char status; + double T,P,p0,a; + + // 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 = pressure.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 = pressure.getTemperature(T); + if (status != 0) + { + // 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 = pressure.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 = pressure.getPressure(P,T); + if (status != 0) + { + return(P); + } + else Serial.println("error retrieving pressure measurement\n"); + } + else Serial.println("error starting pressure measurement\n"); + } + else Serial.println("error retrieving temperature measurement\n"); + } + else Serial.println("error starting temperature measurement\n"); +} + + + diff --git a/external_libraries/SFE_BMP180/examples/SFE_BMP180_example/SFE_BMP180_example.ino b/external_libraries/SFE_BMP180/examples/SFE_BMP180_example/SFE_BMP180_example.ino new file mode 100644 index 0000000..a43c905 --- /dev/null +++ b/external_libraries/SFE_BMP180/examples/SFE_BMP180_example/SFE_BMP180_example.ino @@ -0,0 +1,200 @@ +/* SFE_BMP180 library example sketch + +This sketch shows how to use the SFE_BMP180 library to read the +Bosch BMP180 barometric pressure sensor. +https://www.sparkfun.com/products/11824 + +Like most pressure sensors, the BMP180 measures absolute pressure. +This is the actual ambient pressure seen by the device, which will +vary with both altitude and weather. + +Before taking a pressure reading you must take a temparture reading. +This is done with startTemperature() and getTemperature(). +The result is in degrees C. + +Once you have a temperature reading, you can take a pressure reading. +This is done with startPressure() and getPressure(). +The result is in millibar (mb) aka hectopascals (hPa). + +If you'll be monitoring weather patterns, you will probably want to +remove the effects of altitude. This will produce readings that can +be compared to the published pressure readings from other locations. +To do this, use the sealevel() function. You will need to provide +the known altitude at which the pressure was measured. + +If you want to measure altitude, you will need to know the pressure +at a baseline altitude. This can be average sealevel pressure, or +a previous pressure reading at your altitude, in which case +subsequent altitude readings will be + or - the initial baseline. +This is done with the altitude() function. + +Hardware connections: + +- (GND) to GND ++ (VDD) to 3.3V + +(WARNING: do not connect + to 5V or the sensor will be damaged!) + +You will also need to connect the I2C pins (SCL and SDA) to your +Arduino. The pins are different on different Arduinos: + +Any Arduino pins labeled: SDA SCL +Uno, Redboard, Pro: A4 A5 +Mega2560, Due: 20 21 +Leonardo: 2 3 + +Leave the IO (VDDIO) pin unconnected. This pin is for connecting +the BMP180 to systems with lower logic levels such as 1.8V + +Have fun! -Your friends at SparkFun. + +The SFE_BMP180 library uses floating-point equations developed by the +Weather Station Data Logger project: http://wmrx00.sourceforge.net/ + +Our example code uses the "beerware" license. You can do anything +you like with this code. No really, anything. If you find it useful, +buy me a beer someday. + +V10 Mike Grusin, SparkFun Electronics 10/24/2013 +*/ + +// Your sketch must #include this library, and the Wire library. +// (Wire is a standard library included with Arduino.): + +#include +#include + +// You will need to create an SFE_BMP180 object, here called "pressure": + +SFE_BMP180 pressure; + +#define ALTITUDE 1655.0 // Altitude of SparkFun's HQ in Boulder, CO. in meters + +void setup() +{ + Serial.begin(9600); + Serial.println("REBOOT"); + + // Initialize the sensor (it is important to get calibration values stored on the device). + + if (pressure.begin()) + Serial.println("BMP180 init success"); + else + { + // Oops, something went wrong, this is usually a connection problem, + // see the comments at the top of this sketch for the proper connections. + + Serial.println("BMP180 init fail\n\n"); + while(1); // Pause forever. + } +} + +void loop() +{ + char status; + double T,P,p0,a; + + // Loop here getting pressure readings every 10 seconds. + + // If you want sea-level-compensated pressure, as used in weather reports, + // you will need to know the altitude at which your measurements are taken. + // We're using a constant called ALTITUDE in this sketch: + + Serial.println(); + Serial.print("provided altitude: "); + Serial.print(ALTITUDE,0); + Serial.print(" meters, "); + Serial.print(ALTITUDE*3.28084,0); + Serial.println(" feet"); + + // If you want to measure altitude, and not pressure, you will instead need + // to provide a known baseline pressure. This is shown at the end of the sketch. + + // 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 = pressure.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. + // Function returns 1 if successful, 0 if failure. + + status = pressure.getTemperature(T); + if (status != 0) + { + // Print out the measurement: + Serial.print("temperature: "); + Serial.print(T,2); + Serial.print(" deg C, "); + Serial.print((9.0/5.0)*T+32.0,2); + Serial.println(" 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 = pressure.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. + // 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 = pressure.getPressure(P,T); + if (status != 0) + { + // Print out the measurement: + Serial.print("absolute pressure: "); + Serial.print(P,2); + Serial.print(" mb, "); + Serial.print(P*0.0295333727,2); + Serial.println(" inHg"); + + // The pressure sensor returns abolute pressure, which varies with altitude. + // To remove the effects of altitude, use the sealevel function and your current altitude. + // This number is commonly used in weather reports. + // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m. + // Result: p0 = sea-level compensated pressure in mb + + p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO) + Serial.print("relative (sea-level) pressure: "); + Serial.print(p0,2); + Serial.print(" mb, "); + Serial.print(p0*0.0295333727,2); + Serial.println(" inHg"); + + // On the other hand, if you want to determine your altitude from the pressure reading, + // use the altitude function along with a baseline pressure (sea-level or other). + // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb. + // Result: a = altitude in m. + + a = pressure.altitude(P,p0); + Serial.print("computed altitude: "); + Serial.print(a,0); + Serial.print(" meters, "); + Serial.print(a*3.28084,0); + Serial.println(" feet"); + } + else Serial.println("error retrieving pressure measurement\n"); + } + else Serial.println("error starting pressure measurement\n"); + } + else Serial.println("error retrieving temperature measurement\n"); + } + else Serial.println("error starting temperature measurement\n"); + + delay(5000); // Pause for 5 seconds. +} diff --git a/external_libraries/SFE_BMP180/keywords.txt b/external_libraries/SFE_BMP180/keywords.txt new file mode 100644 index 0000000..664167d --- /dev/null +++ b/external_libraries/SFE_BMP180/keywords.txt @@ -0,0 +1,27 @@ +####################################### +# Syntax Coloring Map for SFE_BMP180 +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +SFE_BMP180 KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +startTemperature KEYWORD2 +getTemperature KEYWORD2 +startPressure KEYWORD2 +getPressure KEYWORD2 +sealevel KEYWORD2 +altitude KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + +BMP180_ADDR LITERAL1 \ No newline at end of file