hollyedward 发表于 2024-3-8 14:35

【安信可BW16-Kit开发板】ble uart 发送测温数据

<div class='showpostmsg'><p><strong>一、板卡连接</strong></p>

<p><strong>&nbsp; &nbsp; &nbsp; </strong>使用的温度传感器是 D6T-44L-06H,板卡通过I2C总线读取传感器信息,再通过蓝牙传输数据</p>

<p>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;</p>

<p><strong>二、代码烧录</strong></p>

<pre>
<code class="language-cpp">* includes */
#include &lt;Wire.h&gt;
#include "BLEDevice.h"


#define UART_SERVICE_UUID      "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

#define STRING_BUF_SIZE 100
/* defines */
#define D6T_ADDR 0x0A// for I2C 7bit address
#define D6T_CMD 0x4C// for D6T-44L-06/06H, D6T-8L-09/09H, for D6T-1A-01/02

#define N_ROW 4
#define N_PIXEL (4 * 4)
#define N_READ ((N_PIXEL + 1) * 2 + 1)

BLEService UartService(UART_SERVICE_UUID);
BLECharacteristic Rx(CHARACTERISTIC_UUID_RX);
BLECharacteristic Tx(CHARACTERISTIC_UUID_TX);
BLEAdvertData advdata;
BLEAdvertData scndata;
bool notify = false;

void writeCB (BLECharacteristic* chr, uint8_t connID) {
    //printf("Characteristic %s write by connection %d :\n", chr-&gt;getUUID().str(), connID);
    Serial.print("Characteristic ");
    Serial.print(chr-&gt;getUUID().str());
    Serial.print(" write by connection ");
    Serial.println(connID);
    if (chr-&gt;getDataLen() &gt; 0) {
      Serial.print("Received string: ");
      Serial.print(chr-&gt;readString());
      Serial.println();
    }
}

void notifCB(BLECharacteristic* chr, uint8_t connID, uint16_t cccd) {
    if (cccd &amp; GATT_CLIENT_CHAR_CONFIG_NOTIFY) {
      //printf("Notifications enabled on Characteristic %s for connection %d \n", chr-&gt;getUUID().str(), connID);
      Serial.print("Notifications enabled on Characteristic");
      notify = true;
    } else {
      //printf("Notifications disabled on Characteristic %s for connection %d \n", chr-&gt;getUUID().str(), connID);
      Serial.print("Notifications disabled on Characteristic");
      notify = false;
    }
    Serial.print(chr-&gt;getUUID().str());
    Serial.print(" for connection");
    Serial.println(connID);
}


uint8_t rbuf;
double ptat;
double pix_data;

uint8_t calc_crc(uint8_t data) {
    int index;
    uint8_t temp;
    for (index = 0; index &lt; 8; index++) {
      temp = data;
      data &lt;&lt;= 1;
      if (temp &amp; 0x80) {data ^= 0x07;}
    }
    return data;
}

/** &lt;!-- D6T_checkPEC {{{ 1--&gt; D6T PEC(Packet Error Check) calculation.
* calculate the data sequence,
* from an I2C Read client address (8bit) to thermal data end.
*/
bool D6T_checkPEC(uint8_t buf[], int n) {
    int i;
    uint8_t crc = calc_crc((D6T_ADDR &lt;&lt; 1) | 1);// I2C Read address (8bit)
    for (i = 0; i &lt; n; i++) {
      crc = calc_crc(buf ^ crc);
    }
    bool ret = crc != buf;
    if (ret) {
      Serial.print("PEC check failed:");
      Serial.print(crc, HEX);
      Serial.print("(cal) vs ");
      Serial.print(buf, HEX);
      Serial.println("(get)");
    }
    return ret;
}

/** &lt;!-- conv8us_s16_le {{{1 --&gt; convert a 16bit data from the byte stream.
*/
int16_t conv8us_s16_le(uint8_t* buf, int n) {
    uint16_t ret;
    ret = (uint16_t)buf;
    ret += ((uint16_t)buf) &lt;&lt; 8;
    return (int16_t)ret;   // and convert negative.
}

/** &lt;!-- setup {{{1 --&gt;
* 1. Initialize
       - initialize a Serial port for output.
           - initialize I2C.
*/
void setup() {
    Serial.begin(115200);// Serial baudrate = 115200bps
    Wire.begin();// i2c master
    advdata.addFlags();
    advdata.addCompleteName("AMEBA_BLE_DEV");
    scndata.addCompleteServices(BLEUUID(UART_SERVICE_UUID));

    Rx.setWriteProperty(true);
    Rx.setWritePermissions(GATT_PERM_WRITE);
    Rx.setWriteCallback(writeCB);
    Rx.setBufferLen(STRING_BUF_SIZE);
    Tx.setReadProperty(true);
    Tx.setReadPermissions(GATT_PERM_READ);
    Tx.setNotifyProperty(true);
    Tx.setCCCDCallback(notifCB);
    Tx.setBufferLen(STRING_BUF_SIZE);

    UartService.addCharacteristic(Rx);
    UartService.addCharacteristic(Tx);

    BLE.init();
    BLE.configAdvert()-&gt;setAdvData(advdata);
    BLE.configAdvert()-&gt;setScanRspData(scndata);
    BLE.configServer(1);
    BLE.addService(UartService);

    BLE.beginPeripheral();
          delay(620);
}

/** &lt;!-- loop - Thermal sensor {{{1 --&gt;
* 2. read data.
*/
void loop() {
    int i = 0;
          int16_t itemp = 0;
       
        // Read data via I2C
        // I2C buffer of "Arduino MKR" is 256 buffer. (It is enough)
    memset(rbuf, 0, N_READ);
    Wire.beginTransmission(D6T_ADDR);// I2C slave address
    Wire.write(D6T_CMD);               // D6T register
    Wire.endTransmission();
          delay(1);
    Wire.requestFrom(D6T_ADDR, N_READ);
    while (Wire.available()) {
      rbuf = Wire.read();
    }
    D6T_checkPEC(rbuf, N_READ - 1);
   
    //Convert to temperature data (degC)
    ptat = (double)conv8us_s16_le(rbuf, 0) / 10.0;
          for (i = 0; i &lt; N_PIXEL; i++) {
                  itemp = conv8us_s16_le(rbuf, 2 + 2*i);
                  pix_data = (double)itemp / 10.0;
          }
   
    //Output results
    double sum ;
          for (i = 0; i &lt; N_PIXEL; i++) {
          sum += pix_data;
        }       
    double average = sum / N_PIXEL;
   
    String str = ("The average temporature: " + String(average) + "°C\n");
       
    delay(300);       

    Tx.writeString(str);
    if (BLE.connected(0) &amp;&amp; notify) {
      Tx.notify(0);
    }
    delay(5000);
}</code></pre>

<p><strong>三、实验结果</strong></p>

<p>使用手机上的 android 端蓝牙调试软件:<strong>serial bluetooth terminal</strong></p>

<p>在device里找到,开发板设置的蓝牙名称,连接</p>

<p> &nbsp;</p>

<p>连接后就进入到串口 terminal,传感器对着一台手机。</p>

<p>由于String buffer不是很大,我只对16个方位的温度值取平均值,做个功能展示</p>

<p> &nbsp;</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

Jacktang 发表于 2024-3-9 07:48

<p>结果看应该是发送测温数据成功</p>
页: [1]
查看完整版本: 【安信可BW16-Kit开发板】ble uart 发送测温数据