Skip to content
wuzhun.wz edited this page Jun 12, 2018 · 1 revision

#设备功能

bmp280是一款用了测量气压值和温度值的传感器,它具有较高的精确度,通过I2C或SPI协议进行数据交互,这里我们会定时读取传感器的气压值,并上报到LD云端平台;

#硬件资源

DevelopKit开发板上自带有bmp280传感器:

image.png | left | 336x415

#软件设计

根据bmp280的数据手册,在读取气压值和温度值前,首先需要读取传感器的测量参数dig_T1-dig_T4,dig_P1-dig_P9,这些参数保存在对应的寄存器中,而气压值需要从寄存器0xF7中读取,这里读取的气压值仅仅是裸数据,最终的气压值计算需要结合测试参数进行计算;

/*DevelopKit开发板配置文件*/
{
  "I2C": [
    {
      "id":"bmp280",
      "port":2,
      "address_width":7,
      "freq":400000,
      "mode":1,
      "dev_addr":238
    }
  ]
}


/*bmp280.js*/
......
/*初始化和获取测量参数值*/
bmp280.init_config = function(){

    this.write_one(0xe0, 0xb6);
    this.write_one(0xf4, 0x27);
    this.write_one(0xf5, 0x20);
	this.dig_T1 = this.read_two(0x88);
	this.dig_T2 = this.read_two(0x8A);
	this.dig_T3 = this.read_two(0x8C);
	this.dig_P1 = this.read_two(0x8E);
	this.dig_P2 = this.read_two(0x90);
	this.dig_P3 = this.read_two(0x92);
	this.dig_P4 = this.read_two(0x94);
	this.dig_P5 = this.read_two(0x96);
	this.dig_P6 = this.read_two(0x98);
	this.dig_P7 = this.read_two(0x9A);
	this.dig_P8 = this.read_two(0x9C);
    this.dig_P9 = this.read_two(0x9E);
    
};
/*获取气压值(备注:这里计算的值还不准确,需要修正)*/
bmp280.getPressure = function(){

    if(0 == this.isInited){
        bmp280.init_config();
        this.isInited = 1;
    }
    var temp_value = this.read_three(this.lowTempReg);
    var press_value = this.read_three(this.lowPressReg);
	var v_x1_u32r = (temp_value/16384-(this.dig_T1)/1024)*(this.dig_T2);
	var v_x2_u32r = ((temp_value/131072-this.ig_T1/8192)*(temp_value/131072-this.dig_T1/8192))*this.dig_T3;
    this.t_fine = v_x2_u32r+v_x1_u32r;
    ......
    return comp_baro;
};

......


/*index.js*/
......
/*发送数据到云端*/
var postEvent = function(val) {
    var obj={};

    var id;
    var attrs = device.properties;
    obj['Pressure'] = val[0];
    var event = device.events[0];
    device.update(event, obj);
};
......

#运行验证

传感器数据在串口的打印:

image.png | left | 351x188

设备运行并接入LD一站式开发平台后,设备上传的数据将显示在界面上:

1.png | left | 683x198

#代码仓库

http://gitlab.alibaba-inc.com/Gravity/gravity_lite/tree/master/devices/bmp280

Clone this wiki locally