EdgeX实现温湿度数据读取

在linux环境中进行测试

​ 起初在win环境下可以通过使用商家提供的软件进行连接获取数据,但是在linux环境下不能连接。

​ 之后通过设置树莓派eth0为静态ip,再ping 192.168.0.88 则可以发现该设备。

​ 静态ip配置如下:

1
2
3
4
interface eth0
static ip_address=192.168.0.2/24
static routers=192.168.0.1
static domain_name_servers=114.114.114.114

利用java编写master端代码,测试连接

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
import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class ModBusConnection {
public final static TcpParameters tcpParameters = new TcpParameters();
public static ModbusMaster master;
static {
try {
InetAddress adress = InetAddress.getByName("192.168.0.88");
// TCP参数设置ip地址
tcpParameters.setHost(adress);
// TCP设置长连接
tcpParameters.setKeepAlive(true);
// TCP设置端口,这里设置是默认端口502
tcpParameters.setPort(502);
// 创建一个主机
master = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
Modbus.setAutoIncrementTransactionId(true);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}

/**
* 获取ModbusMaster
* @return
*/
public static ModbusMaster getModbusMaster(){
return master;
}

/**
* 关闭ModbusMaster
*/
public static void destoryModbusMaster(){
if(master.isConnected()){
try {
master.disconnect();
} catch (ModbusIOException e) {
e.printStackTrace();
}
}
}
}
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

import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class ModBusConnection {
public final static TcpParameters tcpParameters = new TcpParameters();
public static ModbusMaster master;
static {
try {
InetAddress adress = InetAddress.getByName("192.168.0.88");
// TCP参数设置ip地址
tcpParameters.setHost(adress);
// TCP设置长连接
tcpParameters.setKeepAlive(true);
// TCP设置端口,这里设置是默认端口502
tcpParameters.setPort(502);
// 创建一个主机
master = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
Modbus.setAutoIncrementTransactionId(true);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/**
* 获取ModbusMaster
* @return
*/
public static ModbusMaster getModbusMaster(){
return master;
}

/**
* 关闭ModbusMaster
*/
public static void destoryModbusMaster(){
if(master.isConnected()){
try {
master.disconnect();
} catch (ModbusIOException e) {
e.printStackTrace();
}
}
}
}

输出如下:

这是两个十六进制数,转为为十进制表示:101H=257D,也就是表示25.7摄氏度 248H=584D,也就表示湿度为58.4%

利用EdgeX获取数据

针对温湿度设备进行kuiper规则引擎测试

首先测试是否能获取温度数据:

1
2
#get
192.168.137.100:30082/api/v2/device/name/TCP-5900/Temperature

第一步:创建流

post http://192.168.137.100:30720/streams

body {"sql": "create stream demo() WITH (FORMAT=\"JSON\", TYPE=\"edgex\")"}

第二步:创建规则

监视来自TCP-5900设备的事件,如果Temperature读取值的平均值(20 秒内)大于 20,则向Random-Boolean-Device设备发送命令以停止生成随机数字(将随机生成 bool 设置为 false)。

1
2
#post
http://192.168.137.100:30720/rules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"id": "rule1",
"sql": "SELECT avg(temperature) AS avg_temperature FROM demo WHERE temperature != nil and meta(deviceName)= \"TCP-5900\" GROUP BY TUMBLINGWINDOW(ss, 5) HAVING avg(temperature) > 20.0",
"actions": [
{
"rest": {
"url": "http://edgex-core-command:59882/api/v2/device/name/Random-Boolean-Device/WriteBoolValue",
"method": "put",
"dataTemplate": "{\"Bool\":\"false\", \"EnableRandomization_Bool\": \"false\"}",
"sendSingle": true
}
},
{
"log":{}
}
]
}

查看kuiper的log以及布尔型虚拟设备的值,规则生效

下面测试触发规则引擎后设置温湿度设备的高温预警值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"id": "rule2",
"sql": "SELECT avg(temperature) AS avg_temperature FROM demo WHERE temperature != nil and meta(deviceName)= \"TCP-5900\" GROUP BY TUMBLINGWINDOW(ss, 5) HAVING avg(temperature) > 20.0 ",
"actions": [
{
"rest": {
"url": "http://edgex-core-command:59882/api/v2/device/name/TCP-5900/HighTemperatureAlarm",
"method": "put",
"dataTemplate": "{\"HighTemperatureAlarm\":\"66\"}",
"sendSingle": true
}
},
{
"log":{}
}
]
}

rule2触发并且生效

下面测试当10秒内平均温度大于高温预警值的时候,发送数据给云端,规则如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"id": "rule3",
"sql": "SELECT avg(temperature) AS avg_temperature , avg(HighTemperatureAlarm) AS avg_hightemperaturealarm FROM event WHERE temperature != nil and meta(deviceName)= \"TCP-5900\" and meta(sourceName)=\"Data\" GROUP BY TUMBLINGWINDOW(ss, 10) HAVING avg(temperature) > avg(hightemperaturealarm) ",
"actions": [
{
"rest": {
"url": "http://10.116.169.252:8888/kuiper",
"method": "post",
"dataTemplate": "{\"Temperature\": \"{{.avg_temperature}}\",\"HighTemperatureAlarm\": \"{{.avg_hightemperaturealarm}}\"}",
"sendSingle": true
}
},
{
"log":{}
}
]
}

kuiper日志如下:

云端收到消息:

作者

Cindy

发布于

2021-11-22

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×