发送数据+EdgeX Foundry +MQTTX导出数据
前言
在物联网(IoT)领域,数据的高效传输和处理是实现智能应用的关键。EdgeX Foundry作为一个开源的物联网边缘计算平台,通过其模块化架构,提供了数据采集、处理、存储和传输的完整解决方案。然而,尽管EdgeX Foundry在技术上具有许多优势,现有的应用部署教程往往较为简略,缺乏详细的指导和实例。
为了填补这一空白,本文将详细介绍如何使用EdgeX Foundry进行数据的高效传输与处理,特别是通过MQTT协议发送数据并利用MQTTX工具导出这些数据的完整流程。MQTT作为一种轻量级的消息传输协议,以其低带宽占用和高效的消息传递能力,成为物联网设备之间通信的理想选择。通过EdgeX Foundry与MQTT的结合,我们能够实现边缘设备与云端服务的无缝数据传输。
一、搭建Edgex
1.安装 Docker
yum -y install docker-ce
systemctl enable docker && systemctl start docker
docker --version
#添加阿里云加速镜像
cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://gcctk8ld.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=cgroupfs"]
}
#重启docker
systemctl restart docker
2.安装 docker-compose
#下载源码
curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-Linux-x86_64 -o /usr/local/bin/docker-compose
#给docker-compose添加执行权限
sudo chmod +x /usr/local/bin/docker-compose
#查看docker-compose是否安装成功
docker-compose -version
3.创建 docker-compose 文件
主题为:example-topic
WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_ADDRESSABLE_ADDRESS: 192.168.132.134
- 192.168.132.134 要换成自己的ip地址
[root@compute ~]# vim docker-compose.yml
# /*******************************************************************************
# * Copyright 2020 Redis Labs Inc.
# * Copyright 2020 Intel Corporation.
# *
# * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# * in compliance with the License. You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software distributed under the License
# * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# * or implied. See the License for the specific language governing permissions and limitations under
# * the License.
# *
# * @author: Jim White, Dell
# * @author: Andre Srinivasan, Redis Labs
# * @author: Leonard Goodell, Intel
# * EdgeX Foundry, Geneva, version 1.2.0
# * added: May 14, 2020
# *******************************************************************************/
# NOTE: this Docker Compose file does not contain the security services - namely the API Gateway
# and Secret Store
version: '3.4'
# all common shared environment variables defined here:
x-common-env-variables: &common-variables
EDGEX_SECURITY_SECRET_STORE: "false"
Registry_Host: edgex-core-consul
Clients_CoreData_Host: edgex-core-data
Clients_Data_Host: edgex-core-data # For device Services
Clients_Notifications_Host: edgex-support-notifications
Clients_Metadata_Host: edgex-core-metadata
Clients_Command_Host: edgex-core-command
Clients_Scheduler_Host: edgex-support-scheduler
Clients_RulesEngine_Host: edgex-kuiper
Databases_Primary_Host: edgex-redis
# Required in case old configuration from previous release used.
# Change to "true" if re-enabling logging service for remote logging
Logging_EnableRemote: "false"
# Clients_Logging_Host: edgex-support-logging # un-comment if re-enabling logging service for remote logging
volumes:
db-data:
log-data:
consul-config:
consul-data:
services:
consul:
image: edgexfoundry/docker-edgex-consul:1.2.0
ports:
- "0.0.0.0:8400:8400"
- "0.0.0.0:8500:8500"
container_name: edgex-core-consul
hostname: edgex-core-consul
networks:
- edgex-network
volumes:
- consul-config:/consul/config:z
- consul-data:/consul/data:z
environment:
- EDGEX_DB=redis
- EDGEX_SECURE=false
redis:
image: redis:5.0.8-alpine
ports:
- "0.0.0.0:6379:6379"
container_name: edgex-redis
hostname: edgex-redis
networks:
- edgex-network
environment:
<<: *common-variables
volumes:
- db-data:/data:z
# The logging service has been deprecated in Geneva release and will be removed in the Hanoi release.
# All services are configure to send logging to STDOUT, i.e. not remote which requires this logging service
# If you still must use remote logging, un-comment the block below, all the related depends that have been commented out
# and the related global override that are commented out at the top.
#
# logging:
# image: edgexfoundry/docker-support-logging-go:1.2.1
# ports:
# - "0.0.0.0:48061:48061"
# container_name: edgex-support-logging
# hostname: edgex-support-logging
# networks:
# - edgex-network
# environment:
# <<: *common-variables
# Service_Host: edgex-support-logging
# Writable_Persistence: file
# Databases_Primary_Type: file
# Logging_EnableRemote: "false"
# depends_on:
# - consul
system:
image: edgexfoundry/docker-sys-mgmt-agent-go:1.2.1
ports:
- "0.0.0.0:48090:48090"
container_name: edgex-sys-mgmt-agent
hostname: edgex-sys-mgmt-agent
networks:
- edgex-network
environment:
<<: *common-variables
Service_Host: edgex-sys-mgmt-agent
ExecutorPath: /sys-mgmt-executor
MetricsMechanism: executor
volumes:
- /var/run/docker.sock:/var/run/docker.sock:z
depends_on:
- consul
# - logging # uncomment if re-enabled remote logging
- scheduler
- notifications
- metadata
- data
- command
notifications:
image: edgexfoundry/docker-support-notifications-go:1.2.1
ports:
- "0.0.0.0:48060:48060"
container_name: edgex-support-notifications
hostname: edgex-support-notifications
networks:
- edgex-network
environment:
<<: *common-variables
Service_Host: edgex-support-notifications
depends_on:
- consul
# - logging # uncomment if re-enabled remote logging
- redis
metadata:
image: edgexfoundry/docker-core-metadata-go:1.2.1
ports:
- "0.0.0.0:48081:48081"
container_name: edgex-core-metadata
hostname: edgex-core-metadata
networks:
- edgex-network
environment:
<<: *common-variables
Service_Host: edgex-core-metadata
Service_Timeout: "20000"
Notifications_Sender: edgex-core-metadata
depends_on:
- consul
# - logging # uncomment if re-enabled remote logging
- redis
- notifications
data:
image: edgexfoundry/docker-core-data-go:1.2.1
ports:
- "0.0.0.0:48080:48080"
- "0.0.0.0:5563:5563"
container_name: edgex-core-data
hostname: edgex-core-data
networks:
- edgex-network
environment:
<<: *common-variables
Service_Host: edgex-core-data
depends_on:
- consul
# - logging # uncomment if re-enabled remote logging
- redis
- metadata
command:
image: edgexfoundry/docker-core-command-go:1.2.1
ports:
- "0.0.0.0:48082:48082"
container_name: edgex-core-command
hostname: edgex-core-command
networks:
- edgex-network
environment:
<<: *common-variables
Service_Host: edgex-core-command
depends_on:
- consul
# - logging # uncomment if re-enabled remote logging
- redis
- metadata
scheduler:
image: edgexfoundry/docker-support-scheduler-go:1.2.1
ports:
- "0.0.0.0:48085:48085"
container_name: edgex-support-scheduler
hostname: edgex-support-scheduler
networks:
- edgex-network
environment:
<<: *common-variables
Service_Host: edgex-support-scheduler
IntervalActions_ScrubPushed_Host: edgex-core-data
IntervalActions_ScrubAged_Host: edgex-core-data
depends_on:
- consul
# - logging # uncomment if re-enabled remote logging
- redis
app-service-rules:
image: edgexfoundry/docker-app-service-configurable:1.2.0
ports:
- "0.0.0.0:48100:48100"
container_name: edgex-app-service-configurable-rules
hostname: edgex-app-service-configurable-rules
networks:
- edgex-network
environment:
<<: *common-variables
edgex_profile: rules-engine
Service_Host: edgex-app-service-configurable-rules
Service_Port: 48100
MessageBus_SubscribeHost_Host: edgex-core-data
Binding_PublishTopic: events
depends_on:
- consul
# - logging # uncomment if re-enabled remote logging
- data
app-service-mqtt:
image: edgexfoundry/docker-app-service-configurable:1.1.0
ports:
- "0.0.0.0:48101:48101"
container_name: edgex-app-service-configurable-mqtt
hostname: edgex-app-service-configurable-mqtt
networks:
edgex-network:
aliases:
- edgex-app-service-configurable-mqtt
environment:
<<: *common-variables
edgex_profile: mqtt-export
Service_Host: edgex-app-service-configurable-mqtt
Service_Port: 48101
MessageBus_SubscribeHost_Host: edgex-core-data
Binding_PublishTopic: events
# Added for MQTT export using app service
#WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_ADDRESSABLE_ADDRESS: broker.hivemq.com
WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_ADDRESSABLE_ADDRESS: 192.168.132.134
WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_ADDRESSABLE_PORT: 1883
WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_ADDRESSABLE_PROTOCOL: tcp
WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_ADDRESSABLE_TOPIC: "example-topic"
WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_PARAMETERS_AUTORECONNECT: "true"
WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_PARAMETERS_RETAIN: "true"
WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_PARAMETERS_PERSISTONERROR: "false"
# WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_ADDRESSABLE_PUBLISHER:
# WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_ADDRESSABLE_USER:
# WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_ADDRESSABLE_PASSWORD:
# WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_PARAMETERS_QOS: ["your quality or service"]
# WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_PARAMETERS_KEY: [your Key]
# WRITABLE_PIPELINE_FUNCTIONS_MQTTSEND_PARAMETERS_CERT: [your Certificate]
depends_on:
- consul
# - logging # uncomment if re-enabled remote logging
- data
rulesengine:
image: emqx/kuiper:0.4.2-alpine
ports:
- "0.0.0.0:48075:48075"
- "0.0.0.0:20498:20498"
container_name: edgex-kuiper
hostname: edgex-kuiper
networks:
- edgex-network
environment:
# KUIPER_DEBUG: "true"
KUIPER_CONSOLE_LOG: "true"
KUIPER_REST_PORT: 48075
EDGEX_SERVER: edgex-app-service-configurable-rules
EDGEX_SERVICE_SERVER: http://edgex-core-data:48080
EDGEX_TOPIC: events
EDGEX_PROTOCOL: tcp
EDGEX_PORT: 5566
depends_on:
- app-service-rules
# Support RulesEngine has been deprecated in the Geneva (1.2.0) release
# If still required, simply uncomment the block below and comment out the block above.
#
# rulesengine:
# image: edgexfoundry/docker-support-rulesengine:1.2.1
# ports:
# - "0.0.0.0:48075:48075"
# container_name: edgex-support-rulesengine
# hostname: edgex-support-rulesengine
# networks:
# - edgex-network
# depends_on:
# - app-service-rules
#################################################################
# Device Services
#################################################################
device-rest:
image: edgexfoundry/docker-device-rest-go:1.1.1
ports:
- "0.0.0.0:49986:49986"
container_name: edgex-device-rest
hostname: edgex-device-rest
networks:
- edgex-network
environment:
<<: *common-variables
Service_Host: edgex-device-rest
depends_on:
- data
- command
# - logging # uncomment if re-enabled remote logging
# device-random:
# image: edgexfoundry/docker-device-random-go:1.2.1
# ports:
# - "0.0.0.0:49988:49988"
# container_name: edgex-device-random
# hostname: edgex-device-random
# networks:
# - edgex-network
# environment:
# <<: *common-variables
# Service_Host: edgex-device-random
# depends_on:
# - data
# - command
#
# device-mqtt:
# image: edgexfoundry/docker-device-mqtt-go:1.2.1
# ports:
# - "0.0.0.0:49982:49982"
# container_name: edgex-device-mqtt
# hostname: edgex-device-mqtt
# networks:
# - edgex-network
# environment:
# <<: *common-variables
# Service_Host: edgex-device-mqtt
# depends_on:
# - data
# - command
#
# device-modbus:
# image: edgexfoundry/docker-device-modbus-go:1.2.1
# ports:
# - "0.0.0.0:49991:49991"
# container_name: edgex-device-modbus
# hostname: edgex-device-modbus
# networks:
# - edgex-network
# environment:
# <<: *common-variables
# Service_Host: edgex-device-modbus
# depends_on:
# - data
# - command
#
# device-snmp:
# image: edgexfoundry/docker-device-snmp-go:1.2.1
# ports:
# - "0.0.0.0:49993:49993"
# container_name: edgex-device-snmp
# hostname: edgex-device-snmp
# networks:
# - edgex-network
# environment:
# <<: *common-variables
# Service_Host: edgex-device-snmp
# depends_on:
# - data
# - command
networks:
edgex-network:
driver: "bridge"
4.启动 EdgeX Foundry
时间会久一些,耐心等待
[root@compute ~]# sudo docker-compose up -d
[root@compute ~]# docker compose up -d
WARN[0000] /geneva/docker-compose.yml: `version` is obsolete
[+] Running 12/12
✔ Container edgex-redis Started 0.4s
✔ Container edgex-core-consul Started 0.5s
✔ Container edgex-support-notifications Started 0.9s
✔ Container edgex-core-metadata Started 1.4s
✔ Container edgex-core-command Started 2.0s
✔ Container edgex-support-scheduler Started 1.0s
✔ Container edgex-core-data Started 1.8s
✔ Container edgex-app-service-configurable-mqtt Started 2.5s
✔ Container edgex-app-service-configurable-rules Started 2.6s
✔ Container edgex-kuiper Started 3.0s
✔ Container edgex-device-rest Started 2.8s
✔ Container edgex-sys-mgmt-agent Started 2.7s
[root@compute ~]# docker compose ps
WARN[0000] /geneva/docker-compose.yml: `version` is obsolete
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
edgex-app-service-configurable-mqtt edgexfoundry/docker-app-service-configurable:1.1.0 "/app-service-config…" app-service-mqtt 2 minutes ago Up 16 seconds 48095/tcp, 0.0.0.0:48101->48101/tcp
edgex-app-service-configurable-rules edgexfoundry/docker-app-service-configurable:1.2.0 "/app-service-config…" app-service-rules 2 minutes ago Up 16 seconds 48095/tcp, 0.0.0.0:48100->48100/tcp
edgex-core-command edgexfoundry/docker-core-command-go:1.2.1 "/core-command -cp=c…" command 2 minutes ago Up 16 seconds 0.0.0.0:48082->48082/tcp
edgex-core-consul edgexfoundry/docker-edgex-consul:1.2.0 "edgex-consul-entryp…" consul 2 minutes ago Up 18 seconds 0.0.0.0:8400->8400/tcp, 8300-8302/tcp, 8301-8302/udp, 8600/tcp, 8600/udp, 0.0.0.0:8500->8500/tcp
edgex-core-data edgexfoundry/docker-core-data-go:1.2.1 "/core-data -cp=cons…" data 2 minutes ago Up 17 seconds 0.0.0.0:5563->5563/tcp, 0.0.0.0:48080->48080/tcp
edgex-core-metadata edgexfoundry/docker-core-metadata-go:1.2.1 "/core-metadata -cp=…" metadata 2 minutes ago Up 17 seconds 0.0.0.0:48081->48081/tcp
edgex-device-rest edgexfoundry/docker-device-rest-go:1.1.1 "/device-rest-go --c…" device-rest 2 minutes ago Up 16 seconds 0.0.0.0:49986->49986/tcp
edgex-kuiper emqx/kuiper:0.4.2-alpine "/usr/bin/docker-ent…" rulesengine 2 minutes ago Up 15 seconds 0.0.0.0:20498->20498/tcp, 9081/tcp, 0.0.0.0:48075->48075/tcp
edgex-redis redis:5.0.8-alpine "docker-entrypoint.s…" redis 2 minutes ago Up 18 seconds 0.0.0.0:6379->6379/tcp
edgex-support-notifications edgexfoundry/docker-support-notifications-go:1.2.1 "/support-notificati…" notifications 2 minutes ago Up 18 seconds 0.0.0.0:48060->48060/tcp
edgex-support-scheduler edgexfoundry/docker-support-scheduler-go:1.2.1 "/support-scheduler …" scheduler 2 minutes ago Up 17 seconds 0.0.0.0:48085->48085/tcp
edgex-sys-mgmt-agent edgexfoundry/docker-sys-mgmt-agent-go:1.2.1 "/sys-mgmt-agent -cp…" system 2 minutes ago Up 16 seconds 0.0.0.0:48090->48090/tcp
访问UI: http://IP:8500
二、使用postman发送数据
在此使用postman测试发送数据,后续可用摄像头等工具发送数据
1.安装postman
可参考postman教程
2.使用postman发送数据
Edgex接收数据对应的接口为:http://IP:48080/api/v1/event
以笔者为例,为http://192.168.132.134:48080/api/v1/event
测试代码
{
"device": "device-id",
"readings": [
{
"name": "temperature",
"value": "25",
"valueType": "Int64",
"mediaType": "application/json",
"attributes": {
"unit": "Celsius"
}
}
]
}
三、使用MQTTX导出数据
1.在Windows端下载MQTTX
2.连接
3.订阅主题
笔者设置的docker-compose 文件主题为:example-topic
4.导出数据
重新在postman发送数据,成功接受并导出数据