|
|
| 第46行: |
第46行: |
| | | | |
| | ==文档== | | ==文档== |
| − | 支持AT指令配置/控制:[https://wiki.microduino.cn/images/a/ad/Microduino-BLE.pdf Microduino-BLE] | + | 支持AT指令配置/控制, |
| | + | 具体AT指令表见此文档附录:'''[[https://wiki.microduino.cn/images/a/ad/Microduino-BLE.pdf CC2541文档]]''' |
| | | | |
| | + | 注意: |
| | + | **AT+CLEAR |
| | + | **AT+TCON |
| | + | **AT+RSSI |
| | + | **AT+RADD |
| | + | 均会返回"ERROR",无法使用 |
| | | | |
| − | ===主要元件=== | + | ==开发== |
| − | *cc2541_datasheet:'''[[File:Cc2541.pdf]]''' | + | 编程手册:[[Protocol_Reference]] |
| | + | |
| | + | *'''[[【串口通信要求】]]''' |
| | + | |
| | + | ===蓝牙通信程序(通用)=== |
| | + | *请根据模块上的标识选择代码中的通信端口,示例程序中通信端口为D4,D5 |
| | + | *程序:可将如下程序直接复制到Microduino-IDE中并下载到模块上 |
| | + | <source lang="cpp"> |
| | + | #include <SoftwareSerial.h> |
| | | | |
| − | ===测试APP===
| + | String item; |
| − | *Microduino 安卓APP源码: https://github.com/iascchen/android-microduino
| |
| − | *Android APP BLE串口:'''[[File:mSerial.zip]]'''
| |
| − | *Android APP BLE彩灯:'''[[File:mLight.zip]]'''
| |
| − | *Android APP BLE遥控车:'''[[File:mTank.zip]]'''
| |
| | | | |
| | + | String currentInfo=""; |
| | | | |
| − | 如果玩家想参照github里的android代码开发出自己的app,要注意UUID要和Microduino-BT模块的UUID一致
| + | SoftwareSerial mySerial(4, 5); //根据实际模块上的跳线,选择对应端口 |
| | | | |
| − | [[file:Microduino-BT-UUID.png|800px|thumb|center|Microduino-BT-UUID]]
| + | #define my_Serial mySerial |
| | | | |
| − | ==开发==
| + | void setup() |
| − | ===串口通讯要求===
| |
| − | *默认的串口配置为:
| |
| − | **波特率 9600
| |
| − | **无校验
| |
| − | **数据位8
| |
| − | **停止位1
| |
| − | *对于Arduino IDE自带串口监视器:
| |
| − | **设置成:“\r\n”、“9600baud”
| |
| − | *对于其他串口调试软件:
| |
| − | **波特率 9600
| |
| − | **无校验
| |
| − | **数据位8
| |
| − | **停止位1
| |
| | | | |
| − | ===PC上位机调试时===
| + | { |
| − | *直接通过串口调试:
| + | item = ""; |
| − | **你需要准备'''[[Microduino-USBTTL/zh]]'''、不要直接将其与'''Microduino BLE模块'''叠加
| |
| − | ***因为他们对于Core同属从设备,串口的RX和TX未交差
| |
| − | **将'''[[Microduino-USBTTL/zh]]'''与'''Microduino BLE模块'''两个模块按下表相连。
| |
| − | {|class="wikitable"
| |
| − | |-
| |
| − | |Microduino-BLE||Microduino-USBTTL
| |
| − | |-
| |
| − | |D0||D1
| |
| − | |-
| |
| − | |D1||D0
| |
| − | |-
| |
| − | |3V3 ||3V3
| |
| − | |-
| |
| − | |GND ||GND
| |
| − | |-
| |
| − | |}
| |
| | | | |
| − | ===用CoreUSB下载、调试程序时===
| + | my_Serial.begin(9600); |
| − | *'''[[Microduino-CoreUSB/zh ]]'''的'''Serial'''为虚拟串口,'''Serial1'''才对应D0、D1
| |
| − | *所以可以利用以下程序调试
| |
| | | | |
| − | *程序
| |
| − | <source lang="cpp">
| |
| − | void setup() {
| |
| | Serial.begin(9600); | | Serial.begin(9600); |
| − | Serial1.begin(9600);
| |
| − | }
| |
| | | | |
| − | void loop() {
| |
| − | if (Serial.available()) {
| |
| − | char c = Serial.read();
| |
| − | Serial1.write(c);
| |
| − | }
| |
| − | if (Serial1.available()) {
| |
| − | char c = Serial1.read();
| |
| − | Serial.write(c);
| |
| − | }
| |
| | } | | } |
| − | </source>
| |
| | | | |
| − | ===与Android设备通信===
| + | void loop() |
| − | *系统要4.3以上的手机才能与microduino-BT模块通讯。
| |
| − | *下载Android的通讯软件,安装到手机上。
| |
| − | [[File:ble-serial.gif|200px|center]]
| |
| − | |-
| |
| − | |
| |
| − | 步骤一:将程序下载到Microduino里;
| |
| − | |-
| |
| − | |
| |
| − | [[File:ble-Download2.png|600px|center|thumb|Download]]
| |
| | | | |
| − | 步骤二:开始设置Android设备,打开Android设备的蓝牙功能,打开App,并在电脑IDE端打开串口监视器;
| + | { |
| | + | if (my_Serial.available() > 0) { |
| | | | |
| − | 步骤三:点击App右上方SCAN按钮,这是用来搜索周围蓝牙接入点的,点击SCAN后会显示周围的蓝牙设备。
| + | currentInfo = my_Serial.readStringUntil('\n'); |
| − | 点击对应的Microduino蓝牙编号,进入界面等待2-3秒钟,待屏右上角变成“Serial ready”字样,说明手机已经与蓝牙建立了连接.
| |
| − | [[File:202KIT-android-ready.jpg|600px|center|thumb|App—手机App]]
| |
| − | 手机向Microduino发送英文字符,串口监视器中收到手机发送的内容。同时手机接收到了Microduino发送的“^_^ Hello,Microduino!”信息,验证了蓝牙的双向通信功能。
| |
| − | [[File:microduino-android-system5.png|600px|center|thumb|App—串口监视器]]
| |
| − | [[File:202KIT-android-system6.jpg|600px|center|thumb|App—手机App]]
| |
| | | | |
| − | ===与IOS设备通信===
| + | Serial.println(currentInfo); |
| − | *iPhone4s以上、iPod touch 5以上、iPad 3以上、iPad mini以上;
| + | } |
| − | *前往App Store里下载LightBlue;
| + | if (Serial.available() > 0) { |
| | | | |
| − | [[File:LightBlue.jpg|400px|center|thumb|LightBlue]]
| + | item = Serial.readString(); |
| − | 步骤一:将程序下载到microduino里;
| |
| | | | |
| − | 步骤二:安装“lightblue”,打开软件,开始设置IOS设备,打开IOS设备的蓝牙功能。并在电脑端IDE中打开串口监视器
| + | my_Serial.println(item); |
| − | [[File:LightBlue_on_ble.jpg|400px|center|thumb]]
| |
| − | 步骤三:打开LightBlue;进入的界面是蓝牙设备搜索界面,从“Peripherals Nearby”下的列表中找到Microduino的蓝牙设备,点击该条目使手机与其建立连接;
| |
| − | |-
| |
| − | |
| |
| − | [[File:ble-Connection.jpg|400px|center|thumb|Connection1]]
| |
| − | 连接后进入页面如下:
| |
| − | [[File:ble-Connection1.jpg|400px|center|thumb|Connection2]]
| |
| | | | |
| − | 步骤四:选择并点击Characteristic6,观察屏幕右上方的编码格式,默认为Hex 16进制编码,如果要显示字符串请点击Hex所在的按钮并选择UTF-8编码格式,之后点击“Listen for notifications”使手机进入监听状态。
| + | Serial.println(item); |
| − | [[File:ble-Connection5.jpg|400px|center|thumb]]
| + | } |
| | + | } |
| | + | </source> |
| | | | |
| − | 步骤六:点击“Write new value”,弹出文本编辑界面
| + | ===与IOS设备通信=== |
| − | [[File:ble-Connection2.jpg|400px|center|thumb]] | + | *'''[[【使用LightBlue通信】]]''' |
| − | 自定义输入一个英文和数字组成的字符串,观察手机和串口的显示结果
| + | *'''[[【使用mRobot通信制作小车】]]''' |
| − | | + | *'''[[【使用mDock通信制作多样应用】]]''' |
| − | [[File:ble-Connection3.jpg|400px|center|thumb]] | |
| − | [[File:ble-Connection4.jpg|400px|center|thumb]] | |
| − | 可以从图中看到串口收到了手机发送的数据“12345”,手机端也收到了蓝牙返回的数据“bluetooth respond”,说明蓝牙双向通信是畅通的。
| |
| | | | |
| | ===与MAC设备通信=== | | ===与MAC设备通信=== |
| − | mac蓝牙无法直接与BT搜索连接,需要借助Light Blue来开发。下载
| + | *'''[[【与MAC设备通信的方法】]]''' |
| − | *将Microduino-Core与BT模块连接到电脑,下载同样的代码。 | |
| − | *打开Arduino串口监视器,再打开Light Blue软件,可以发现识到Microduino设备。
| |
| − | [[File:ble-Connection5.png|400px|center]]
| |
| − | *点击Microduino可以和蓝牙连接,连接成功后可以看到串口监视器打印出Connected。同时也可以看到蓝牙指示灯微闪(频率变快,亮度变低)。
| |
| − | [[File:ble-Connection6.png|400px|center]]
| |
| − | *按下图选择,然后向BT模块发送信息,例如:mCookie。
| |
| − | [[File:ble-Connection7.png|800px|center]]
| |
| − | *可以看到串口监视器打印出Microduino。
| |
| − | [[File:ble-Connection8.png|400px|center]] | |
| | | | |
| | ==扩展== | | ==扩展== |
| − | ===使用AT查看或更改BT的参数===
| + | *'''[[【使用AT指令修改蓝牙设置】]]''' |
| − | *AT指令配置/控制文档:'''[[File:Microduino-BLE.pdf]]''' | + | *'''[[【修改跳线以更改串口引脚】]]''' |
| − | *程序 | |
| − | <source lang="cpp">
| |
| − | //使用其他软串口用SoftwareSerial
| |
| − | //#include <SoftwareSerial.h>
| |
| − | //SoftwareSerial mySerial(4, 5); // RX, TX
| |
| | | | |
| − | //#define my_Serial mySerial | + | ===测试APP=== |
| − | #define my_Serial Serial1 //定义CoreUSB与BT串口
| + | *Microduino 安卓APP源码: https://github.com/iascchen/android-microduino |
| | + | *Android APP BLE串口:'''[[File:mSerial.zip]]''' |
| | + | *Android APP BLE彩灯:'''[[File:mLight.zip]]''' |
| | + | *Android APP BLE遥控车:'''[[File:mTank.zip]]''' |
| | | | |
| − | void setup()
| |
| − | {
| |
| − | Serial.begin(9600);//串口监视器通讯波特率
| |
| − | my_Serial.begin(9600);//BT通讯波特率
| |
| − | }
| |
| | | | |
| − | void loop()
| + | 如果玩家想参照github里的android代码开发出自己的app,要注意UUID要和Microduino-BT模块的UUID一致 |
| − | {
| |
| − | if (Serial.available())//监视到串口监视器的数据
| |
| − | my_Serial.write(Serial.read());//将数据写入BT
| |
| − | if (my_Serial.available())//监视到BT串口的数据
| |
| − | Serial.write(my_Serial.read());//将数据在串口监视器打印出来
| |
| − | }
| |
| − | </source>
| |
| − | *下载程序
| |
| − | **将mCookie-BT与mCookie-CoreUSB两个模块叠在一起,将USB线插入mCookie-CoreUSB的插孔,另一端连接电脑USB插口;
| |
| − | **启动Arduino IED,将上面程序复制到IDE中;
| |
| − | **在工具(tools)->板卡(Board)中选择Microduino CoreUSB,并在工具(tools)->串口(Serial)中选择对应的串口号;
| |
| − | **点击IDE左上角的编译(√)按钮对程序进行编译,编译结束后点击下载(->)按钮将程序烧录到板子中;
| |
| − | *打开串口监视器,设置成:“\r\n”、“9600baud”。
| |
| − | [[File:ble-Serial.jpg|600px|center|thumb|Serial]]
| |
| − | *在串口监视器中输入指定指令,可以看到返回结果,参考文档可更改BT的参数。
| |
| − | [[File:ble-AT.jpg|600px|center|thumb|AT]]
| |
| | | | |
| − | 使用软串口通讯程序:
| + | [[file:Microduino-BT-UUID.png|800px|thumb|center|Microduino-BT-UUID]] |
| − | <source lang="cpp">
| |
| − | //使用其他软串口用SoftwareSerial
| |
| − | #include <SoftwareSerial.h>
| |
| − | SoftwareSerial mySerial(4, 5);//根据选择的串口,选择对应的端口号(2,3)或(4,5)
| |
| − | | |
| − | #define my_Serial mySerial
| |
| − | | |
| − | String msg = "";
| |
| − | | |
| − | void setup()
| |
| − | {
| |
| − | // 初始化蓝牙通信波特率
| |
| − | my_Serial.begin(9600);
| |
| − | // 初始化串口监视器通信波特率
| |
| − | Serial.begin(9600);
| |
| − | }
| |
| − | void loop()
| |
| − | {
| |
| − | //每收到一次信号,向通信另一端反馈一次
| |
| − | if (my_Serial.available() > 0) //如果串口有数据输入
| |
| − | {
| |
| − | msg = my_Serial.readStringUntil('\n'); //获取换行符前所有的内容
| |
| − | Serial.println(msg); //在串口监视器显示收到的msg中的字符串
| |
| − | my_Serial.println("bluetooth respond"); //向蓝牙通信的另一端发送数据
| |
| − | }
| |
| − | }
| |
| − | </source>
| |
| | | | |
| | ===引脚说明=== | | ===引脚说明=== |
| 第259行: |
第144行: |
| | |} | | |} |
| | | | |
| − | ==应用==
| |
| − | ===程序下载===
| |
| − | BT模块发送示例:
| |
| − | <source lang="cpp">
| |
| − |
| |
| − | #include <SoftwareSerial.h>
| |
| − |
| |
| − | SoftwareSerial mySerial(4, 5); //RX,TX
| |
| − |
| |
| − | #define my_Serial mySerial //core
| |
| − | //#define my_Serial Serial1 //Core+
| |
| − |
| |
| − | String currentInfo="";
| |
| − |
| |
| − | void setup() {
| |
| − | Serial.begin(9600);
| |
| − |
| |
| − | my_Serial.begin(9600);
| |
| − |
| |
| − | delay(200); // a 2 seconds delay while we position the solar panel
| |
| − | }
| |
| − |
| |
| − | void loop() {
| |
| − |
| |
| − | my_Serial.println("111");
| |
| − | delay(500);
| |
| − | my_Serial.println("222");
| |
| − | delay(500);
| |
| − | my_Serial.println("333");
| |
| − | delay(500);
| |
| − |
| |
| − | }
| |
| − |
| |
| − | </source>
| |
| − |
| |
| − |
| |
| − | BT模块接收示例:
| |
| − |
| |
| − | <source lang="cpp">
| |
| − |
| |
| − | #include <SoftwareSerial.h>
| |
| − |
| |
| − | SoftwareSerial mySerial(4, 5); //RX,TX
| |
| − |
| |
| − | #define my_Serial mySerial //core
| |
| − | //#define my_Serial Serial1 //Core+
| |
| − |
| |
| − | String currentInfo="";
| |
| − |
| |
| − | void setup() {
| |
| − | Serial.begin(9600);
| |
| − |
| |
| − | my_Serial.begin(9600);
| |
| − |
| |
| − | delay(200); // a 2 seconds delay while we position the solar panel
| |
| − | }
| |
| − |
| |
| − | void loop() {
| |
| − |
| |
| − |
| |
| − | if (my_Serial.available() > 0) {
| |
| − | currentInfo = my_Serial.readStringUntil('\n');
| |
| − | Serial.println(currentInfo);
| |
| − | if(currentInfo=="ERROR"||currentInfo=="Connected") {
| |
| − | return;
| |
| − | }
| |
| − |
| |
| − | }
| |
| − |
| |
| − | }
| |
| − |
| |
| − | </source>
| |
| | | | |
| | ===通过CoreUSB对Shield BT4.0进行串口调试=== | | ===通过CoreUSB对Shield BT4.0进行串口调试=== |