藍芽讀取溫濕度
# 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,並且已經登陸。
點選藍芽配對按鈕,在下拉選單中選擇已配對的藍芽設備,然後點選藍芽服務按鈕,在下拉選單中選擇藍芽服務,等待一段時間后連線建立,點選打開溫濕度計
,每隔五秒重新整理溫濕度數據。