【cc2541历程】ds18b20发送数值到App

来源:互联网 发布:win10打不开任何软件 编辑:程序博客网 时间:2024/06/06 01:42

一、简介

本文介绍如何在SimpleBLEPeripheral工程中,采集ds18b20的温度值,通过char6,传送到手机APP端。


二、实验平台

编译环境:IAR820.02

协议栈:BLE-CC254x-1.40

代码查看器:Source Insight 3.5

硬件平台:SmartRf 开发板

安卓APP平台:SmartRf

三、编写声明

作者:爱已oО欠费

相关CC2541群:606444519

淘宝小店:https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4023-16306486995.5.hdKUUO&id=548576956486
四、实验前提
请完成以下博文:
【cc2541历程】ds18b20:http://blog.csdn.net/wangjiawu7/article/details/72550654
【cc2541历程】APPpwm调光:http://blog.csdn.net/wangjiawu7/article/details/72463244
四、相关电路
板子上DHT11和DS18B20公用一个接口

六、实验步骤

1.增加char6的宏定义(simpleGATTprofile.h中CONSTANTS段部分):

因为在pwm调光基础上修改而来,所以会比原协议栈多了charA的宏定义
代码如下:
// Profile Parameters#define SIMPLEPROFILE_CHAR1                   0  // RW uint8 - Profile Characteristic 1 value #define SIMPLEPROFILE_CHAR2                   1  // RW uint8 - Profile Characteristic 2 value#define SIMPLEPROFILE_CHAR3                   2  // RW uint8 - Profile Characteristic 3 value#define SIMPLEPROFILE_CHAR4                   3  // RW uint8 - Profile Characteristic 4 value#define SIMPLEPROFILE_CHAR5                   4  // RW uint8 - Profile Characteristic 5 value#define SIMPLEPROFILE_CHAR65  //RW uint8 - Profile Characteristic 6 value#define SIMPLEPROFILE_CHARA9  //RW uint8 - Profile Characteristic A value// Simple Profile Service UUID#define SIMPLEPROFILE_SERV_UUID               0xFFF0    // Key Pressed UUID#define SIMPLEPROFILE_CHAR1_UUID            0xFFF1#define SIMPLEPROFILE_CHAR2_UUID            0xFFF2#define SIMPLEPROFILE_CHAR3_UUID            0xFFF3#define SIMPLEPROFILE_CHAR4_UUID            0xFFF4#define SIMPLEPROFILE_CHAR5_UUID            0xFFF5#define SIMPLEPROFILE_CHAR6_UUID            0xFFF6#define SIMPLEPROFILE_CHARA_UUID            0xFFFA// Simple Keys Profile Services bit fields#define SIMPLEPROFILE_SERVICE               0x00000001// Length of Characteristic 5 in bytes#define SIMPLEPROFILE_CHAR5_LEN           5 #define SIMPLEPROFILE_CHAR6_LEN          4     // 温度传输#define SIMPLEPROFILE_CHARA_LEN           4     // pwm 输出


2.增加char6的UUID(simpleGATTprofile.c中GLOBAL VARIABLES段部分)
代码如下:

// Characteristic 6 UUID: 0xFFF6CONST uint8 simpleProfilechar6UUID[ATT_BT_UUID_SIZE] ={   LO_UINT16(SIMPLEPROFILE_CHAR6_UUID), HI_UINT16(SIMPLEPROFILE_CHAR6_UUID)};



3.增加char6的配置属性(simpleGATTprofile.c中Profile Attributes - variables段)
代码如下:

// Simple Profile Characteristic 6 Propertiesstatic uint8 simpleProfileChar6Props = GATT_PROP_READ | GATT_PROP_WRITE | GATT_PROP_NOTIFY;// Characteristic 6 Valuestatic uint8 simpleProfileChar6[SIMPLEPROFILE_CHAR6_LEN] = {0};static uint8 simpleProfileChar6Len = 0;// Simple Profile Characteristic 6 Configuration Each client has its own// instantiation of the Client Characteristic Configuration. Reads of the// Client Characteristic Configuration only shows the configuration for// that client and writes only affect the configuration of that client.static gattCharCfg_t simpleProfileChar6Config[GATT_MAX_NUM_CONN];// Simple Profile Characteristic 6 User Descriptionstatic uint8 simpleProfileChar6UserDesp[20] = "Current Temperature\0";


4.修改属性表:

1)修改属性表宏定义(simpleGATTprofile.c中CONSTANTS部分):
含有char6和charA的属性表宏定义(其中CharA 占了3个,char6占了4个)
#define SERVAPP_NUM_ATTR_SUPPORTED         24//20//17



2)修改属性表(simpleGATTprofile.c中simpleProfileAttrTbl部分):
    // Characteristic 6 Declaration    {       { ATT_BT_UUID_SIZE, characterUUID },      GATT_PERMIT_READ,       0,      &simpleProfileChar6Props     },      // Characteristic Value 6      {         { ATT_BT_UUID_SIZE, simpleProfilechar6UUID },        GATT_PERMIT_READ | GATT_PERMIT_WRITE,        0,         simpleProfileChar6       },      // Characteristic 6 configuration      {         { ATT_BT_UUID_SIZE, clientCharCfgUUID },        GATT_PERMIT_READ | GATT_PERMIT_WRITE,         0,         (uint8 *)simpleProfileChar6Config       },      // Characteristic 6 User Description      {         { ATT_BT_UUID_SIZE, charUserDescUUID },        GATT_PERMIT_READ,         0,         simpleProfileChar6UserDesp       },



5.修改特征值参数

1)增加char6数值可设置处理(simpleGATTprofile.c中SimpleProfile_SetParameter部分)
    case SIMPLEPROFILE_CHAR6:      //LCD_WRITE_STRING_VALUE( "SetParameter 6 len=", len, 10, HAL_LCD_LINE_1 );      //if ( len == SIMPLEPROFILE_CHAR6_LEN )       if ( len <= SIMPLEPROFILE_CHAR6_LEN )       {        VOID osal_memcpy( simpleProfileChar6, value, len );        simpleProfileChar6Len = len;        // See if Notification has been enabled        GATTServApp_ProcessCharCfg( simpleProfileChar6Config, simpleProfileChar6, FALSE,                                    simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),                                    INVALID_TASK_ID );      }      else      {        ret = bleInvalidRange;      }      break;


2)增加char6数值可获取处理(simpleGATTprofile.c中SimpleProfile_GetParameter部分)
    case SIMPLEPROFILE_CHAR6:      VOID osal_memcpy( value, simpleProfileChar6, simpleProfileChar6Len );      *returnBytes = simpleProfileChar6Len;      break;  


6.增修改特征值读写
1)增加char6数值读取处理(simpleGATTprofile.c中simpleProfile_ReadAttrCB部分)
case SIMPLEPROFILE_CHAR6_UUID:case SIMPLEPROFILE_CHARA_UUID:        {            uint8 newValue[20];            extern uint8 simpleProfileReadConfig(uint16 uuid, uint8 *newValue);                        *pLen = simpleProfileReadConfig(uuid, newValue);            VOID osal_memcpy( pValue, newValue, *pLen );        }        break;


2)增加char6数值写入处理(simpleGATTprofile.c中simpleProfile_WriteAttrCB部分)
  case SIMPLEPROFILE_CHAR6_UUID:        //Validate the value        // Make sure it's not a blob oper        //LCD_WRITE_STRING_VALUE( "WriteAttrCB 6 len=", len, 10, HAL_LCD_LINE_1 );        //LCD_WRITE_STRING_VALUE( "WriteAttrCB 6 len2=", simpleProfileChar6Len, 10, HAL_LCD_LINE_1 );        if ( offset == 0 )        {          //if ( len != SIMPLEPROFILE_CHAR6_LEN )          if ( len > SIMPLEPROFILE_CHAR6_LEN )          {            status = ATT_ERR_INVALID_VALUE_SIZE;          }        }        else        {          status = ATT_ERR_ATTR_NOT_LONG;        }        //Write the value        if ( status == SUCCESS )        {        //VOID osal_memcpy( pAttr->pValue, pValue, SIMPLEPROFILE_CHAR6_LEN );        VOID osal_memcpy( pAttr->pValue, pValue, len );            simpleProfileChar6Len = len;            notifyApp = SIMPLEPROFILE_CHAR6;        }                     break;


7.修改应用层
1)声明温度数值缓存
static uint8 TempValue[4];//温度值



2)增加应用层读取函数(simpleBLEPeripheral.c中simpleProfileReadConfig部分):
  case SIMPLEPROFILE_CHAR6_UUID:        {                      // 湿度          newValue[len++] = TempValue[0];          newValue[len++] = TempValue[1];            // 温度          newValue[len++] = TempValue[2];          newValue[len++] = TempValue[3];          break;        }

8.编译无误,下载,板子成功将ds18b20数值发送到手机APP端.





原创粉丝点击