爱招飞帮助手册 爱招飞帮助手册
首页
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 系统工具
    • 系统管理
    • 数据库工具
    • 专用模板
    • 外部功能
    • 开发流程
    • 函数代码
  • 开发手册
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 开发手册
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 开发手册
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • 报表设计
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • RestAPI
    • WebSocket
  • 学习手册

    • 安装配置
    • 快速上手
    • 程序架构
    • 界面介绍
    • 功能介绍
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • 预设资料
    • RestAPI说明
    • WebSocket说明
    • MQTT说明
    • Python说明
    • 自定程序
    • 运行卫士
    • 自动化作业
    • 函数程序
    • 控件使用
  • 开发手册
  • FastERP
  • FastWeb
  • Smart
  • PinToo
  • Flying
  • TARS
  • 通用功能

    • Report
    • Script
    • Echarts
    • Chart
    • DB Install
    • HiDesk
    • HiNAT
    • FastBPM
    • ReportBuilder
首页
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 系统工具
    • 系统管理
    • 数据库工具
    • 专用模板
    • 外部功能
    • 开发流程
    • 函数代码
  • 开发手册
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 开发手册
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 开发手册
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • 报表设计
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • RestAPI
    • WebSocket
  • 学习手册

    • 安装配置
    • 快速上手
    • 程序架构
    • 界面介绍
    • 功能介绍
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • 预设资料
    • RestAPI说明
    • WebSocket说明
    • MQTT说明
    • Python说明
    • 自定程序
    • 运行卫士
    • 自动化作业
    • 函数程序
    • 控件使用
  • 开发手册
  • FastERP
  • FastWeb
  • Smart
  • PinToo
  • Flying
  • TARS
  • 通用功能

    • Report
    • Script
    • Echarts
    • Chart
    • DB Install
    • HiDesk
    • HiNAT
    • FastBPM
    • ReportBuilder
  • Smart帮助主页
  • 学习手册

  • 开发手册

    • 通讯协议

      • 串口

      • 蓝牙Bluetooth

        • 蓝牙电子秤
        • 蓝牙控制LED灯
        • 蓝牙读取温湿度
          • 1. 说明
          • 2. 零件连接图
          • 3. 使用零件
          • 4. Arduino流程图
          • 5. Arduino程序
          • 6. 设计模板
          • 7. 程序设计
            • 7.1. 程序初始设置
            • 7.2. 事件设置
          • 8. 运行结果
      • 无线网WIFI

      • ModbusRTU

      • ModbusTCP

      • OPC

      • MQTT

      • RestAPI

      • HTTP

    • 交互输出

    • 媒体存取

    • 人工智慧

    • 实例运用

目录

蓝牙读取温湿度

# Smart之蓝牙读取温湿度

# 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连接电源,需要先将PC端与HC-06的蓝牙配对,需使用支持蓝牙功能的计算机(如不支持蓝牙的计算机也可以使用USB蓝牙适配器等外接拓展设备使之支持蓝牙功能),打开蓝牙选项,可搜索到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秒
  }
}
1
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. 设计模板

  开启Smart智慧控制平台,分别加入下插图之控件。或者通过点击菜单栏[文件]-[打开项目]选择范例项目文件来打开该范例。

蓝牙读取温湿度 UI布局

  ①:TImage组件,控件名称为Image1。

  ②:TShape组件,控件名称为Shape1。

  ③:TPanel组件,控件名称为Panel1。

  ④:TLabel组件,控件名称为Label9。

  ⑤:TPanel组件,控件名称为Panel2。

  ⑥:TLabel组件,控件名称为Label1。

  ⑦:TLabel组件,控件名称为Label2。

  ⑧:TLabel组件,控件名称为Label3。

  ⑨:TWidgetLCDLabel组件,控件名称为WidgetLCDLabel1。

  ⑩:TLabel组件,控件名称为Label4。

  (11):TBluetooth组件,控件名称为Bluetooth1。

  (12):TTimer组件,控件名称为Timer1。

  (13):TSwitchButton组件,控件名称为SwitchButton1。

  (14):TLabel组件,控件名称为Label10。

  (15):TLabel组件,控件名称为Label5。

  (16):TLabel组件,控件名称为Label6。

  (17):TImage组件,控件名称为ImageDevices。

  (18):TImage组件,控件名称为ImageServices。

  (19):TComboBox组件,控件名称为ComboBoxPaired。

  (20):TComboBox组件,控件名称为ComboBoxServices。

  (21):TWidgetLCDLabel组件,控件名称为WidgetLCDLabel2。

  (22):TLabel组件,控件名称为Label7。

  (23):TLabel组件,控件名称为Label8。

  • Main窗体属性设置

    • Caption:主窗体标题,设置为蓝牙读取温湿度。
    • ClientHeight:窗体客户区高度=480。
    • ClientWidth:窗体客户区宽度=720。
  • ①Image1属性设置

    • Stretch:设置图片拉伸=True。
    • Height:设置图片高度=480。
    • Width:设置图片宽度=720。
    • Picture:设置显示背景图片。点击Picture属性右侧的[...]按钮,打开文件上传界面,点击[Load...]从文件浏览器中选择对应的图片文件上传,返回该界面下,待显示出图片后点击[OK]加载图片。
  • ②Shape1属性设置

    • Height:设置控件高度=262。
    • Shape:设置形状=stRoundRect(圆角矩形)。
    • Width:设置控件宽度=544。
  • ③Panel1属性设置

    • BevelInner:设置内斜边样式=bvRaised。
    • BevelKind:设置斜边的样式=bkSoft。
    • BorderStyle:设置边界的样式=bsSingle。
    • Color:设置颜色=clBtnText。
    • Height:设置控件高度=217。
    • Width:设置控件宽度=500。
  • ④Label9属性设置

    • Caption:设置标签文字,设置为开。
    • Font:设置字体。设置内容如下图。
  • ⑤Panel2属性设置

    • BevelInner:设置内斜边样式=bvRaised。
    • BevelKind:设置斜边的样式=bkSoft。
    • BorderStyle:设置边界的样式=bsSingle。
    • Color:设置颜色=clMenuText。
    • Height:设置控件高度=83。
    • Width:设置控件宽度=404。
  • ⑥Label1属性设置

    • Caption:设置标签文字,设置为串口读取温湿度。
    • Font:设置字体。设置内容如下图。
  • ⑦Label2属性设置

    • Caption:设置标签文字,设置为温度。
    • Font:设置字体。设置内容如下图。
  • ⑧Label3属性设置

    • Caption:设置标签文字,设置为Temperature。
    • Font:设置字体。设置内容如下图。
  • ⑨WidgetLCDLabel1属性设置

    • Caption.Format:显示格式,设置为00.00。
    • Height:设置控件高度=60。
    • Width:设置控件宽度=209。
  • ⑩Label4属性设置

    • Caption:设置标签文字,设置为℃。
    • Font:设置字体。设置内容如下图。
  • (11)Bluetooth1属性设置

    • Bluetooth1无属性设置。
  • (12)Timer属性设置

    • Enabled:是否启用计时器的功能,此处设置为False。
    • Interval:计数器计时事件触发的时间间隔,以ms为单位,设置为2000。
  • (13)SwitchButton1属性设置

    • IsChecked:开关状态,设置为False。
    • SwitchOff:关闭状态时显示的图片。点击SwitchOff属性右侧的[...]按钮,打开文件上传界面,点击[Load...]从文件浏览器中选择对应的图片文件上传,返回该界面下,待显示出图片后点击[OK]加载图片。
    • SwitchOn:打开状态时显示的图片。点击SwitchOn属性右侧的[...]按钮,打开文件上传界面,点击[Load...]从文件浏览器中选择对应的图片文件上传,返回该界面下,待显示出图片后点击[OK]加载图片。
    • Height:设置控件高度=72。
    • Width:设置控件宽度=68。
    • Stretch:图像是否随控件大小拉伸,设置为True。
  • (14)Label10属性设置

    • Caption:设置标签文字,设置为关。
    • Font:设置字体。设置内容如下图。
  • (15)Label5属性设置

    • Caption:设置标签文字,设置为温度。
    • Font:设置字体。设置内容如下图。
  • (16)Label6属性设置

    • Caption:设置标签文字,设置为Humidity。
    • Font:设置字体。设置内容如下图。
  • (17)ImageDevices属性设置

    • Height:设置图片高度=31。
    • Width:设置图片宽度=31。
    • Name:控件名称=ImageDevices。
    • Stretch:设置图片拉伸=True。
    • Picture:设置图片。点击Picture属性右侧的[...]按钮,打开文件上传界面,点击[Load...]从文件浏览器中选择对应的图片文件上传,返回该界面下,待显示出图片后点击[OK]加载图片。
  • (18)ImageServices属性设置

    • Height:设置图片高度=31。
    • Width:设置图片宽度=31。
    • Name:控件名称=ImageServices。
    • Stretch:设置图片拉伸=True。
    • Picture:设置图片。点击Picture属性右侧的[...]按钮,打开文件上传界面,点击[Load...]从文件浏览器中选择对应的图片文件上传,返回该界面下,待显示出图片后点击[OK]加载图片。
  • (19)ComboBoxPaired属性设置

    • Height:设置控件高度=31。
    • Width:设置控件宽度=31。
    • Name:设置控件名称=ComboBoxPaired。
    • Font:设置字体,点击右侧的[...]按钮,打开字体设置界面。
  • (20)ComboBoxServices属性设置

    • Height:设置控件高度=31。
    • Width:设置控件宽度=31。
    • Name:设置控件名称=ComboBoxServices。
    • Font:设置字体,点击右侧的[...]按钮,打开字体设置界面。
  • (21)WidgetLCDLabel2属性设置

    • Caption.Format:显示格式,设置为00.00。
    • Height:设置控件高度=60。
    • Width:设置控件宽度=209。
  • (22)Label7属性设置

    • Caption:设置标签文字,设置为%。
    • Font:设置字体。设置内容如下图。
  • (23)Label8属性设置

    • Caption:设置标签文字,设置为RH。
    • Font:设置字体。设置内容如下图。

# 7. 程序设计

# 7.1. 程序初始设置

  该程序无初始设置。

# 7.2. 事件设置

  • (13)SwitchButton1-OnSwitch事件

SwitchButton1在点击时会切换开-关状态,即改变IsChecked属性的值,此时会触发OnSwitch事件,打开或者关闭计时器。

procedure TMyHandler.SwitchButton1Switch;
//开启读取
begin
  FThis.Timer1.Enabled := FThis.SwitchButton1.IsChecked;
end;
1
2
3
4
5
  • (12)Timer1-OnTimer事件

  定时器事件,每隔2秒读取数据。

procedure TMyHandler.Timer1Timer;
//读取温湿度并显示
var
 FData:String;
begin
  FThis.Bluetooth1.SendData('R');//发送读取温湿度的命令
  FThis.Bluetooth1.ReadData;  
End;
1
2
3
4
5
6
7
8
  • (17)ImageDevices-OnClick事件

ImageDevice是设备搜寻按钮,点击加载蓝牙设备列表。

procedure TMyHandler.ImageDevicesClick;
//点击加载配对设备列表
begin
  FThis.Bluetooth1.PairedDevices;
  FThis.ComboboxPaired.Items := FThis.Bluetooth1.BTPaireds;
  FThis.ComboBoxPaired.DroppedDown := True;
  FThis.ImageDevices.Enabled := False;
end;
1
2
3
4
5
6
7
8
  • (18)ImageServices-OnClick事件

ImageServices是服务搜寻按钮,点击以加载服务列表。

procedure TMyHandler.ImageSevicesClick;
//点击加载服务列表
begin
  FThis.Bluetooth1.Services;
  FThis.ComboBoxServices.Items := FThis.Bluetooth1.BTServices;
  FThis.ComboBoxServices.DroppedDown := True;
end;
1
2
3
4
5
6
7
  • (19)ComboBoxPaired-OnChange事件

  点击切换设备时触发事件,修改Bluetooth1的设备信息。

procedure TMyHandler.ComboBoxPairedChange;
//切换设备
begin
  FThis.Bluetooth1.PairedDevice := FThis.ComboBoxPaired.Items[FThis.ComboBoxPaired.ItemIndex];
end;
1
2
3
4
5
  • (20)ComboBoxServices-OnChange事件

  点击切换服务,并启动蓝牙服务。

procedure TMyHandler.ComboBoxServicesChange;
//切换服务
begin
  FThis.Bluetooth1.CloseServerSocket;
  FThis.Bluetooth1.FreeClientSocket;
  FThis.Bluetooth1.ServiceUUID := FThis.ComboBoxServices.Items[FThis.ComboBoxServices.ItemIndex];
  FThis.Bluetooth1.CreateServerSocket;
end;
1
2
3
4
5
6
7
8
  • (11)Bluetooth1-OnReceiveData事件

  读取蓝牙接收的数据解析并显示。

procedure TMyHandler.Bluetooth1ReceiveData;
//读取并显示数据
var
  FData: String;
begin
  FData := Trim(ReceiveData);
  if FData <> '' Then
  begin
    if length(FData)=10 Then
    begin
      //ShowMessage('湿度:'+Copy(FData,1,5)+' %, 温度:'+Copy(FData,6,4)+' ℃');
      FThis.WidgetLCDLabel1.Caption.Value := StrToFloat(Copy(FData,6,4));
      FThis.WidgetLCDLabel2.Caption.Value := StrToFLoat(Copy(FData,1,5));
    End
    else
    begin
      //ShowMessage('返回数据:'+FData);   
    End;
  end
  else
  begin
    //ShowMessage('返回数据:无数据!');   
  end        
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 8. 运行结果

  通过工具栏保存,将程序保存为 sdb 项目文件。

保存项目

  使用鼠标点击工具栏运行(Run),测试运行结果。首先点击蓝牙图标按钮,右侧下拉框显示可选的蓝牙设备,点击选择HC-06,再点击下方的服务图标按钮,右侧下拉框会显示处可选的服务,点击Dev B系列,此时关注HC-06模块的指示,待指示灯停止闪烁变成常亮状态则配对成功,此时将图中的拨杆拨向“开”,数值显示屏中的温湿度数据每隔两秒更新一次。

蓝牙读取温湿度 运行结果
蓝牙控制LED灯
WiFi控制LED灯

← 蓝牙控制LED灯 WiFi控制LED灯→

Copyright © 2021-2025 爱招飞IsoFace | ALL Rights Reserved
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式