CSR101x BLE芯片 简简单单实现一个 温湿度BLE 传感器结点
CSR101x ble 结点
由于工作的性质,手头有许多Csr101X beacon开发板,想利用这些开发板做几个温度湿度的sensor ble 节点设备,进行 Ble 组网的测试。于是,开始动手,并把整个过程记录下来,希望对同行有个帮助。事情 并不复杂,代码量也不大。纯属个人消遣。
开发软件和硬件
IDE环境: csr uEngery SDK 2.6.3.12(xIDE) ,软件包里带了一些应用场景的例子。
演示软件:CSR uEnergy Profile Demonstrator 2.6.2.1
BLE demo板 :CSR1010 shelf Tag
温湿度传感器:SHT21 工业级温度湿度传感器(I2C)
硬件原理图和硬件连接
这里参考的是Csr1010 shelf tag 原理图,Csr1010 作为一个ble 结点方案的实现还是有很大的优势的。电路很简单,profile的参考例子也很多,不用购买license。适合初学者上手。
起初,想把 SHT21的传感器连接到I2c flash 的总线上,但是代码中并没有这样实现,为了方便起见,充分利用了 空闲的GPIO,用于连接 SHT21 传感器。
原理图参考如下,如果需要完整的原理图可与我单独联系。
PIO(0) SCL 用于连接 SHT21 的 SCL pin
PIO(1) SDA 用于连接 SHT21的 SDA pin
VDD_BAT 连接SHT21 的VDD
GND 连接SHT21 的GND
软件的修改
这里选择Environment sensor 工程的例子
在软件定义中 user_config.h 中定义了I2c 数据线的配置
/* I2C communication lines */
#define I2C_SCL_PIO (1)
#define I2C_SDA_PIO (0)
在user_config.h 配置文件中
定义#define INCLUDE_TEMPERATURE_SENSING 使能温度传感器功能,
使能SHT21 传感器的支持
定义 #define INCLUDE_HUMIDITY_SENSING 使能SHT21 对湿度的支持
在humidity_sensor.h 添加SHT21 湿度支持文件
添加温度读写的代码
在sht21_humidity_sensor.c 文件中添加如下代码
extern bool SHT21_ReadBlocking_temp(int16 *p_tempture)
{
int32 temperture_value = 0; /* Humidity value requires only 16bits but to
* avoid overflow while doing calculations, the
* value has been defined as uint32.
*/
uint8 temperture[2] = {0};
bool success = FALSE;
uint8 poll_wait = 0;
if(I2CAcquire())
{
/* Initialise I2C */
I2CcommsInit();
TimeDelayUSec(5 * MILLISECOND);
/* Write command for humidity measurement in NO Hold mode */
success = sht21_writeCommand(SHT21_I2C_ADDRESS,
SHT21_TRIGGER_T_MEASUREMENT_NO_HOLD_MODE);
if(success)
{
/* The sensor takes maximum 30ms for humidity measurement. */
TimeDelayUSec(5 * MILLISECOND);
do{
success = sht21_readTwoBytes(SHT21_I2C_ADDRESS, &temperture[0]);
poll_wait++;
TimeDelayUSec(2 * MILLISECOND);
}while((poll_wait < MAX_MEASUREMENT_TIME) && (!success));
if(success)
{
/* 4 LSBs should be converted to zeros. */
temperture_value= (((uint32 )temperture[0])<<8) |
((uint32 )(temperture[1] & 0xFC));
/* Conversion of Signal Output:
* Default resolution is set to 12 bit relative humidity and 14
* bit temperature reading. Measured data are transferred in
* two byte packages, i.e. in frames of 8 bit length where the
* most significant bit (MSB) is transferred first (left
* aligned).Each byte is followed by an acknowledge bit. The
* twostatus bits, the last bits of LSB, must be set to 0
* before calculating physical values.
*
* Relative Humidity Conversion
* With the relative humidity signal output SRH the relative
* humidity RH is obtained by the following formula (result in
* %RH), no matter which resolution is chosen:
*
* RH = -6 + (125*(SRH/2^16))
*
* T = -46.85 +(175.72 *(SRT/2^12))
*/
/* Since the application needs to send humidity percentage with
* a resolution of 0.01 percent, multiply by 100.
*/
temperture_value= (( temperture_value *17500)/65536) - (4685);
*p_tempture= (int16)temperture_value;
}
}
/* Release the I2C bus */
I2CRelease();
}
return(success);
}
软件和硬件修改完后,编译并且通过下载器下载到csr1010 tag 的开发板中
demo 板如下
DEMO的演示
我们使用CSR 软件包中提供 CSR uEnergy Profile Demonstrate 2.6.2.1软件
扫描,配对后,便可以获得BLE 节点 发送过来的温度和湿度的结果。
强调文本 强调文本
参考文档:
【1】 cs-331349-DD-1 cns12015 beacon board schematic.pdf