查看: 12612|回复: 13

[应用教程] Arduino/Genuino 101 入门教程六:蓝牙接收数据

[复制链接]
简单的实验,我们用手机蓝牙发送数据,控制开发板13引脚连接的LED灯,发送0表示关灯,发送非0数据表示开灯。

1,下载必要的蓝牙软件:lightBLE
1.jpg


还有【产品链接】:  Arduino 101

2.在IDE中打开蓝牙驱动库中的实例LED:CurieBLE\examples\LED,下载到开发板!

  1. /*
  2.    Copyright (c) 2015 Intel Corporation.  All rights reserved.

  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Lesser General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2.1 of the License, or (at your option) any later version.

  7.    This library is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  10.    Lesser General Public License for more details.

  11.    You should have received a copy of the GNU Lesser General Public
  12.    License along with this library; if not, write to the Free Software
  13.    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  14. */

  15. /*
  16.    This sketch example partially implements the standard Bluetooth Low-Energy Heart Rate service.
  17.    For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
  18. */

  19. #include <CurieBle.h>

  20. BLEPeripheral blePeripheral;       // BLE Peripheral Device (the board you're programming)
  21. BLEService heartRateService("180D"); // BLE Heart Rate Service

  22. // BLE Heart Rate Measurement Characteristic"
  23. BLECharacteristic heartRateChar("2A37",  // standard 16-bit characteristic UUID
  24.     BLERead | BLENotify, 2);  // remote clients will be able to get notifications if this characteristic changes
  25.                               // the characteristic is 2 bytes long as the first field needs to be "Flags" as per BLE specifications
  26.                               // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml

  27. int oldHeartRate = 0;  // last heart rate reading from analog input
  28. long previousMillis = 0;  // last time the heart rate was checked, in ms

  29. void setup() {
  30.   Serial.begin(9600);    // initialize serial communication
  31.   pinMode(13, OUTPUT);   // initialize the LED on pin 13 to indicate when a central is connected

  32.   /* Set a local name for the BLE device
  33.      This name will appear in advertising packets
  34.      and can be used by remote devices to identify this BLE device
  35.      The name can be changed but maybe be truncated based on space left in advertisement packet */
  36.   blePeripheral.setLocalName("HeartRateSketch");
  37.   blePeripheral.setAdvertisedServiceUuid(heartRateService.uuid());  // add the service UUID
  38.   blePeripheral.addAttribute(heartRateService);   // Add the BLE Heart Rate service
  39.   blePeripheral.addAttribute(heartRateChar); // add the Heart Rate Measurement characteristic

  40.   /* Now activate the BLE device.  It will start continuously transmitting BLE
  41.      advertising packets and will be visible to remote BLE central devices
  42.      until it receives a new connection */
  43.   blePeripheral.begin();
  44.   Serial.println("Bluetooth device active, waiting for connections...");
  45. }

  46. void loop() {
  47.   // listen for BLE peripherals to connect:
  48.   BLECentral central = blePeripheral.central();

  49.   // if a central is connected to peripheral:
  50.   if (central) {
  51.     Serial.print("Connected to central: ");
  52.     // print the central's MAC address:
  53.     Serial.println(central.address());
  54.     // turn on the LED to indicate the connection:
  55.     digitalWrite(13, HIGH);

  56.     // check the heart rate measurement every 200ms
  57.     // as long as the central is still connected:
  58.     while (central.connected()) {
  59.       long currentMillis = millis();
  60.       // if 200ms have passed, check the heart rate measurement:
  61.       if (currentMillis - previousMillis >= 200) {
  62.         previousMillis = currentMillis;
  63.         updateHeartRate();
  64.       }
  65.     }
  66.     // when the central disconnects, turn off the LED:
  67.     digitalWrite(13, LOW);
  68.     Serial.print("Disconnected from central: ");
  69.     Serial.println(central.address());
  70.   }
  71. }

  72. void updateHeartRate() {
  73.   /* Read the current voltage level on the A0 analog input pin.
  74.      This is used here to simulate the heart rate's measurement.
  75.   */
  76.   int heartRateMeasurement = analogRead(A0);
  77.   int heartRate = map(heartRateMeasurement, 0, 1023, 0, 100);
  78.   if (heartRate != oldHeartRate) {      // if the heart rate has changed
  79.     Serial.print("Heart Rate is now: "); // print it
  80.     Serial.println(heartRate);
  81.     const unsigned char heartRateCharArray[2] = { 0, (char)heartRate };
  82.     heartRateChar.setValue(heartRateCharArray, 2);  // and update the heart rate measurement characteristic
  83.     oldHeartRate = heartRate;           // save the level for next comparison
  84.   }
  85. }
复制代码

3.打开手机蓝牙,打开软件lightBLE,会显示101的蓝牙设备名称,点击设备名连接即可:
2.jpg

4.出现下面的界面表示连接成功,点击最下面的设备(0x19B10001-E8F2-537E-XXXX):
3.jpeg

5.如果顺利会出现下面的界面,这里我们就可以向开发板发送数据了,点击Write new value:
4.jpeg

6.点击Write new value后可输入数字,然后点击右下角的Done即可发送数据,发送0关闭小灯,非0值打开小灯:
5.jpeg

7.我们在电脑IDE的串口中也能看到相关信息:
6.jpg


备注:如果蓝牙连接后界面和上面不一样,你需要忽略手机设置里的蓝牙连接
7.png

OK,蓝牙控制开发板的基本操作可以玩起来了!


看累了没,最后一个帖子来个脑经急转弯:
有个盲人,别人生病了都去看医生,为什么他生病了从来不去看医生,这是为什么呢?

Arduino/Genuino 101 入门教程】

  * DF创客社区版权所有,
欢迎转载
  转载请务必标注来源: DF创客社区+作者姓名+原文网址。


iooops  版主

发表于 2016-3-20 21:44:01

我问了下度娘,他告诉我盲人看不到医生。
回复 支持 反对

使用道具 举报

Jason_G  高级技师
 楼主|

发表于 2016-3-21 09:36:04

iooops 发表于 2016-3-20 21:44
我问了下度娘,他告诉我盲人看不到医生。

小妹妹,度娘说的是对的!
回复 支持 反对

使用道具 举报

凌风清羽  版主

发表于 2016-3-25 13:53:07

话说DF的BLE Arduino加上陀螺仪是不是就和101差不多啦,哈哈
回复 支持 反对

使用道具 举报

凌风清羽  版主

发表于 2016-3-27 20:40:41

好长的代码啊~~~~~~
回复 支持 反对

使用道具 举报

Jason_G  高级技师
 楼主|

发表于 2016-3-28 09:38:18

凌风清羽 发表于 2016-3-25 13:53
话说DF的BLE Arduino加上陀螺仪是不是就和101差不多啦,哈哈

那估计Inter就该改行玩Arduino了
回复 支持 反对

使用道具 举报

Jason_G  高级技师
 楼主|

发表于 2016-3-28 09:40:21


代码中的注释比较多
回复 支持 反对

使用道具 举报

lalahuo  学徒

发表于 2016-4-1 15:43:24

蓝牙还是连接不上
回复 支持 反对

使用道具 举报

Jason_G  高级技师
 楼主|

发表于 2016-4-2 16:33:48

lalahuo 发表于 2016-4-1 15:43
蓝牙还是连接不上

你用的什么手机?我用苹果的很正常,用蓝牙App连接蓝牙的时候,不要让手机设置里面的蓝牙自动连接到板子,如果连接了,请断开连接。
回复 支持 反对

使用道具 举报

lalahuo  学徒

发表于 2016-4-6 13:40:26

Jason_G 发表于 2016-4-2 16:33
你用的什么手机?我用苹果的很正常,用蓝牙App连接蓝牙的时候,不要让手机设置里面的蓝牙自动连接到板子 ...

能连接上了,是供电问题,我之前直接用usb供电调试的,后来用电源适配器12v供电就能连接上了
回复 支持 反对

使用道具 举报

Jason_G  高级技师
 楼主|

发表于 2016-4-6 23:37:18

lalahuo 发表于 2016-4-6 13:40
能连接上了,是供电问题,我之前直接用usb供电调试的,后来用电源适配器12v供电就能连接上了 ...

解决就好
回复 支持 反对

使用道具 举报

1661381542  见习技师

发表于 2016-7-8 21:53:51

written value为什么消失了呢............
完全无法向curie输入数据.........
求解
回复 支持 反对

使用道具 举报

张飞飞  学徒

发表于 2017-4-17 19:25:50

请问可以将数据发到电脑上的蓝牙吗?以串口的方式接收。我看了好多教程都是直接利用官网给的APP。
回复 支持 反对

使用道具 举报

mmc  见习技师

发表于 2017-8-21 11:37:51

代码贴错了。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
wifi气象站

硬件清单

btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2019 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail