How to import the libraries needed by BMP085?

I tried to import the libraries that are needed to use Bosch BMP085 breakout board but failed rather miserably. Copy pasting them in the beginning of the code on the online IDE did not work.

How should I modify the libraries to make them work or is this something that requires extensive rewriting?

@Sauli
This may not be a trivial under taking as the the library also relies upon the Adafruit_Sensor library. You may have to include all the dependencies in the user application file before you could get it to compile. Did you include all of these?

Hi @Sauli, I have used this library successfully with Arduino and BMP085. It may have fewer dependencies and so be easier to adapt for SparkCore.

Paul

1 Like

@Sauli just watching a movie here and decided to whip this up while it’s getting started. It’s just the library you linked mashed together with the example into one file. Read comments above setup() for pinout/hookups. Let us know if it works or needs tweaks! :smile:

Download here: http://pastebin.com/raw.php?i=7Xd9QsbB

/*************************************************** 
  This is a library for the Adafruit BMP085/BMP180 Barometric Pressure + Temp sensor

  Designed specifically to work with the Adafruit BMP085 or BMP180 Breakout 
  ----> http://www.adafruit.com/products/391
  ----> http://www.adafruit.com/products/1603

  These displays use I2C to communicate, 2 pins are required to  
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/
#include <math.h>

#define boolean bool 
  
#define BMP085_DEBUG 0

#define BMP085_I2CADDR 0x77

#define BMP085_ULTRALOWPOWER 0
#define BMP085_STANDARD      1
#define BMP085_HIGHRES       2
#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


class Adafruit_BMP085 {
 public:
  Adafruit_BMP085();
  boolean begin(uint8_t mode = BMP085_ULTRAHIGHRES);  // by default go highres
  float readTemperature(void);
  int32_t readPressure(void);
  float readAltitude(float sealevelPressure = 101325); // std atmosphere
  uint16_t readRawTemperature(void);
  uint32_t readRawPressure(void);
  
 private:
  uint8_t read8(uint8_t addr);
  uint16_t read16(uint8_t addr);
  void write8(uint8_t addr, uint8_t data);

  uint8_t oversampling;

  int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
  uint16_t ac4, ac5, ac6;
};


/*************************************************** 
  This is a library for the Adafruit BMP085/BMP180 Barometric Pressure + Temp sensor

  Designed specifically to work with the Adafruit BMP085 or BMP180 Breakout 
  ----> http://www.adafruit.com/products/391
  ----> http://www.adafruit.com/products/1603

  These displays use I2C to communicate, 2 pins are required to  
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

Adafruit_BMP085::Adafruit_BMP085() {
}


boolean Adafruit_BMP085::begin(uint8_t mode) {
  if (mode > BMP085_ULTRAHIGHRES) 
    mode = BMP085_ULTRAHIGHRES;
  oversampling = mode;

  Wire.begin();

  if (read8(0xD0) != 0x55) return false;

  /* read calibration data */
  ac1 = read16(BMP085_CAL_AC1);
  ac2 = read16(BMP085_CAL_AC2);
  ac3 = read16(BMP085_CAL_AC3);
  ac4 = read16(BMP085_CAL_AC4);
  ac5 = read16(BMP085_CAL_AC5);
  ac6 = read16(BMP085_CAL_AC6);

  b1 = read16(BMP085_CAL_B1);
  b2 = read16(BMP085_CAL_B2);

  mb = read16(BMP085_CAL_MB);
  mc = read16(BMP085_CAL_MC);
  md = read16(BMP085_CAL_MD);
#if (BMP085_DEBUG == 1)
  Serial.print("ac1 = "); Serial.println(ac1, DEC);
  Serial.print("ac2 = "); Serial.println(ac2, DEC);
  Serial.print("ac3 = "); Serial.println(ac3, DEC);
  Serial.print("ac4 = "); Serial.println(ac4, DEC);
  Serial.print("ac5 = "); Serial.println(ac5, DEC);
  Serial.print("ac6 = "); Serial.println(ac6, DEC);

  Serial.print("b1 = "); Serial.println(b1, DEC);
  Serial.print("b2 = "); Serial.println(b2, DEC);

  Serial.print("mb = "); Serial.println(mb, DEC);
  Serial.print("mc = "); Serial.println(mc, DEC);
  Serial.print("md = "); Serial.println(md, DEC);
#endif
  return true;
}

uint16_t Adafruit_BMP085::readRawTemperature(void) {
  write8(BMP085_CONTROL, BMP085_READTEMPCMD);
  delay(5);
#if BMP085_DEBUG == 1
  Serial.print("Raw temp: "); Serial.println(read16(BMP085_TEMPDATA));
#endif
  return read16(BMP085_TEMPDATA);
}

uint32_t Adafruit_BMP085::readRawPressure(void) {
  uint32_t raw;

  write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));

  if (oversampling == BMP085_ULTRALOWPOWER) 
    delay(5);
  else if (oversampling == BMP085_STANDARD) 
    delay(8);
  else if (oversampling == BMP085_HIGHRES) 
    delay(14);
  else 
    delay(26);

  raw = read16(BMP085_PRESSUREDATA);

  raw <<= 8;
  raw |= read8(BMP085_PRESSUREDATA+2);
  raw >>= (8 - oversampling);

 /* this pull broke stuff, look at it later?
  if (oversampling==0) {
    raw <<= 8;
    raw |= read8(BMP085_PRESSUREDATA+2);
    raw >>= (8 - oversampling);
  }
 */

#if BMP085_DEBUG == 1
  Serial.print("Raw pressure: "); Serial.println(raw);
#endif
  return raw;
}


int32_t Adafruit_BMP085::readPressure(void) {
  int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
  uint32_t B4, B7;

  UT = readRawTemperature();
  UP = readRawPressure();

#if BMP085_DEBUG == 1
  // use datasheet numbers!
  UT = 27898;
  UP = 23843;
  ac6 = 23153;
  ac5 = 32757;
  mc = -8711;
  md = 2868;
  b1 = 6190;
  b2 = 4;
  ac3 = -14383;
  ac2 = -72;
  ac1 = 408;
  ac4 = 32741;
  oversampling = 0;
#endif

  // 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;

#if BMP085_DEBUG == 1
  Serial.print("X1 = "); Serial.println(X1);
  Serial.print("X2 = "); Serial.println(X2);
  Serial.print("B5 = "); Serial.println(B5);
#endif

  // 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;

#if BMP085_DEBUG == 1
  Serial.print("B6 = "); Serial.println(B6);
  Serial.print("X1 = "); Serial.println(X1);
  Serial.print("X2 = "); Serial.println(X2);
  Serial.print("B3 = "); Serial.println(B3);
#endif

  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 BMP085_DEBUG == 1
  Serial.print("X1 = "); Serial.println(X1);
  Serial.print("X2 = "); Serial.println(X2);
  Serial.print("B4 = "); Serial.println(B4);
  Serial.print("B7 = "); Serial.println(B7);
#endif

  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;

#if BMP085_DEBUG == 1
  Serial.print("p = "); Serial.println(p);
  Serial.print("X1 = "); Serial.println(X1);
  Serial.print("X2 = "); Serial.println(X2);
#endif

  p = p + ((X1 + X2 + (int32_t)3791)>>4);
#if BMP085_DEBUG == 1
  Serial.print("p = "); Serial.println(p);
#endif
  return p;
}


float Adafruit_BMP085::readTemperature(void) {
  int32_t UT, X1, X2, B5;     // following ds convention
  float temp;

  UT = readRawTemperature();

#if BMP085_DEBUG == 1
  // use datasheet numbers!
  UT = 27898;
  ac6 = 23153;
  ac5 = 32757;
  mc = -8711;
  md = 2868;
#endif

  // 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;
}

float Adafruit_BMP085::readAltitude(float sealevelPressure) {
  float altitude;
  float pressure = readPressure();
  altitude = 44330 * (1.0 - pow(pressure /sealevelPressure,0.1903));
  return altitude;
}


/*********************************************************************/

uint8_t Adafruit_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 Adafruit_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;
}

void Adafruit_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
  Wire.endTransmission(); // end transmission
}

/*************************************************** 
  This is an example for the BMP085 Barometric Pressure & Temp Sensor

  Designed specifically to work with the Adafruit BMP085 Breakout 
  ----> https://www.adafruit.com/products/391

  These displays use I2C to communicate, 2 pins are required to  
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// SPARK CORE PINOUT ---------------------------------
// Connect VCC of the BMP085 sensor to 3.3* (NOT Vin!)
// Connect GND to GND
// Connect SCL to i2c clock - on Spark Core this is D1
// Connect SDA to i2c data - on Spark Core this is D0
// EOC is not used, it signifies an end of conversion
// XCLR is a reset pin, also not used here

Adafruit_BMP085 bmp;
  
void setup() {
  Serial.begin(9600);
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {}
  }
}
  
void loop() {
    Serial.print("Temperature = ");
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");
    
    Serial.print("Pressure = ");
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");
    
    // Calculate altitude assuming 'standard' barometric
    // pressure of 1013.25 millibar = 101325 Pascal
    Serial.print("Altitude = ");
    Serial.print(bmp.readAltitude());
    Serial.println(" meters");

  // you can get a more precise measurement of altitude
  // if you know the current sea level pressure which will
  // vary with weather and such. If it is 1015 millibars
  // that is equal to 101500 Pascals.
    Serial.print("Real altitude = ");
    Serial.print(bmp.readAltitude(101500));
    Serial.println(" meters");
    
    Serial.println();
    delay(500);
}

I copy pasted the code above and edited then end of it to display the temperature readings. Unfortunately I did not get it to work.

A picture of the setup and the code I used can be found below. I included below only the code that I edited. All the code above that excerpt was just as in the post above. If you want to see the full code it is available at: https://dl.dropboxusercontent.com/u/9883495/code.txt

Any ideas what could be wrong?

https://dl.dropboxusercontent.com/u/9883495/2014-01-05%2016.53.54.jpg

// SPARK CORE PINOUT ---------------------------------
// Connect VCC of the BMP085 sensor to 3.3* (NOT Vin!)
// Connect GND to GND
// Connect SCL to i2c clock - on Spark Core this is D1
// Connect SDA to i2c data - on Spark Core this is D0
// EOC is not used, it signifies an end of conversion
// XCLR is a reset pin, also not used here


Adafruit_BMP085 bmp;
float temperature = 0;
  
void setup() {
  Spark.variable("temperature", &temperature, INT);

}
  
void loop() {
    temperature = bmp.readTemperature();

}

Your wiring looks correct.

You are trying to send the float type temperature as an int.
DOUBLE Spark.variable() is not working yet, or I would suggest that.

First try this code all by itself:

double temperature = 1.0;
char bmpStr[10];

void setup() {
  Spark.variable("readBMP", &bmpStr, STRING);
}

void loop() {
  temperature += 0.5;
  sprintf(bmpStr,"%.2f",temperature);
  delay(500);
}

You can read it with this URL in your browser:
https://api.spark.io/v1/devices/DEVICE_ID/readBMP?access_token=ACCESS_TOKEN_ID

Once that’s working for you, try to modify this part of your code

Adafruit_BMP085 bmp;
float temperature = 0;
char bmpStr[8];

void setup() {
  Spark.variable("readBMP", &bmpStr, STRING);
}

void loop() {
    temperature = bmp.readTemperature();
    sprintf(bmpStr,"%.2f",temperature);
    
    // A little delay in case you are polling your sensor too fast.  
    // You might not need this.
    delay(100);
}

And use the same URL for the GET request.
https://api.spark.io/v1/devices/DEVICE_ID/readBMP?access_token=ACCESS_TOKEN_ID

Thanks for the help.

The first code works ok. The result increases after each refresh of the web page. The first result was as follows:

{
  "cmd": "VarReturn",
  "name": "readBMP",
  "TEMPORARY_allTypes": {
    "string": "9.00",
    "uint32": 959328304,
    "number": 959328304,
    "double": null,
    "float": 0.00016611884348094463,
    "raw": "9.00"
  },
  "result": "9.00",
  "coreInfo": {
    "last_app": "",
    "last_heard": "2014-01-08T19:33:34.091Z",
    "connected": true,
    "deviceID": "removed"
  }
}

Modifying the code, however, does not seem to work. The result of the code was as follows:

{
  "cmd": "VarReturn",
  "name": "readBMP",
  "TEMPORARY_allTypes": {
    "string": "0.05",
    "uint32": 808333365,
    "number": 808333365,
    "double": null,
    "float": 6.33693375551303e-10,
    "raw": "0.05"
  },
  "result": "0.05",
  "coreInfo": {
    "last_app": "",
    "last_heard": "2014-01-08T19:51:40.234Z",
    "connected": true,
    "deviceID": "removed"
  }
}

The code I used is as follows (see https://dl.dropboxusercontent.com/u/9883495/Code2.txt for the full code):

Adafruit_BMP085 bmp;

double temperature = 1.0;
char bmpStr[10];

void setup() {
  Spark.variable("readBMP", &bmpStr, STRING);
}

void loop() {
  temperature = bmp.readTemperature();
  //temperature = bmp.readPressure();
  //temperature = bmp.readAltitude();
  sprintf(bmpStr,"%.2f",temperature);
  delay(500);
}

What do you think. Is this because of the “imported” code not working or me doing something wrong?

Hmm, try changing the I2C address to the following:
#define BMP085_I2CADDR 0xEE

There is an issue right now with 7-bit addresses being interpreted as 8-bit addresses, so you if you shift the 0x77 address one bit to the left to make it 8-bit, you get 0xEE. https://community.spark.io/t/i2c-7-or-8-bit-addresses-fixed-pull-request-submitted/1328

I have my fingers crossed!

I now have bmp085 working by translating the non-Adafruit Arduino library I posted the link to earlier. I also have a sht21 humidity sensor working by translating another Arduino library (that one was almost trivial to do). Will post code later. I want to start a new thread for that but am getting server error 500 from the forum.

Paul

So you never got the above code working? Did you try the 0xEE thing?

@BDub, who are you asking? @Sauli was trying to get the Adafruit library working.

I had to change the i2c addresses for both devices in my code also of course.

The main problems with the library I converted was its mixed use of int, long, int16_t and int32_t. The issue was that int corresponds to int16_t on Arduino but corresponds to int32_t on Spark. Once I had replaced all the int and long, commented out a few includes, changed the i2c address and included an extra math library to get pow (), it worked.

Yeah I meant @Sauli, sorry! Time for bed :sleeping: :zzz:

I tried changing the I2C. Unfortunately it did not work. The result is the same as before changing it:

{
  "cmd": "VarReturn",
  "name": "readBMP",
  "TEMPORARY_allTypes": {
    "string": "0.05",
    "uint32": 808333365,
    "number": 808333365,
    "double": null,
    "float": 6.33693375551303e-10,
    "raw": "0.05"
  },
  "result": "0.05",
  "coreInfo": {
    "last_app": "",
    "last_heard": "2014-01-13T17:58:39.968Z",
    "connected": true,
    "deviceID": "48ff6a065067555055332387"
  }
}

I tried to look for the code you mentioned but was not able to find it. Is it available somewhere on this forum?

PaulRB got it working so I know the library works…

Can you post your full code so we can see what might be causing a problem? I don’t have one of these sensors so I might as if @PaulRB can chime in as well :slight_smile:

Sorry for not including it right away. The code is available at:

I wonder is there an easy way to include the code on the forum as well.

Aha, here we go. Please add this to your setup()
bmp.begin();

To add code here, just copy/paste it in… select it all, and press CTRL+K or press the Preformatted text button :wink:

Excellent, now it works. I get temperature, pressure and altitude readings ok. The only thing that is missing is the unit from the end of the reading but this, of course, is only a cosmetic problem.

Thank you very much for the help.

The code can be found below:

/*************************************************** 
  This is a library for the Adafruit BMP085/BMP180 Barometric Pressure + Temp sensor

  Designed specifically to work with the Adafruit BMP085 or BMP180 Breakout 
  ----> http://www.adafruit.com/products/391
  ----> http://www.adafruit.com/products/1603

  These displays use I2C to communicate, 2 pins are required to  
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/
#include <math.h>

#define boolean bool 
  
#define BMP085_DEBUG 0

//#define BMP085_I2CADDR 0x77
#define BMP085_I2CADDR 0xEE

#define BMP085_ULTRALOWPOWER 0
#define BMP085_STANDARD      1
#define BMP085_HIGHRES       2
#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


class Adafruit_BMP085 {
 public:
  Adafruit_BMP085();
  boolean begin(uint8_t mode = BMP085_ULTRAHIGHRES);  // by default go highres
  float readTemperature(void);
  int32_t readPressure(void);
  float readAltitude(float sealevelPressure = 101325); // std atmosphere
  uint16_t readRawTemperature(void);
  uint32_t readRawPressure(void);
  
 private:
  uint8_t read8(uint8_t addr);
  uint16_t read16(uint8_t addr);
  void write8(uint8_t addr, uint8_t data);

  uint8_t oversampling;

  int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
  uint16_t ac4, ac5, ac6;
};


/*************************************************** 
  This is a library for the Adafruit BMP085/BMP180 Barometric Pressure + Temp sensor

  Designed specifically to work with the Adafruit BMP085 or BMP180 Breakout 
  ----> http://www.adafruit.com/products/391
  ----> http://www.adafruit.com/products/1603

  These displays use I2C to communicate, 2 pins are required to  
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

Adafruit_BMP085::Adafruit_BMP085() {
}


boolean Adafruit_BMP085::begin(uint8_t mode) {
  if (mode > BMP085_ULTRAHIGHRES) 
    mode = BMP085_ULTRAHIGHRES;
  oversampling = mode;

  Wire.begin();

  if (read8(0xD0) != 0x55) return false;

  /* read calibration data */
  ac1 = read16(BMP085_CAL_AC1);
  ac2 = read16(BMP085_CAL_AC2);
  ac3 = read16(BMP085_CAL_AC3);
  ac4 = read16(BMP085_CAL_AC4);
  ac5 = read16(BMP085_CAL_AC5);
  ac6 = read16(BMP085_CAL_AC6);

  b1 = read16(BMP085_CAL_B1);
  b2 = read16(BMP085_CAL_B2);

  mb = read16(BMP085_CAL_MB);
  mc = read16(BMP085_CAL_MC);
  md = read16(BMP085_CAL_MD);
#if (BMP085_DEBUG == 1)
  Serial.print("ac1 = "); Serial.println(ac1, DEC);
  Serial.print("ac2 = "); Serial.println(ac2, DEC);
  Serial.print("ac3 = "); Serial.println(ac3, DEC);
  Serial.print("ac4 = "); Serial.println(ac4, DEC);
  Serial.print("ac5 = "); Serial.println(ac5, DEC);
  Serial.print("ac6 = "); Serial.println(ac6, DEC);

  Serial.print("b1 = "); Serial.println(b1, DEC);
  Serial.print("b2 = "); Serial.println(b2, DEC);

  Serial.print("mb = "); Serial.println(mb, DEC);
  Serial.print("mc = "); Serial.println(mc, DEC);
  Serial.print("md = "); Serial.println(md, DEC);
#endif
  return true;
}

uint16_t Adafruit_BMP085::readRawTemperature(void) {
  write8(BMP085_CONTROL, BMP085_READTEMPCMD);
  delay(5);
#if BMP085_DEBUG == 1
  Serial.print("Raw temp: "); Serial.println(read16(BMP085_TEMPDATA));
#endif
  return read16(BMP085_TEMPDATA);
}

uint32_t Adafruit_BMP085::readRawPressure(void) {
  uint32_t raw;

  write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));

  if (oversampling == BMP085_ULTRALOWPOWER) 
    delay(5);
  else if (oversampling == BMP085_STANDARD) 
    delay(8);
  else if (oversampling == BMP085_HIGHRES) 
    delay(14);
  else 
    delay(26);

  raw = read16(BMP085_PRESSUREDATA);

  raw <<= 8;
  raw |= read8(BMP085_PRESSUREDATA+2);
  raw >>= (8 - oversampling);

 /* this pull broke stuff, look at it later?
  if (oversampling==0) {
    raw <<= 8;
    raw |= read8(BMP085_PRESSUREDATA+2);
    raw >>= (8 - oversampling);
  }
 */

#if BMP085_DEBUG == 1
  Serial.print("Raw pressure: "); Serial.println(raw);
#endif
  return raw;
}


int32_t Adafruit_BMP085::readPressure(void) {
  int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
  uint32_t B4, B7;

  UT = readRawTemperature();
  UP = readRawPressure();

#if BMP085_DEBUG == 1
  // use datasheet numbers!
  UT = 27898;
  UP = 23843;
  ac6 = 23153;
  ac5 = 32757;
  mc = -8711;
  md = 2868;
  b1 = 6190;
  b2 = 4;
  ac3 = -14383;
  ac2 = -72;
  ac1 = 408;
  ac4 = 32741;
  oversampling = 0;
#endif

  // 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;

#if BMP085_DEBUG == 1
  Serial.print("X1 = "); Serial.println(X1);
  Serial.print("X2 = "); Serial.println(X2);
  Serial.print("B5 = "); Serial.println(B5);
#endif

  // 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;

#if BMP085_DEBUG == 1
  Serial.print("B6 = "); Serial.println(B6);
  Serial.print("X1 = "); Serial.println(X1);
  Serial.print("X2 = "); Serial.println(X2);
  Serial.print("B3 = "); Serial.println(B3);
#endif

  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 BMP085_DEBUG == 1
  Serial.print("X1 = "); Serial.println(X1);
  Serial.print("X2 = "); Serial.println(X2);
  Serial.print("B4 = "); Serial.println(B4);
  Serial.print("B7 = "); Serial.println(B7);
#endif

  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;

#if BMP085_DEBUG == 1
  Serial.print("p = "); Serial.println(p);
  Serial.print("X1 = "); Serial.println(X1);
  Serial.print("X2 = "); Serial.println(X2);
#endif

  p = p + ((X1 + X2 + (int32_t)3791)>>4);
#if BMP085_DEBUG == 1
  Serial.print("p = "); Serial.println(p);
#endif
  return p;
}


float Adafruit_BMP085::readTemperature(void) {
  int32_t UT, X1, X2, B5;     // following ds convention
  float temp;

  UT = readRawTemperature();

#if BMP085_DEBUG == 1
  // use datasheet numbers!
  UT = 27898;
  ac6 = 23153;
  ac5 = 32757;
  mc = -8711;
  md = 2868;
#endif

  // 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;
}

float Adafruit_BMP085::readAltitude(float sealevelPressure) {
  float altitude;
  float pressure = readPressure();
  altitude = 44330 * (1.0 - pow(pressure /sealevelPressure,0.1903));
  return altitude;
}


/*********************************************************************/

uint8_t Adafruit_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 Adafruit_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;
}

void Adafruit_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
  Wire.endTransmission(); // end transmission
}

/*************************************************** 
  This is an example for the BMP085 Barometric Pressure & Temp Sensor

  Designed specifically to work with the Adafruit BMP085 Breakout 
  ----> https://www.adafruit.com/products/391

  These displays use I2C to communicate, 2 pins are required to  
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// SPARK CORE PINOUT ---------------------------------
// Connect VCC of the BMP085 sensor to 3.3* (NOT Vin!)
// Connect GND to GND
// Connect SCL to i2c clock - on Spark Core this is D1
// Connect SDA to i2c data - on Spark Core this is D0
// EOC is not used, it signifies an end of conversion
// XCLR is a reset pin, also not used here

/*
Adafruit_BMP085 bmp;
float temperature = 0;
char bmpStr[8];

void setup() {
  Spark.variable("readBMP", &bmpStr, STRING);
}

void loop() {
    temperature = bmp.readTemperature();
    sprintf(bmpStr,"%.2f",temperature);

    // A little delay in case you are polling your sensor too fast.  
    // You might not need this.
    delay(1000);
}

*/

//***********************************************************

Adafruit_BMP085 bmp;



double temperature = 1.0;
char bmpStr[10];

void setup() {
  bmp.begin();
  Spark.variable("readBMP", &bmpStr, STRING);
}

void loop() {
  temperature = bmp.readTemperature();
  //temperature = bmp.readPressure();
  //temperature = bmp.readAltitude();
  sprintf(bmpStr,"%.2f",temperature);
  delay(500);
}
2 Likes

Hi all!

Been trying to get this code working on the electron but getting the following errors…

../hal/inc/pinmap_hal.h:169:19: error: expected unqualified-id before numeric constant
 #define B3        27
                   ^

bmp180.cpp:178:19: note: in expansion of macro 'B3'
   return raw;
                   ^
../hal/inc/pinmap_hal.h:170:19: error: expected unqualified-id before numeric constant
 #define B4        28
                   ^

bmp180.cpp:179:12: note: in expansion of macro 'B4'
 }
            ^
bmp180.cpp:202:3: error: 'X1' was not declared in this scope
   ac4 = 32741;
   ^

bmp180.cpp:203:3: error: 'X2' was not declared in this scope
   oversampling = 0;
   ^

bmp180.cpp:213:3: error: 'B6' was not declared in this scope
   Serial.print("X2 = "); Serial.println(X2);
   ^

bmp180.cpp:216:3: error: 'X3' was not declared in this scope
 
   ^

bmp180.cpp:230:3: error: 'B7' was not declared in this scope
 
   ^

bmp180.cpp:240:5: error: 'p' was not declared in this scope
   Serial.print("B4 = "); Serial.println(B4);
     ^

bmp180.cpp:242:5: error: 'p' was not declared in this scope
 #endif
     ^

bmp180.cpp:244:9: error: 'p' was not declared in this scope
   if (B7 < 0x80000000) {
         ^

In file included from ../hal/inc/gpio_hal.h:31:0,
                 from ../hal/src/stm32f2xx/platform_headers.h:16,
                 from ./inc/application.h:32,
                 from bmp180.cpp:17:
bmp180.cpp: In member function 'float Adafruit_BMP085::readTemperature()':
../hal/inc/pinmap_hal.h:171:19: error: expected unqualified-id before numeric constant
 #define B5        29
                   ^

bmp180.cpp:263:23: note: in expansion of macro 'B5'
   return p;

I have tried renaming those variables, B5 to BB5 etc, and this means it will compile ok but I’m not getting any sensible data back, just 255’s back from the i2c requests.

Any ideas?

Cheers

I am trying to do the exact same thing and getting the same errors you are. Did you ever get this working?