蓝牙读取温湿度
# PinToo之蓝牙读取温湿度
# 1. 说明
范例采用蓝牙通讯协议,读取驳接在Arduino上的温湿度传感器的数据。HC-06采用BC417143芯片,支持蓝牙2.1+EDR标准,HC-06的主控端或者从端模式是在出厂前就设定好的不能更改,通常能够买到的HC-06均为从端模式。
蓝牙设备(HC-06)通过杜邦线,将蓝牙VCC接口接至Arduino的5V针脚,蓝牙的GND接口接至Arduino GND针脚,蓝牙RXD接至Arduino 10号针脚,蓝牙TXD接至Arduino 11号针脚。
温湿度传感器DHT22,负极接至Arduino GND针脚,正极接至Arduino 5V针脚,信号线连接Arduino 的D8针脚。
在硬件连接完成后,将Arduino连接电源,需要先将手机端与HC-06的蓝牙配对,打开蓝牙选项,可搜索到HC-06
字样的蓝牙设备,默认的配对PIN码为1234。范例使用的Arduino程序使用到的蓝牙设备库的地址:http://drho.club/2018/06/arduino-bluetooth-mobile-phone01/ (opens new window),下载该库文件并将其添加至Arduino程序库。
通过范例学习,可以掌握蓝牙的基本通讯原理,并结合arduino开发板实现温湿度模块的数据采集功能。
# 2. 零件连接图

# 3. 使用零件
序 | 零件名称 | 数量 |
---|---|---|
1 | Arduino UNO R3 开发板 | 1 |
2 | HC-06蓝牙模块或HC-05蓝牙模块 | 1 |
3 | DHT22温湿度模块 | 1 |
4 | USB数据线 | 1 |
5 | 面包板 | 1 |
6 | 杜邦线 | 若干 |
# 4. Arduino流程图

# 5. Arduino程序
使用Arduino IDE 编译并上传以下Arduino程序。
// 使用温湿度传感器之链接库 https://github.com/adafruit/DHT-sensor-library
// http://drho.club/2018/06/arduino-bluetooth-read-sensor-value/
// https://home.gamer.com.tw/creationDetail.php?sn=3671289
#include <SoftwareSerial.h>
#include <DHT.h>
#include <Wire.h>
#define dhtPin 8 //读取DHT22 Data
#define dhtType DHT22 //选用DHT22
DHT dht(dhtPin, dhtType); // Initialize DHT sensor
SoftwareSerial BT(10,11);
void setup() {
BT.begin(9600);
Serial.begin(9600); //设定鲍率9600
dht.begin();//启动DHT
}
void loop() {
byte cmd[20];
int strsize;
while(true){
if ((strsize=(BT.available()))>0){
for (int i=0; i<strsize; i++){
cmd[i]=char(BT.read());
}
Serial.println(cmd[0]);
}
if (cmd[0]=='R') {
float h = dht.readHumidity(); //读取湿度
float t = dht.readTemperature(); //读取摄氏温度
char hum_buf[10], tem_buf[10];
if (!isnan(h) && !isnan(t)) {
dtostrf(h, 3, 2, hum_buf);
dtostrf(t, 3, 2, tem_buf);
Serial.print("湿度: ");
Serial.print(h);
Serial.print("%\t");
Serial.print("摄氏温度: ");
Serial.print(t);
Serial.print("*C\n");
//BT.write('s');
for(int i=0; i<5; i++) {
BT.write(hum_buf[i]);
}
//BT.write('w');
for(int i=0; i<5; i++) {
BT.write(tem_buf[i]);
}
}
else {
Serial.println("无法从DHT传感器读取!");
return;
}
}
delay(2000);//延时2秒
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 6. 设计明细
开启PinToo设计器,分别加入下插图之控件。或者点击左上角的[打开模板Lib文件]
选择模板文件来打开对应模板。

①:TfxSuperButton组件,控件名此为fxSuperButtonPaired
。
②:TfxWidgetLCDLabel组件,控件名称为fxWidgetLCDLabel1
。
③:TfxWidgetLCDLabel组件,控件名称为fxWidgetLCDLabel2
。
④:TfxBluetooth组件,控件名称为fxBluetooth1
。
⑤:TfxLabel组件,控件名称为fxLabelMsg
。
⑥:TfxSuperButton组件,控件名称为fxSuperButtonServices
。
⑦:TfxSwitch组件,控件名称为fxSwitch
。
fxRunFrame属性设置
Height
:设置页面高度=800
。Width
:设置页面宽度=400
。
fxTimer1属性设置
Interval
:设置计时器触发的时间间隔,单位为ms,设置为5000
。Enabled
:设置是否启用,设置为False
。
ComboBoxPaired属性设置
Name
:设置控件名称,设置为ComboBoxPaired
。Visible
:设置是是否可见,设置为False
。
ComboBoxServices属性设置
Name
:设置控件名称,设置为ComboBoxServices
。Visible
:设置是是否可见,设置为False
。
①fxSuperButtonPaired属性设置
Height
:设置控件高度=40
。Width
:设置控件宽度=40
。ButtonType
:设置按钮类型,设置为TfxSvgButton
。Name
:设置控件名称=fxSuperButtonPaired
。ButtonType.SvgData
:设置SVG数据,其数据内容如下。
<path d="M31.9325103759766,13.0669937133789 C32.3362045288086,15.0472469329834 32.5401840209961,17.3547077178955 32.5401840209961,20.0021266937256 C32.5401840209961,22.6452980041504 32.3362045288086,24.9570045471191 31.9325103759766,26.9372577667236 C31.52880859375,28.9175128936768 30.9763793945313,30.540807723999 30.2709770202637,31.8113994598389 C29.5698089599609,33.0819931030273 28.6774215698242,34.1103630065918 27.6065521240234,34.8965148925781 C26.5356826782227,35.6826667785645 25.3840789794922,36.2308502197266 24.1644859313965,36.5368118286133 C22.9448852539063,36.8470191955566 21.555305480957,37 20.0000038146973,37 C18.4446983337402,37 17.0551147460938,36.8427696228027 15.8355255126953,36.5368118286133 C14.6159210205078,36.2266006469727 13.4643173217773,35.6784172058105 12.3934478759766,34.8965148925781 C11.3183326721191,34.1103630065918 10.4301948547363,33.0862426757813 9.72903823852539,31.8113994598389 C9.02787017822266,30.540807723999 8.47118759155273,28.9132595062256 8.0674934387207,26.9372577667236 C7.66379165649414,24.957010269165 7.45981597900391,22.6495456695557 7.45981597900391,20.0021266937256 C7.45981597900391,17.3589553833008 7.66379165649414,15.0472440719604 8.0674934387207,13.0669918060303 C8.47118759155273,11.0867385864258 9.02362060546875,9.46344184875488 9.72903823852539,8.19285106658936 C10.4344444274902,6.92225933074951 11.322582244873,5.89388847351074 12.3934478759766,5.10773658752441 C13.4685668945313,4.32583475112915 14.6159210205078,3.77340388298035 15.8355255126953,3.46744155883789 C17.0551147460938,3.15723013877869 18.444694519043,3 20.0000038146973,3 C21.555305480957,3 22.9448852539063,3.15723013877869 24.1644859313965,3.46319222450256 C25.3840789794922,3.77340388298035 26.5356826782227,4.32158470153809 27.6065521240234,5.10348701477051 C28.6816673278809,5.88963890075684 29.5698089599609,6.91376113891602 30.2709770202637,8.18860149383545 C30.9721336364746,9.46344184875488 31.5245666503906,11.0867414474487 31.9325103759766,13.0669918060303 Z M18.8993873596191,34.607551574707 L27.7000427246094,25.8026523590088 L21.895263671875,19.9978809356689 L27.7000350952148,14.1931028366089 L18.8993873596191,5.38820266723633 L18.8993873596191,16.9807567596436 L14.0634918212891,12.1448593139648 L12.2999649047852,13.9083881378174 L18.3724594116211,19.9978809356689 L12.2999649047852,26.087366104126 L14.0634918212891,27.8508968353271 L18.8993873596191,23.0150012969971 L18.8993873596191,34.607551574707 Z M21.3640747070313,11.3842029571533 L24.1899719238281,14.2101001739502 L21.3810806274414,17.0190010070801 L21.3640747070313,11.3842029571533 Z M21.3853302001953,22.9767570495605 L24.1942291259766,25.7856521606445 L21.3683319091797,28.6115493774414 L21.3853302001953,22.9767570495605 Z" p-id="1966" fill="#FF1296DB" stroke="Null"></path>
1②fxWidgetLCDLabel1属性设置
Height
:设置控件高度=95
。Width
:设置控件宽度=285
。Color
:设置背景颜色=Black
。
③fxWidgetLCDLabel2属性设置
Height
:设置控件高度=95
。Width
:设置控件宽度=285
。Color
:设置背景颜色=Black
。
⑤fxLabelMsg属性设置
Height
:设置控件高度=30
。Width
:设置控件宽度=290
。Text
:设置标签文本=请选择配对设备
。Name
:设置控件名称=fxLabelMsg
。TextSettings.Font
:设置字体大小及颜色。Size
=18,Style
中将fsBold
勾选。使用该属性需将StyledSettings
中的所有设定为True
的选项的勾去除。
⑥fxSuperButtonServices属性设置
Height
:设置控件高度=40
。Width
:设置控件宽度=40
。ButtonType
:设置按钮类型,设置为TfxSvgButton
。Name
:设置控件名称=fxSuperButtonServices
。ButtonType.SvgData
:设置SVG数据,其数据内容如下。
<path d="M23.2725524902344,3.01327514648438 L16.9796981811523,3.01327514648438 L13.8332691192627,8.47858810424805 L16.9796981811523,13.943902015686 L23.2725524902344,13.943902015686 L26.4189777374268,8.47858810424805 L23.2725524902344,3.01327514648438 Z M22.2060432434082,12.0852537155151 L18.0506324768066,12.0852537155151 L15.9707164764404,8.47858810424805 L18.0506324768066,4.87192440032959 L22.2060432434082,4.87192440032959 L24.2815361022949,8.47858810424805 L22.2060432434082,12.0852537155151 Z M33.7517929077148,20.3562431335449 L27.4545116424561,20.3562431335449 L24.3080863952637,25.8215560913086 L27.4545116424561,31.2868709564209 L33.6721343994141,31.2868709564209 L36.0397033691406,27.3173294067383 L36.9026489257813,25.8215560913086 L33.7517929077148,20.3562431335449 Z M34.4421463012695,26.365873336792 L32.6189002990723,29.4237937927246 L28.525447845459,29.4237937927246 L26.449951171875,25.8171291351318 L28.525447845459,22.2104663848877 L32.6808547973633,22.2104663848877 L34.75634765625,25.8171291351318 L34.4421463012695,26.365873336792 Z M14.7271909713745,27.3129005432129 L15.5901355743408,25.8171291351318 L12.4437074661255,20.3518161773682 L6.14642715454102,20.3518161773682 L3,25.8171291351318 L6.14642715454102,31.2824420928955 L12.3640518188477,31.2824420928955 L14.7271909713745,27.3129005432129 Z M7.21736335754395,29.4237937927246 L5.14187145233154,25.8171291351318 L7.21736240386963,22.2104663848877 L11.3683462142944,22.2104663848877 L13.4438381195068,25.8171291351318 L13.1296377182007,26.365873336792 L11.3063907623291,29.4237937927246 L7.21736240386963,29.4237937927246 Z M23.4274387359619,29.6185111999512 C23.2858276367188,29.3751163482666 22.9981784820557,29.2644863128662 22.7282333374023,29.3485641479492 C21.8343124389648,29.6273612976074 20.8917102813721,29.7689743041992 19.9269847869873,29.7689743041992 C18.9755325317383,29.7689743041992 18.0506324768066,29.6317901611328 17.1699886322021,29.3662662506104 C16.904468536377,29.2866153717041 16.6212463378906,29.3928184509277 16.4796333312988,29.6317901611328 C16.2804908752441,29.9681186676025 16.4442310333252,30.4018001556396 16.8159599304199,30.5168609619141 C17.8160915374756,30.8222141265869 18.8604736328125,30.9771003723145 19.9269847869873,30.9771003723145 C21.0111961364746,30.9771003723145 22.0688571929932,30.8177871704102 23.0866889953613,30.4991588592529 C23.4539928436279,30.3796768188477 23.6221561431885,29.9548454284668 23.4274425506592,29.6185169219971 Z M26.972146987915,34.5837631225586 C26.8039817810059,34.3049621582031 26.4455280303955,34.212028503418 26.1623077392578,34.3624877929688 C24.2284278869629,35.2829666137695 22.1042575836182,35.7786102294922 19.9269828796387,35.7786102294922 C17.732006072998,35.7786102294922 15.5945606231689,35.2785415649414 13.6562557220459,34.3492202758789 C13.3686084747314,34.1722030639648 12.99245262146,34.2651290893555 12.8242893218994,34.5616302490234 L12.811014175415,34.5837631225586 C12.6517009735107,34.8625564575195 12.749059677124,35.212158203125 13.0234308242798,35.3847427368164 L13.0278568267822,35.3891754150391 L13.2402725219727,35.4953842163086 C15.3423166275024,36.4866638183594 17.5903968811035,36.9867248535156 19.9269847869873,36.9867248535156 C22.2237453460693,36.9867248535156 24.4408473968506,36.4999389648438 26.5119113922119,35.5396347045898 C26.5959930419922,35.4998016357422 26.6800746917725,35.4599761962891 26.7685813903809,35.4201507568359 C27.0518035888672,35.2431335449219 27.144739151001,34.8714065551758 26.972146987915,34.5837554931641 Z M26.9411716461182,12.2843942642212 C26.7951374053955,12.5233631134033 26.839391708374,12.8331384658813 27.0429534912109,13.027853012085 C27.7156105041504,13.6783809661865 28.2953281402588,14.435115814209 28.7599906921387,15.2847843170166 C29.2202301025391,16.1167507171631 29.5432796478271,16.9973964691162 29.7335681915283,17.9001693725586 C29.7911014556885,18.1701164245605 30.0212211608887,18.3692569732666 30.3000183105469,18.3781070709229 C30.6894454956055,18.3913822174072 30.9903717041016,18.037353515625 30.9107151031494,17.656774520874 C30.693868637085,16.630090713501 30.3265647888184,15.6388120651245 29.8132247924805,14.7050638198853 C29.291036605835,13.7536125183105 28.6405067443848,12.9039440155029 27.8704967498779,12.1604852676392 C27.5916996002197,11.8949632644653 27.1403121948242,11.9569177627563 26.9411716461182,12.2843942642212 Z M29.5698318481445,6.77925252914429 C29.4105186462402,7.06247520446777 29.4990253448486,7.42092943191528 29.7689723968506,7.59351778030396 C31.5081386566162,8.84589290618896 32.9640808105469,10.4744243621826 34.0128860473633,12.3817529678345 C35.0705528259277,14.3067817687988 35.6635475158691,16.4265270233154 35.7874565124512,18.5772495269775 C35.7697525024414,18.9135761260986 36.0352745056152,19.2012252807617 36.3760261535645,19.2056503295898 L36.4025764465332,19.2056503295898 C36.7256278991699,19.210075378418 36.9778785705566,18.9578304290771 37,18.6347808837891 L37,18.6303539276123 L36.9867286682129,18.3913860321045 C36.8406867980957,16.0680751800537 36.1945877075195,13.8509712219238 35.0661239624023,11.8020315170288 C33.9597854614258,9.78407001495361 32.4640121459961,8.07588291168213 30.6230621337891,6.71729946136475 C30.5478324890137,6.6597695350647 30.472599029541,6.60666561126709 30.3929424285889,6.55356121063232 C30.1097183227539,6.3898229598999 29.7379913330078,6.48718070983887 29.5698261260986,6.77925491333008 Z M9.61590576171875,18.3073024749756 C9.89470291137695,18.2763233184814 10.1115465164185,18.0550556182861 10.1469488143921,17.7718353271484 C10.2531566619873,16.8380832672119 10.4965515136719,15.9176111221313 10.8859834671021,15.0325403213501 C11.2665643692017,14.1607446670532 11.7622032165527,13.3641805648804 12.3596258163452,12.6649751663208 C12.5366401672363,12.4525585174561 12.5543413162231,12.1516332626343 12.3950281143188,11.9259405136108 C12.1693353652954,11.6073141098022 11.7046728134155,11.585186958313 11.4524278640747,11.8816862106323 C10.770923614502,12.6782503128052 10.2133293151855,13.572172164917 9.7840690612793,14.5501747131348 C9.35038471221924,15.545880317688 9.07158660888672,16.581413269043 8.95652675628662,17.643497467041 C8.9078483581543,18.0285053253174 9.23089981079102,18.3515548706055 9.61590385437012,18.307300567627 Z M3.74345946311951,19.5729541778564 C4.06208515167236,19.5286998748779 4.29220294952393,19.2410526275635 4.27007675170898,18.9180011749268 C4.20369625091553,16.7761325836182 4.60197830200195,14.6254081726074 5.47377300262451,12.6251468658447 C6.35441827774048,10.6116104125977 7.6643238067627,8.85031890869141 9.29285430908203,7.44305562973022 C9.57165145874023,7.24834060668945 9.63360786437988,6.86775970458984 9.4344654083252,6.59338808059692 L9.42119026184082,6.57126140594482 C9.23090171813965,6.31016540527344 8.87244701385498,6.26591205596924 8.60692596435547,6.44292593002319 L8.60249900817871,6.44735145568848 C8.5405445098877,6.50045585632324 8.4830150604248,6.54913520812988 8.42105960845947,6.60223913192749 C6.67304420471191,8.1334114074707 5.31446075439453,10.0009117126465 4.37628555297852,12.142783164978 C3.45581150054932,14.2492523193359 3.01327586174011,16.4796314239502 3.06195497512817,18.7675380706787 C3.06195497512817,18.8604717254639 3.06638073921204,18.9578304290771 3.07080554962158,19.0507621765137 C3.10620856285095,19.3826637268066 3.41155791282654,19.6172065734863 3.74345946311951,19.5729522705078 Z" p-id="4161" fill="#FF1296DB" stroke="Null"></path>
1
# 7. 程序设计
点击程序设计界面右下角的按钮,切换至单元选择界面,勾选需要使用的单元。该程序需要引用 BlueTooth
,BlueToothComponents
,ufxBlueTooth
单元。
# 7.1. 程序初始设置
在程序启动时,修改数值显示区的外观。
Begin
fxWidgetLCDLabel1.Caption.FillOff.Color := NULL;
fxWidgetLCDLabel2.Caption.FillOff.Color := NULL;
fxWidgetLCDLabel1.Caption.Format := '000.00';
fxWidgetLCDLabel2.Caption.Format := '000.00';
End.
2
3
4
5
6
在程序关闭前释放蓝牙连接。
Procedure fxRunFrameBeforeClose(Sender: TObject);
//关闭窗体前关闭蓝牙连接
Begin
fxBluetooth1.CloseServerSocket;
fxBluetooth1.FreeClientSocket;
End;
2
3
4
5
6
# 7.2. 事件设置
- ①fxSuperButtonPaired-OnClick事件 点击按钮加载蓝牙配对列表并显示。
Procedure fxSuperButtonPairedOnClick(Sender: TObject);
//加载已经配对蓝牙列表
Begin
fxBluetooth1.PairedDevices;
if fxBluetooth1.BTPaireds.Text = '' Then
begin
fxLabelMsg.Text := '请检查蓝牙是否配对';
Exit;
End;
ComboBoxPaired.Items := fxBluetooth1.BTPaireds;
ComboboxPaired.DropDown;
End;
2
3
4
5
6
7
8
9
10
11
12
- ComboBoxPaired-OnChange事件
当切换配对设备时更新。
Procedure ComboBoxPairedOnChange(Sender: TObject);
//切换配对设备
Begin
fxBluetooth1.PairedDevice := ComboBoxPaired.Items[ComboBoxPaired.ItemIndex];
fxLabelMsg.Text := '已选择设备' + ComboBoxPaired.Items[ComboBoxPaired.ItemIndex];
End;
2
3
4
5
6
- ⑥fxSuperButtonServices-OnClick事件
点击按钮,加载蓝牙服务列表。
Procedure fxSuperButtonServicesOnClick(Sender: TObject);
//加载蓝牙服务列表
begin
fxBluetooth1.Services;
ComboBoxServices.Items := fxBluetooth1.BTServices;
ComboBoxServices.DropDown;
End;
2
3
4
5
6
7
- ComboBoxServices-OnChange事件
当切换蓝牙服务时更新。
Procedure ComboBoxServicesOnChange(Sender: TObject);
//更改服务
Begin
fxBluetooth1.ServiceUUID := ComboBoxServices.Items[ComboBoxServices.ItemIndex];
fxBluetooth1.CreateServerSocket;
fxLabelMsg.Text := '设备已连接,已加载蓝牙服务';
End;
2
3
4
5
6
7
- ⑦fxSwitch-OnSwitch事件
点击以开启或者关闭温湿度的读取。
Procedure fxSwitchOnSwitch(Sender: TObject);
//开启关闭温湿度计读取
Begin
fxTimer1.Enabled := fxSwitch.IsChecked;
End;
2
3
4
5
- fxTimer1-OnTimer事件
定时发送读取数据的请求。
Procedure fxTimer1OnTimer(Sender: TObject);
begin
fxBluetooth1.SendData('R');//发送读取温湿度的命令
fxBluetooth1.ReadData;
End;
2
3
4
5
- ④fxBluetooth1-OnReceiveData事件
在接收到数据后解析并显示对应的温湿度。
Procedure fxBluetooth1OnReceiveData(Sender: TObject; ServiceName: string; ServiceUUID: string; ReceiveData: string);
//将获取到的数据显示在界面上
var
FData: String;
Begin
FData := Trim(ReceiveData);
if FData <> '' Then
begin
if length(FData)=10 Then
begin
fxWidgetLCDLabel1.Caption.Value := StrToFloat(Copy(FData,6,4)); //温度
fxWidgetLCDLabel2.Caption.Value := StrToFloat(Copy(FData,1,5)); //湿度
End
else
begin
//fxLabelMsg.Text := '返回数据:'+FData;
End;
end
else
begin
//fxLabelMsg.Text := '返回数据:无数据!';
end
End;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 8. 运行结果
使用鼠标在 PinToo 菜单,点击[保存至数据库]
按钮,将其保存至数据库,点击[调试运行]
确认能够正常打开。
通过同步中心,将程序上传至手机PinToo运行;同步时,请确保手机已经运行PinToo,并且已经登陆。


点击蓝牙配对按钮,在下拉列表中选择已配对的蓝牙设备,然后点击蓝牙服务按钮,在下拉列表中选择蓝牙服务,等待一段时间后连接建立,点击打开温湿度计
,每隔五秒刷新温湿度数据。