OPCDA读取温湿度
# Smart OPCDA读取温湿度
# 1. 说明
采用OPC通讯协议,读取连接在Arduino上的DHT22温湿度传感器数据。OPC是标准的工业通讯界面,根据微软的OLE、COM、DCOM标准制定,它解决的是工业设备互通性的问题,让设备资料的存取不再受限于硬件制造商。OPC依据用途可分成OPC服务端(Server)与OPC用户端(Client)两部分。OPC Server与HMI/SCADA软件进行通信,协议OPC DA(资料存取),通过COM/DCOM技术达成工业自动化数据获取的架构,OPC Server提供了许多的方法,Client端通过这些方法可以获取与OPC Server相连的硬件的数据信息,而不需要去了解硬件的数据获取方式。开发者可以通过编写一套代码来实现操作不同的硬件。
范例中Arduino开发板与Ethernet W5100网络扩展板进行组装。温湿度传感器DHT22,负极接入Ethernet W5100扩展板的GND针脚,正极接入扩展板的5V针脚,信号线接入扩展板的D8针脚。
通过范例学习,可以掌握 OPCDA的配置方式,并结合Arduino开发板进行DHT22温湿度传感器读取的功能。
# 2. 零件连接图

# 3. 使用零件
序 | 零件名称 | 数量 |
---|---|---|
1 | Arduino UNO R3 开发板 | 1 |
2 | Arduino Ethernet W5100 网络扩展板 | 1 |
3 | USB数据线 | 1 |
4 | 面包板 | 1 |
5 | 杜邦线 | 3 |
6 | 网线 | 1 |
7 | DHT22温湿度传感器 | 1 |
# 4. Arduino流程图

# 5. Arduino程序
使用Arduino IDE 编译并上传以下Arduino程序。
// 采用 SoftwareTools4Makers/OPC 程序库
// https://github.com/SoftwareTools4Makers/OPC
// 使用温湿度传感器之连接库 https://github.com/adafruit/DHT-sensor-library
// 当 OPC 浏览器 呼叫 tmp 此 Item 会自动回传测得之温度
// 当 OPC 浏览器 呼叫 hum 此 Item 会自动回传测得之湿度
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include <DHT.h>
#define dhtPin 8 //读取DHT22 Data
#define dhtType DHT22 //选用DHT22
OPCEthernet aOPCEthernet; // 宣告 OPC 物件
DHT dht(dhtPin, dhtType); // 温湿度传感器初始化
// 设置网络IP地址 (网络扩展卡 MAC 可自行修改 +1 避免冲突)
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xAD, 0x8E };
IPAddress ip(192, 168, 0, 167); //IP地址
IPAddress gateway(192,168,0,1); //网关
IPAddress dns_server(192,168,0,1); //DNS服务器
IPAddress subnet(255,255,255,0); //子网掩码
const int listen_port = 80; // 设置 OPC Server 读取端口 listen port
void setup() {
Serial.begin(9600); //设定速率为9600
dht.begin();//启动DHT
aOPCEthernet.setup(listen_port,mac,ip); // 启动OPC
aOPCEthernet.addItem("tmp",opc_readwrite, opc_float, callbacktmp); // 在 OPC中加入 Item 并指定呼叫函数
aOPCEthernet.addItem("hum",opc_readwrite, opc_float, callbackhum);
}
// 当 OPC 浏览器呼叫此 Item (温度) 自动运行之副程序
float callbacktmp(const char *itemID, const opcOperation opcOP, const float value){
OPCItemType aOPCItem = aOPCEthernet.getOPCItem(itemID);
float t = dht.readTemperature(); //读取摄氏温度
if (isnan(t)) {
Serial.println("无法从DHT传感器读取!");
return;
}
// 判断 Item 是可读可写的状态
if (opcOP == opc_opread) {
if ((aOPCItem.opcAccessRight == opc_read) || (aOPCItem.opcAccessRight == opc_readwrite)) {
return t;
}
}
}
// 当 OPC 浏览器呼叫此 Item (湿度) 自动运行之副程序
float callbackhum(const char *itemID, const opcOperation opcOP, const float value){
OPCItemType aOPCItem = aOPCEthernet.getOPCItem(itemID);
float h = dht.readHumidity(); //读取湿度
if (isnan(h)) {
Serial.println("无法从DHT传感器读取!");
return;
}
//判断此 Item 是可读可写的状态
if (opcOP == opc_opread) {
if ((aOPCItem.opcAccessRight == opc_read) || (aOPCItem.opcAccessRight == opc_readwrite)) {
return h;
}
}
}
void loop() {
aOPCEthernet.processOPCCommands(); // 运行 OPC程序命令
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 6. 设计明细
设计模版前需要配置OPCDAServer服务器。
到https://opcfoundation.org/developer-tools/samples-and-tools-classic/core-components/ (opens new window)下载OPC核心组件(下载可能需要注册账号),根据操作系统类型选择安装(文件名中包含
OPC Core Components Redistributable
的msi文件,双击即可)。到https://www.st4makers.com/download/arduino-opc-server/ (opens new window)下载
Arduino OPC Server
。解压执行文件,注意执行文件的目录不能包含中文,否则可能会出现执行失败的情况。运行
ArduinoOPCServer.exe
,切换至Configuration
选项卡,点击Arduino Ethernet
添加一个设置,在其中修改对应的Arduino设备的IP地址及端口号。点击Save Configuration
。点击程序目录中新生成的
register.bat
进行注册,若在注册完毕后需要取消注册的,点击unregister.bat
。程序运行时需要保持注册的状态。开启Smart智慧控制平台,分别加入下插图之控件。或者通过点击菜单栏
[文件]-[打开项目]
选择范例项目文件来打开该范例。
①:TImage组件,控件名称为Image1
。
②:TShape组件,控件名称为Shape1
。
③:TPanel组件,控件名称为Panel1
。
④:TLabel组件,控件名称为Label9
。
⑤:TPanel组件,控件名称为Panel2
。
⑥:TLabel组件,控件名称为Label1
。
⑦:TLabel组件,控件名称为Label2
。
⑧:TLabel组件,控件名称为Label3
。
⑨:TWidgetLCDLabel组件,控件名称为WidgetLCDLabel1
。
⑩:TLabel组件,控件名称为Label4
。
(11):TTimer组件,控件名称为Timer1
。
(12):TSwitchButton组件,控件名称为SwitchButton1
。
(13):TLabel组件,控件名称为Label10
。
(14):TLabel组件,控件名称为Label5
。
(15):TLabel组件,控件名称为Label6
。
(16):TWidgetLCDLabel组件,控件名称为WidgetLCDLabel2
。
(17):TLabel组件,控件名称为Label7
。
(18):TLabel组件,控件名称为Label8
。
(19):TOPCDAServer组件,控件名称为OPCDAServer1
。
Main窗体属性设置
Caption
:主窗体标题,设置为OPCDA读取温湿度
。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)Timer1属性设置
Enabled
:启用计数器,设置为False
。Interval
:设置计数时间间隔(ms)=2000
。
(12)SwitchButton1属性设置
IsChecked
:开关状态,设置为False
。SwitchOff
:关闭状态时显示的图片。点击SwitchOff
属性右侧的[...]
按钮,打开文件上传界面,点击[Load...]
从文件浏览器中选择对应的图片文件上传,返回该界面下,待显示出图片后点击[OK]
加载图片。
SwitchOn
:打开状态时显示的图片。点击SwitchOn
属性右侧的[...]
按钮,打开文件上传界面,点击[Load...]
从文件浏览器中选择对应的图片文件上传,返回该界面下,待显示出图片后点击[OK]
加载图片。
Height
:设置控件高度=72
。Width
:设置控件宽度=68
。Stretch
:图像是否随控件大小拉伸,设置为True
。
(13)Label10属性设置
Caption
:设置标签文字,设置为关
。Font
:设置字体。设置内容如下图。
(14)Label5属性设置
Caption
:设置标签文字,设置为温度
。Font
:设置字体。设置内容如下图。
(15)Label6属性设置
Caption
:设置标签文字,设置为Humidity
。Font
:设置字体。设置内容如下图。
(16)WidgetLCDLabel2属性设置
Caption.Format
:显示格式,设置为00.00
。Height
:设置控件高度=60
。Width
:设置控件宽度=209
。
(17)Label7属性设置
Caption
:设置标签文字,设置为%
。Font
:设置字体。设置内容如下图。
(18)Label8属性设置
Caption
:设置标签文字,设置为RH
。Font
:设置字体。设置内容如下图。
(19)OPCDAServer1属性设置
- 双击
OPCDAServer1
,打开OPC Inspector
对话框。
- 选择
Server-Select
,打开Select OPC Server
对话框。
- 点击右侧的
Find
按钮,列表中会出现可选的OPC服务器,选择该服务器,点击[ok]
。
- 回到
OPC Inspector
界面,选择Server-Connect
,连接成功。接下来在该界面下新建Groups与Items。选择Groups-Add
,填写组名称,点击OK
保存。
- 选择
Items-Add
,在Select OPC Item
对话框中选择项目,此处的项目是在Arduino程序中进行定义的。选择对应选项,点击Open
按钮。
- 至此 OPCDAServer1配置完毕。
- 双击
# 7. 程序设计
# 7.1. 程序初始设置
程序启动时,启动OPCDAServer。
constructor TMyHandler.Create(AOwner: TComponent);
var
s :String;
begin
FThis :=TBaseForm(AOwner);
FThis.OPCDAServer1.Active := True;//打开OPCClient
end;
2
3
4
5
6
7
# 7.2. 事件设置
- (12)SwitchButton1-OnSwitch事件
SwitchButton1
在点击时会切换开-关状态,即改变IsChecked
属性的值,此时会触发OnSwitch
事件,打开或者关闭计时器。
procedure TMyHandler.SwitchButton1Switch;
//SwitchButton1的OnSwitch事件,开启或关闭定时器定时读取
begin
FThis.Timer1.Enabled := FThis.SwitchButton1.IsChecked;
end;
2
3
4
5
- (11)Timer1-OnTimer事件
定时器事件,每隔2秒读取数据解析并显示。
procedure TMyHandler.Timer1Timer;
//Timer1的OnTimer事件,定时读取温湿度
var
tmp: Variant;
hum: Variant;
begin
hum := FThis.OPCDAServer1.OPCGroups[0].OPCItems[0].Value;
tmp := FThis.OPCDAServer1.OPCGroups[0].OPCItems[1].Value;
FThis.WidgetLCDLabel1.Caption.Value := StrToFloat(VarToStr(tmp));
FThis.WidgetLCDLabel2.Caption.Value := StrToFloat(VarToStr(hum));
end;
2
3
4
5
6
7
8
9
10
11
# 8. 运行结果
通过工具栏保存,将程序保存为 sdb 项目文件。
使用鼠标点击工具栏运行(Run),测试运行结果。将图中的拨杆拨向“开”,数值显示屏中的温湿度数据每隔两秒更新一次。
