BME280 mbed i2c

Dependencies:   EthernetInterface HTTPClient mbed-rtos mbed

Fork of php_access by shinichi satoh

Files at this revision

API Documentation at this revision

Comitter:
thursday1024
Date:
Thu Jul 28 22:55:30 2016 +0000
Parent:
1:3c8fcecce568
Commit message:
BME280 I2C

Changed in this revision

BME280.cpp Show annotated file Show diff for this revision Revisions of this file
BME280.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
php_access.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BME280.cpp	Thu Jul 28 22:55:30 2016 +0000
@@ -0,0 +1,193 @@
+/**
+ *  BME280 Combined humidity and pressure sensor library
+ *
+ *  @author  Toyomasa Watarai
+ *  @version 1.0
+ *  @date    06-April-2015
+ *
+ *  Library for "BME280 temperature, humidity and pressure sensor module" from Switch Science
+ *    https://www.switch-science.com/catalog/2236/
+ *
+ *  For more information about the BME280:
+ *    http://ae-bst.resource.bosch.com/media/products/dokumente/bme280/BST-BME280_DS001-10.pdf
+ */
+
+#include "mbed.h"
+#include "BME280.h"
+
+BME280::BME280(PinName sda, PinName scl, char slave_adr)
+    :
+    i2c_p(new I2C(sda, scl)), 
+    i2c(*i2c_p),
+    address(slave_adr),
+    t_fine(0)
+{
+    initialize();
+}
+
+BME280::BME280(I2C &i2c_obj, char slave_adr)
+    :
+    i2c_p(NULL), 
+    i2c(i2c_obj),
+    address(slave_adr),
+    t_fine(0)
+{
+    initialize();
+}
+
+BME280::~BME280()
+{
+    if (NULL != i2c_p)
+        delete  i2c_p;
+}
+    
+void BME280::initialize()
+{
+    char cmd[18];
+ 
+    cmd[0] = 0xf2; // ctrl_hum
+    cmd[1] = 0x01; // Humidity oversampling x1
+    i2c.write(address, cmd, 2);
+ 
+    cmd[0] = 0xf4; // ctrl_meas
+    cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
+    i2c.write(address, cmd, 2);
+ 
+    cmd[0] = 0xf5; // config
+    cmd[1] = 0xa0; // Standby 1000ms, Filter off
+    i2c.write(address, cmd, 2);
+ 
+    cmd[0] = 0x88; // read dig_T regs
+    i2c.write(address, cmd, 1);
+    i2c.read(address, cmd, 6);
+ 
+    dig_T1 = (cmd[1] << 8) | cmd[0];
+    dig_T2 = (cmd[3] << 8) | cmd[2];
+    dig_T3 = (cmd[5] << 8) | cmd[4];
+ 
+    DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
+ 
+    cmd[0] = 0x8E; // read dig_P regs
+    i2c.write(address, cmd, 1);
+    i2c.read(address, cmd, 18);
+ 
+    dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
+    dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
+    dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
+    dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
+    dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
+    dig_P6 = (cmd[11] << 8) | cmd[10];
+    dig_P7 = (cmd[13] << 8) | cmd[12];
+    dig_P8 = (cmd[15] << 8) | cmd[14];
+    dig_P9 = (cmd[17] << 8) | cmd[16];
+ 
+    DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
+ 
+    cmd[0] = 0xA1; // read dig_H regs
+    i2c.write(address, cmd, 1);
+    i2c.read(address, cmd, 1);
+     cmd[1] = 0xE1; // read dig_H regs
+    i2c.write(address, &cmd[1], 1);
+    i2c.read(address, &cmd[1], 7);
+
+    dig_H1 = cmd[0];
+    dig_H2 = (cmd[2] << 8) | cmd[1];
+    dig_H3 = cmd[3];
+    dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
+    dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
+    dig_H6 = cmd[7];
+ 
+    DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
+}
+ 
+float BME280::getTemperature()
+{
+    uint32_t temp_raw;
+    float tempf;
+    char cmd[4];
+ 
+    cmd[0] = 0xfa; // temp_msb
+    i2c.write(address, cmd, 1);
+    i2c.read(address, &cmd[1], 3);
+ 
+    temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
+ 
+    int32_t temp;
+ 
+    temp =
+        (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
+        ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
+ 
+    t_fine = temp;
+    temp = (temp * 5 + 128) >> 8;
+    tempf = (float)temp;
+ 
+    return (tempf/100.0f);
+}
+ 
+float BME280::getPressure()
+{
+    uint32_t press_raw;
+    float pressf;
+    char cmd[4];
+ 
+    cmd[0] = 0xf7; // press_msb
+    i2c.write(address, cmd, 1);
+    i2c.read(address, &cmd[1], 3);
+ 
+    press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
+ 
+    int32_t var1, var2;
+    uint32_t press;
+ 
+    var1 = (t_fine >> 1) - 64000;
+    var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
+    var2 = var2 + ((var1 * dig_P5) << 1);
+    var2 = (var2 >> 2) + (dig_P4 << 16);
+    var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
+    var1 = ((32768 + var1) * dig_P1) >> 15;
+    if (var1 == 0) {
+        return 0;
+    }
+    press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
+    if(press < 0x80000000) {
+        press = (press << 1) / var1;
+    } else {
+        press = (press / var1) * 2;
+    }
+    var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
+    var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
+    press = (press + ((var1 + var2 + dig_P7) >> 4));
+ 
+    pressf = (float)press;
+    return (pressf/100.0f);
+}
+ 
+float BME280::getHumidity()
+{
+    uint32_t hum_raw;
+    float humf;
+    char cmd[4];
+ 
+    cmd[0] = 0xfd; // hum_msb
+    i2c.write(address, cmd, 1);
+    i2c.read(address, &cmd[1], 2);
+ 
+    hum_raw = (cmd[1] << 8) | cmd[2];
+ 
+    int32_t v_x1;
+ 
+    v_x1 = t_fine - 76800;
+    v_x1 =  (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
+               ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
+                                            (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
+                                            (int32_t)dig_H2 + 8192) >> 14));
+    v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
+    v_x1 = (v_x1 < 0 ? 0 : v_x1);
+    v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
+ 
+    humf = (float)(v_x1 >> 12);
+ 
+    return (humf/1024.0f);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BME280.h	Thu Jul 28 22:55:30 2016 +0000
@@ -0,0 +1,102 @@
+/**
+ *  BME280 Combined humidity and pressure sensor library
+ *
+ *  @author  Toyomasa Watarai
+ *  @version 1.0
+ *  @date    06-April-2015
+ *
+ *  Library for "BME280 temperature, humidity and pressure sensor module" from Switch Science
+ *    https://www.switch-science.com/catalog/2236/
+ *
+ *  For more information about the BME280:
+ *    http://ae-bst.resource.bosch.com/media/products/dokumente/bme280/BST-BME280_DS001-10.pdf
+ */
+ 
+#ifndef MBED_BME280_H
+#define MBED_BME280_H
+
+#include "mbed.h"
+
+//#define _DEBUG
+#define DEFAULT_SLAVE_ADDRESS (0x76 << 1)
+
+#ifdef _DEBUG
+extern Serial pc;
+#define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
+#else
+#define DEBUG_PRINT(...)
+#endif
+
+ 
+/** BME280 class
+ *
+ *  BME280: A library to correct environmental data using Boshe BME280 device
+ *
+ *  BME280 is an environmental sensor
+ *  @endcode
+ */
+ 
+class BME280
+{
+public:
+
+    /** Create a BME280 instance
+     *  which is connected to specified I2C pins with specified address
+     *
+     * @param sda I2C-bus SDA pin
+     * @param scl I2C-bus SCL pin
+     * @param slave_adr (option) I2C-bus address (default: 0x76)
+     */
+    BME280(PinName sda, PinName sck, char slave_adr = DEFAULT_SLAVE_ADDRESS);
+
+    /** Create a BME280 instance
+     *  which is connected to specified I2C pins with specified address
+     *
+     * @param i2c_obj I2C object (instance)
+     * @param slave_adr (option) I2C-bus address (default: 0x76)
+     */
+    BME280(I2C &i2c_obj, char slave_adr = DEFAULT_SLAVE_ADDRESS);
+
+    /** Destructor of BME280
+     */
+    virtual ~BME280();
+
+    /** Initializa BME280 sensor
+     *
+     *  Configure sensor setting and read parameters for calibration
+     *
+     */
+    void initialize(void);
+
+    /** Read the current temperature value (degree Celsius) from BME280 sensor
+     *
+     */
+    float getTemperature(void);
+
+    /** Read the current pressure value (hectopascal)from BME280 sensor
+     *
+     */
+    float getPressure(void);
+
+    /** Read the current humidity value (humidity %) from BME280 sensor
+     *
+     */
+    float getHumidity(void);
+
+private:
+
+    I2C         *i2c_p;
+    I2C         &i2c;
+    char        address;
+    uint16_t    dig_T1;
+    int16_t     dig_T2, dig_T3;
+    uint16_t    dig_P1;
+    int16_t     dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9;
+    uint16_t    dig_H1, dig_H3;
+    int16_t     dig_H2, dig_H4, dig_H5, dig_H6;
+    int32_t     t_fine;
+
+};
+
+#endif // MBED_BME280_H
+
--- a/main.cpp	Thu Jul 30 07:29:17 2015 +0000
+++ b/main.cpp	Thu Jul 28 22:55:30 2016 +0000
@@ -1,70 +1,104 @@
-// php_acces main.cpp
- 
-#include "mbed.h" 
-#include "EthernetInterface.h"
-#include "HTTPClient.h"
-#define BUFF_SIZE 128
-
-extern void php_access_main( char *uri, char *ch_name, int* sp );
-char php_uri[]="https://alpha.c.dendai.ac.jp/plot/save_data.php"; 
-
-char ch_str[] ="ch13894";
-
-extern HTTPClient http;
-EthernetInterface eth;
-
-static int speed=0;
-void thread_main( void const *av ){
-    char ch_name[BUFF_SIZE];
-    strcpy( ch_name, ch_str );
-    printf( "Channel Name:%s\n", ch_name );
-    
-    php_access_main( php_uri, ch_name, &speed ); // 
-    
-    while(true) Thread::wait(1000);
-}
-
-// memo
-// R-LED -> LED1; G-LED -> LED2; B-LED -> LED3;
-// sw3 -> PTA4; sw2 -> PTC6;
-
-DigitalOut my_LED0(LED1);
-DigitalOut my_LED1(LED3);
-DigitalOut my_LED2(LED2);
-DigitalIn  my_sw0(PTA4); 
-DigitalIn  my_sw1(PTC6); 
-
-int main() {
-    my_LED0=1;
-    my_LED1=1;
-    Timer t;
-    t.start();
-    
-    int ret = eth.init(); //Use DHCP
-    printf( "php_test Starting,...\n" ) ;
-    while(1) {
-        ret = eth.connect();
-        if( ret==0 )break ; // DHCP OK
-        Thread::wait( 100 );
-    }
-    t.stop();
-    srand(t.read_ms());
-
-    printf( "IP = %s\n", eth.getIPAddress() );
-    
-    #define STACK_SIZE 20000
-    Thread thr( thread_main, NULL, osPriorityNormal, STACK_SIZE );
-    speed=0;
-    int i=0;
-    while(true){
-        if(!my_sw0 && speed<=0)speed++;
-        if(!my_sw1 && speed>=0)speed--;
-        if(speed>0 ){my_LED0=0;my_LED1=1;my_LED2=1;}
-        if(speed<0 ){my_LED0=1;my_LED1=0;my_LED2=1;}
-        if(speed==0){my_LED0=1;my_LED1=1;my_LED2=0;}
-        speed = (rand()%3)-1;
-        Thread::wait(1000);
-        i++;
-    }
-}
-
+// php_acces main.cpp
+  
+#include "mbed.h" 
+#include "BME280.h"
+#include "EthernetInterface.h"
+#include "HTTPClient.h"
+#define BUFF_SIZE 128
+
+extern void php_access_main( char *uri, char *ch_name, int* sp );
+char php_uri[]="https://alpha.c.dendai.ac.jp/plot/save_data.php"; 
+
+//char ch_str[] ="ch13894";
+char ch_str[] ="ch1389400";
+
+
+extern HTTPClient http;
+EthernetInterface eth;
+
+static int speed=0;
+void thread_main( void const *av ){
+    char ch_name[BUFF_SIZE];
+    strcpy( ch_name, ch_str );
+    printf( "Channel Name:%s\n", ch_name );
+    
+    php_access_main( php_uri, ch_name, &speed ); // 
+    
+    while(true) Thread::wait(1000);
+}
+
+// memo
+// R-LED -> LED1; G-LED -> LED2; B-LED -> LED3;
+// sw3 -> PTA4; sw2 -> PTC6;
+
+DigitalOut my_LED0(LED1);
+DigitalOut my_LED1(LED3);
+DigitalOut my_LED2(LED2);
+DigitalIn  my_sw0(PTA4); 
+DigitalIn  my_sw1(PTC6); 
+
+#if defined(TARGET_LPC1768)
+BME280 sensor(p28, p27);
+#else
+BME280 sensor(I2C_SDA, I2C_SCL);
+#endif
+
+int main() {
+
+    
+    //while(1) {
+    //    printf("hoge test = %2.2f degC, %04.2f hPa, %2.2f %%\n", sensor.getTemperature(), sensor.getPressure(), sensor.getHumidity());
+    //    wait(1);
+    //}
+
+
+    
+    
+    
+    my_LED0=1;
+    my_LED1=1;
+    Timer t;
+    t.start();
+    
+    int ret = eth.init(); //Use DHCP
+    printf( "php_test Starting,...\n" ) ;
+    while(1) {
+        ret = eth.connect();
+        if( ret==0 )break ; // DHCP OK
+        Thread::wait( 100 );
+    }
+    t.stop();
+    srand(t.read_ms());
+
+    printf( "IP = %s\n", eth.getIPAddress() );
+    
+    #define STACK_SIZE 20000
+    Thread thr( thread_main, NULL, osPriorityNormal, STACK_SIZE );
+    speed=0;
+    int i=0;
+    //double tmp;
+    while(true){
+        speed = sensor.getTemperature()*100, 
+
+/*        printf("hoge test = %2.2f degC, %04.2f hPa, %2.2f %%\n", 
+            tmp, 
+            sensor.getPressure(), 
+            sensor.getHumidity());
+        speed = tmp*100;
+*/    
+  
+        
+/*        if(!my_sw0 && speed<=0)speed++;
+        if(!my_sw1 && speed>=0)speed--;
+        if(speed>0 ){my_LED0=0;my_LED1=1;my_LED2=1;}
+        if(speed<0 ){my_LED0=1;my_LED1=0;my_LED2=1;}
+        if(speed==0){my_LED0=1;my_LED1=1;my_LED2=0;}
+        speed = (rand()%3)-1;
+*/      
+      
+        Thread::wait(1000);
+        i++;
+    }
+}
+
+
--- a/php_access.cpp	Thu Jul 30 07:29:17 2015 +0000
+++ b/php_access.cpp	Thu Jul 28 22:55:30 2016 +0000
@@ -24,7 +24,7 @@
         HTTPMap params;
         HTTPText inText( buff, BUFFSIZE );
         
-        sprintf( str, "%d", j+=*sp );
+        sprintf( str, "%.2f", (*sp)/100.0 );
         params.put("data", str );
         params.put("ch", ch_name );
         char str_sp[128]; sprintf( str_sp, "%d", *sp );