宜城龙山 发表于 2024-3-5 15:20

【安信可BW16-Kit开发板】BLE应用—温度测量与显示

<div class='showpostmsg'><p align="center"><a name="_Hlk160543378"></a><b>【安信可BW16-Kit开发板】BLE应用&mdash;温度测量与显示</b></p>

<p >&nbsp;</p>

<p align="left" >&nbsp;&nbsp; 在《<a href="https://bbs.eeworld.com.cn/thread-1272227-1-1.html" style="color:blue; text-decoration:underline">【安信可BW16-Kit开发板】DHT11应用</a>》和《<a href="https://bbs.eeworld.com.cn/thread-1272567-1-1.html" style="color:blue; text-decoration:underline">【安信可BW16-Kit开发板】WifiWebClient应用》</a>中分别介绍了利用安信可BW16-Kit开发板进行温度测试和wifi应用,本篇介绍一下安信可BW16-Kit开发板在ArduinoIDE下的BLE应用示例。有关BW16-Kit开发板基本性能和Arduino开发环境搭建不在赘述。</p>

<p ><b>一、BLE应用开发示例</b></p>

<ol>
        <li >按下图连接好线路,注意BW16-Kit开发板DHT11信号线连接A26引脚。</li>
</ol>

<p > &nbsp;</p>

<ol start="2">
        <li >ArduinoIDE环境下选择开发板为Ameba。</li>
        <li >选择&ldquo;Tools&rdquo; -&gt; &ldquo;Board&rdquo; -&gt; &ldquo;Arduino Ameba&rdquo;</li>
        <li >然后打开AmebaBLE下的DHT over BLEUart示例,&ldquo;Files&rdquo;-&gt;&ldquo;Examples&rdquo; -&gt;&ldquo;AmebaBLE&rdquo;-&gt;&ldquo;DHT_over_BLEUart&rdquo;。</li>
</ol>

<p align="left" ><br />
&nbsp; &nbsp;</p>

<ol start="5">
        <li >程序如下</li>
</ol>

<pre>
<code>/*

 Example

 */

#include "BLEDevice.h"

#include "DHT.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

#define DHTPIN 8

#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);



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) {

    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) {

        Serial.print("Notifications enabled on Characteristic");

        notify = true;

    } else {

        Serial.print("Notifications disabled on Characteristic");

        notify = false;

    }

    Serial.print(chr-&gt;getUUID().str());

    Serial.print(" for connection");

    Serial.println(connID);

}



void setup() {

    Serial.begin(115200);

    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();

    dht.begin();

}



void loop() {

    float h = dht.readHumidity();

    float t = dht.readTemperature();

    if (isnan(h) || isnan(t)) {

        Serial.println("Failed to read from DHT sensor!");

        return;

    }

    String msg = ("Humidity: " + String((int) h) + "%\t" + "Temperature: " + String((int) t) + "°C\n");

    Tx.writeString(msg);

    if (BLE.connected(0) &amp;&amp; notify) {

        Tx.notify(0);

    }

    delay(5000);

}</code></pre>

<p align="left" >&nbsp;</p>

<p ><b>二、运行</b></p>

<p >1、打开手机蓝牙客户端,搜索到蓝牙名为AMEBA_BLE_DEV,并配对;</p>

<p > &nbsp;</p>

<p >2、在列表中选择名为&ldquo;AMEBA_BLE_DEV&rdquo;,点击进入下面的界面后,选择&ldquo;UNknown Service&rdquo;选项;</p>

<p > &nbsp;</p>

<p >运行结果如下:</p>

<p align="center" > &nbsp;</p>

<p align="center" >&nbsp;</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>
页: [1]
查看完整版本: 【安信可BW16-Kit开发板】BLE应用—温度测量与显示