Code not returning correct altitude, any ideas?

315MHz and 433MHz wireless connections are very commonly used by consumer electronics including weather stations, home automation remote controls, power consumption meters, car alarms, and also many DIY projects.
Post Reply
rachalmers
Posts: 15
Joined: Thu Jul 24, 2014 3:01 pm
Location: Suffolk, England
Contact:

Code not returning correct altitude, any ideas?

Post by rachalmers » Mon Aug 18, 2014 10:11 am

This code is straight out of the Adafruit BMP085 sample code and library.
But obviously I'm doing something wrong, because its returning.

Fixed: It now returns 0.00 as it should..

This is the code I changed. and also added #include <math.h> and deleted that declaration for pow()

Code: Select all

  const uint32_t P0 = 1013.25; // seaLevel,
  		ut = bmp180_get_value(BMP180_TEMPERATURE, 5);
		t = (bmp180_get_temperature(ut));
		up = bmp180_get_value(BMP180_PRESSURE_3, 26);
		p = bmp180_get_pressure(up);                 

  uint32_t T = t/10;
  //temp = t; // returned by sensor above, degrees C
  uint32_t P = p/100; // atmospheric pressure in hpa
  uint32_t h, altitude; //height in meters
  h = (((double)pow((P0/P), 0.190223F) - 1.0F) * (T + 273.15F)) / 0.0065F;
  


23140.43
as the altitude. When I'm practically at sea level as it is. Maybe 10 or so meters above sea level, but not 23,140 meters ....

The code is from the BMP085_U library basically.
The Arduino + Weathershield is returning everything else ok,

Code: Select all

/* -------------- Get the altitude ---------------------- */  
/**************************************************************************/
/*!
    Calculates the altitude (in meters) from the specified atmospheric
    pressure (in hPa), sea-level pressure (in hPa), and temperature (in �C)

    @param  seaLevel      Sea-level pressure in hPa
    @param  atmospheric   Atmospheric pressure in hPa
    @param  temp          Temperature in degrees Celsius
*/
/**************************************************************************/
  /* Hyposometric formula:                      */
  /*                                            */
  /*     ((P0/P)^(1/5.257) - 1) * (T + 273.15)  */
  /* h = -------------------------------------  */
  /*                   0.0065                   */
  /*                                            */
  /* where: h   = height (in meters)            */
  /*        P0  = sea-level pressure (in hPa)   */
  /*        P   = atmospheric pressure (in hPa) */
  /*        T   = temperature (in �C)           */
  
  const uint32_t seaLevel, P0 = 1013.25;
  float pow (double base, double exponent);
  uint32_t temp, T = t;
  temp = t; // returned by sensor above, degrees C
  uint32_t atmospheric, P = p; // pressure in hpa
  uint32_t h, altitude; //height in meters
  h = (((float)pow((P0/P), 0.190223F) - 1.0F) * (T + 273.15F)) / 0.0065F;
  
  //h = (((float)pow((seaLevel/atmospheric), 0.190223F) - 1.0F)
  //       * (temp + 273.15F)) / 0.0065F;
  altitude = h;
  		putchar(',');
		utoa(altitude/100, buffer, 10);
		usart_puts(buffer);
		putchar('.');
		if (altitude%100 < 10)
		{
			putchar('0');
		}
		utoa(altitude%100, buffer, 10);
		usart_puts(buffer);
/*---------------------------------------------------------*/
Station output is looking ok, apart from altitude. Last number on the second last line.

Code: Select all

HEX,05,0d,a5,74,a8,40,TEMPERATURE:17.4
HEX,15,0d,a6,0f,9f,00,HUMIDITY:60
HEX,75,0d,a2,2b,dd,20,WINDDIRECTION:WSW WINDSPEED:3.4
HEX,f5,0d,a3,79,c8,80,GUSTDIRECTION:SSW GUSTSPEED:5.5
HEX,05,0d,a5,74,a8,40,TEMPERATURE:17.4
HEX,15,0d,a6,0f,9f,00,HUMIDITY:60
HEX,75,0d,a2,2b,dd,20,WINDDIRECTION:WSW WINDSPEED:3.4
HEX,f5,0d,a3,79,c8,80,GUSTDIRECTION:SSW GUSTSPEED:5.5
--END--
I2C UT=26972
I2C T=21
I2C UP=334677
I2C P=1007.29
80,27,27,17.4,60,-,SSW,5.5,WSW,3.4,1007.29,23140.43
Indoor Temp=21.3


Post Reply