查看: 4025|回复: 0

【Curie Nano】Curie Nano BLE初学(一)

[复制链接]
  • TA的每日心情
    奋斗
    2023-6-27 14:09
  • 签到天数: 943 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2017-5-21 11:47:13 | 显示全部楼层 |阅读模式
    分享到:
           之前没有自己开发过蓝牙,就用过HC-05串口蓝牙模块,学习蓝牙必须从头开始了,Curie Nano内部集成了低功耗蓝牙,方便实现互连。如果不学习如何使用Curie自带的蓝牙的话,感觉就是一种资源浪费。依葫芦画瓢,现在对于蓝牙的协议不了解,就先看自带的例子去学习。好在例子里有注释,这个例子通过手机蓝牙可以控制Curie Nano上的LED灯的亮灭,同时,也可以通过按钮的电平变化去控制LED。
           和蓝牙设置有关的部分在setup函数中,自己可以在loop函数中稍微改动一下就可以实现一些其他IO的控制等
    1. #include <CurieBLE.h>

    2. const int ledPin = 13; // set ledPin to on-board LED
    3. const int buttonPin = 4; // set buttonPin to digital pin 4

    4. BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service


    5. // create switch characteristic and allow remote device to read and write
    6. BLECharCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
    7. // create button characteristic and allow remote device to get notifications
    8. BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // allows remote device to get notifications

    9. void setup() {
    10.   Serial.begin(9600);
    11.   pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
    12.   pinMode(buttonPin, INPUT); // use button pin 4 as an input

    13.   // begin initialization
    14.   BLE.begin();

    15.   // set the local name peripheral advertises
    16.   BLE.setLocalName("BtnLED");
    17.   // set the UUID for the service this peripheral advertises:
    18.   BLE.setAdvertisedService(ledService);

    19.   // add the characteristics to the service
    20.   ledService.addCharacteristic(ledCharacteristic);
    21.   ledService.addCharacteristic(buttonCharacteristic);

    22.   // add the service
    23.   BLE.addService(ledService);

    24.   ledCharacteristic.setValue(0);
    25.   buttonCharacteristic.setValue(0);

    26.   // start advertising
    27.   BLE.advertise();

    28.   Serial.println("Bluetooth device active, waiting for connections...");
    29. }

    30. void loop() {
    31.   // poll for BLE events
    32.   BLE.poll();

    33.   // read the current button pin state
    34.   char buttonValue = digitalRead(buttonPin);

    35.   // has the value changed since the last read
    36.   boolean buttonChanged = (buttonCharacteristic.value() != buttonValue);

    37.   if (buttonChanged) {
    38.     // button state changed, update characteristics
    39.     ledCharacteristic.setValue(buttonValue);
    40.     buttonCharacteristic.setValue(buttonValue);
    41.   }

    42.   if (ledCharacteristic.written() || buttonChanged) {
    43.     // update LED, either central has written to characteristic or button state has changed
    44.     if (ledCharacteristic.value()) {
    45.       Serial.println("LED on");
    46.       digitalWrite(ledPin, HIGH);
    47.     } else {
    48.       Serial.println("LED off");
    49.       digitalWrite(ledPin, LOW);
    50.     }
    51.   }
    52. }
    复制代码
    将代码编译烧写后,打开手机调试软件,打开蓝牙,连接成功后,通过写入0和非零的数就可以控制LED的亮灭,通过也可以查看按钮对应引脚的高低电平。点击向上的箭头后弹出发送数值的界面,写入0或1就可以看到LED对应的变化


    InkedScreenshot_2017-05-21-11-31-06_LI.jpg


    上传一下调试软件,方便下载













    no.nordicsemi.android.mcp_4.10.0_79.zip

    6.14 MB, 下载次数: 0

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 07:04 , Processed in 0.128854 second(s), 19 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.