OS6. i2s BME280 library, also works with BMP280 without Humidity. See BME280.h for example code.
BME280.h@0:19fab6c64964, 2021-01-02 (annotated)
- Committer:
- star297
- Date:
- Sat Jan 02 10:32:48 2021 +0000
- Revision:
- 0:19fab6c64964
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:19fab6c64964 | 1 | #ifndef BME_H |
star297 | 0:19fab6c64964 | 2 | #define BME_H |
star297 | 0:19fab6c64964 | 3 | |
star297 | 0:19fab6c64964 | 4 | #include "mbed.h" |
star297 | 0:19fab6c64964 | 5 | |
star297 | 0:19fab6c64964 | 6 | #define _debug 1 // '1' to enable prinf BME's registers |
star297 | 0:19fab6c64964 | 7 | |
star297 | 0:19fab6c64964 | 8 | /* |
star297 | 0:19fab6c64964 | 9 | Bosch Sensortec BMP280 BME280 i2c example code |
star297 | 0:19fab6c64964 | 10 | using 64bit numeric calculations as recomended by Bosch. |
star297 | 0:19fab6c64964 | 11 | Some sensors supplied from Chinese suppliers may have 'suspect' |
star297 | 0:19fab6c64964 | 12 | quality sensor with different i2c address. |
star297 | 0:19fab6c64964 | 13 | My sensors were address 0xEC and all the ones I have |
star297 | 0:19fab6c64964 | 14 | differ quite a bit in tolerance :( so be aware if accuracy is paramount. |
star297 | 0:19fab6c64964 | 15 | */ |
star297 | 0:19fab6c64964 | 16 | |
star297 | 0:19fab6c64964 | 17 | /* |
star297 | 0:19fab6c64964 | 18 | #include "mbed.h" |
star297 | 0:19fab6c64964 | 19 | #include "BME280.h" |
star297 | 0:19fab6c64964 | 20 | |
star297 | 0:19fab6c64964 | 21 | BME sensor(D0,D1, 0xEC); // sda, clk, 8bit address (NUCLEO-L432) |
star297 | 0:19fab6c64964 | 22 | DigitalOut led(LED1); |
star297 | 0:19fab6c64964 | 23 | |
star297 | 0:19fab6c64964 | 24 | float temp, humidity, pressure, altitude; |
star297 | 0:19fab6c64964 | 25 | |
star297 | 0:19fab6c64964 | 26 | int main() |
star297 | 0:19fab6c64964 | 27 | { |
star297 | 0:19fab6c64964 | 28 | |
star297 | 0:19fab6c64964 | 29 | int BMPE_id = sensor.init(); // initialise and get sensor id |
star297 | 0:19fab6c64964 | 30 | |
star297 | 0:19fab6c64964 | 31 | if(!BMPE_id) { |
star297 | 0:19fab6c64964 | 32 | printf("No sensor detected!!\n"); |
star297 | 0:19fab6c64964 | 33 | while(1){ |
star297 | 0:19fab6c64964 | 34 | led=!led; |
star297 | 0:19fab6c64964 | 35 | ThisThread::sleep_for(200ms); |
star297 | 0:19fab6c64964 | 36 | } |
star297 | 0:19fab6c64964 | 37 | } |
star297 | 0:19fab6c64964 | 38 | |
star297 | 0:19fab6c64964 | 39 | if(_debug){ // set _debug to '1' to see BME's registers |
star297 | 0:19fab6c64964 | 40 | ThisThread::sleep_for(2s); |
star297 | 0:19fab6c64964 | 41 | } |
star297 | 0:19fab6c64964 | 42 | |
star297 | 0:19fab6c64964 | 43 | while(1) { |
star297 | 0:19fab6c64964 | 44 | led=1; |
star297 | 0:19fab6c64964 | 45 | printf("\033[0m\033[2J\033[H------- BME280-BMP280 Sensor example -------\r\n\n"); |
star297 | 0:19fab6c64964 | 46 | if(BMPE_id==0x60) { |
star297 | 0:19fab6c64964 | 47 | printf("BME280 detected id: 0x%x\n\n",BMPE_id); |
star297 | 0:19fab6c64964 | 48 | } else if(BMPE_id==0x58) { |
star297 | 0:19fab6c64964 | 49 | printf("BMP280 detected id: 0x%x\n\n",BMPE_id); |
star297 | 0:19fab6c64964 | 50 | } |
star297 | 0:19fab6c64964 | 51 | |
star297 | 0:19fab6c64964 | 52 | temp = sensor.getTemperature(); |
star297 | 0:19fab6c64964 | 53 | |
star297 | 0:19fab6c64964 | 54 | printf("Temperature: %3.2f %cc \n",temp,0xb0); |
star297 | 0:19fab6c64964 | 55 | |
star297 | 0:19fab6c64964 | 56 | if(sensor.chip_id==0x60) { // only BME has Humidity |
star297 | 0:19fab6c64964 | 57 | humidity = sensor.getHumidity(); |
star297 | 0:19fab6c64964 | 58 | printf("Humidity: %2.2f %cRh \n", humidity,0x25); |
star297 | 0:19fab6c64964 | 59 | } |
star297 | 0:19fab6c64964 | 60 | pressure = sensor.getPressure(); |
star297 | 0:19fab6c64964 | 61 | printf("Pressure: %4.2f mbar's \n\n",pressure); |
star297 | 0:19fab6c64964 | 62 | |
star297 | 0:19fab6c64964 | 63 | altitude = 44330.0f*( 1.0f - pow((pressure/1013.25f), (1.0f/5.255f)))+18; // Calculate altitude in meters |
star297 | 0:19fab6c64964 | 64 | printf("Altitude: %3.1f meters %4.1f feet \r\n(Referenced to 1,013.25 millibars @ sea level) \r\n\n", altitude,altitude*3.2810f); |
star297 | 0:19fab6c64964 | 65 | led=0; |
star297 | 0:19fab6c64964 | 66 | ThisThread::sleep_for(1s); |
star297 | 0:19fab6c64964 | 67 | } |
star297 | 0:19fab6c64964 | 68 | } |
star297 | 0:19fab6c64964 | 69 | */ |
star297 | 0:19fab6c64964 | 70 | |
star297 | 0:19fab6c64964 | 71 | class BME |
star297 | 0:19fab6c64964 | 72 | { |
star297 | 0:19fab6c64964 | 73 | public: |
star297 | 0:19fab6c64964 | 74 | |
star297 | 0:19fab6c64964 | 75 | BME(PinName sda, PinName scl, char slave_adr); |
star297 | 0:19fab6c64964 | 76 | |
star297 | 0:19fab6c64964 | 77 | int init(void); |
star297 | 0:19fab6c64964 | 78 | int chipID(void); |
star297 | 0:19fab6c64964 | 79 | float getTemperature(void); |
star297 | 0:19fab6c64964 | 80 | float getPressure(void); |
star297 | 0:19fab6c64964 | 81 | float getHumidity(void); |
star297 | 0:19fab6c64964 | 82 | |
star297 | 0:19fab6c64964 | 83 | uint16_t chip_id; |
star297 | 0:19fab6c64964 | 84 | |
star297 | 0:19fab6c64964 | 85 | private: |
star297 | 0:19fab6c64964 | 86 | |
star297 | 0:19fab6c64964 | 87 | I2C bme; |
star297 | 0:19fab6c64964 | 88 | void initialize(void); |
star297 | 0:19fab6c64964 | 89 | char address; |
star297 | 0:19fab6c64964 | 90 | uint16_t dig_T1; |
star297 | 0:19fab6c64964 | 91 | int16_t dig_T2, dig_T3; |
star297 | 0:19fab6c64964 | 92 | uint16_t dig_P1; |
star297 | 0:19fab6c64964 | 93 | int16_t dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9; |
star297 | 0:19fab6c64964 | 94 | uint16_t dig_H1, dig_H3; |
star297 | 0:19fab6c64964 | 95 | int16_t dig_H2, dig_H4, dig_H5, dig_H6; |
star297 | 0:19fab6c64964 | 96 | int32_t t_fine; |
star297 | 0:19fab6c64964 | 97 | bool dbg_on; |
star297 | 0:19fab6c64964 | 98 | }; |
star297 | 0:19fab6c64964 | 99 | #endif // BME_H |