EdgeX Foundry - 导出数据到 MQTT 服务

# EdgeX Foundry

https://iothub.org.cn/docs/edgex/
https://iothub.org.cn/docs/edgex/device/export-mqtt/

一、概述

1.安装说明

安装方式:

  • 部署虚拟设备服务 ds-virtual 模拟设备
  • EdgeX 导出数据到 EMQX,EMQX 作为 mqtt-broker
  • 使用 MQTTX 工具从 EMQX 订阅导出数据

2.安装 EMQX

$ docker run -d --name emqx -p 18084:18083 -p 1884:1883 emqx/emqx:4.4.17
[root@edgex custom-config]# docker run -d --name emqx -p 18084:18083 -p 1884:1883 emqx/emqx:4.4.17
Unable to find image 'emqx/emqx:4.4.17' locally
4.4.17: Pulling from emqx/emqx
3d2430473443: Pull complete 
9affe486a1de: Pull complete 
59289c77776e: Pull complete 
7f4b43e76fd0: Pull complete 
a73c64d8e555: Pull complete 
4f4fb700ef54: Pull complete 
ff83ca607058: Pull complete 
7787af4360dd: Pull complete 
Digest: sha256:6b81ca26a7f7243e46186b35ac9f1580b497cf30fd09064ca7ee4875934b66bf
Status: Downloaded newer image for emqx/emqx:4.4.17
13a37d777b15408802ca87da2dd296a4359767dc4c224d72dab93bbf4b051e33

访问EMQX

# 访问地址

http://192.168.202.233:18084
账号:admin
初始密码:public
修改密码:1qaz2wsx

在这里插入图片描述

3.MQTTX 工具

在这里插入图片描述

二、安装部署

1.docker-comepse

# 1.克隆 edgex-compose
$ git clone git@github.com:edgexfoundry/edgex-compose.git 
$ git clone https://github.com/edgexfoundry/edgex-compose.git
$ cd edgex-compose 
$ git checkout v3.1


# 2.生成 docker-compose.yml 文件(注意这包括 mqtt-broker)
$ cd compose-builder
$ make gen ds-virtual asc-mqtt no-secty


# 3.检查生成的文件
$ ls | grep 'docker-compose.yml'
docker-compose.yml
[root@edgex mqtt-device]# git clone https://github.com/edgexfoundry/edgex-compose.git
Cloning into 'edgex-compose'...
remote: Enumerating objects: 4779, done.
remote: Counting objects: 100% (2916/2916), done.
remote: Compressing objects: 100% (173/173), done.
remote: Total 4779 (delta 2831), reused 2804 (delta 2741), pack-reused 1863
Receiving objects: 100% (4779/4779), 1.22 MiB | 450.00 KiB/s, done.
Resolving deltas: 100% (4042/4042), done.


[root@edgex mqtt-device]# ll
total 4
drwxr-xr-x. 6 root root 4096 Feb  1 04:10 edgex-compose


[root@edgex mqtt-device]# cd edgex-compose/
[root@edgex edgex-compose]# git checkout v3.1
Note: checking out 'v3.1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 488a3fe... Merge pull request #424 from lenny-intel/device-mqtt-secure-mode-napa


[root@edgex edgex-compose]# cd compose-builder/

[root@edgex compose-builder]# make gen ds-virtual asc-mqtt no-secty
echo MQTT_VERBOSE=
MQTT_VERBOSE=
docker compose  -p edgex -f docker-compose-base.yml -f add-device-virtual.yml -f add-asc-mqtt-export.yml convert > docker-compose.yml
rm -rf ./gen_ext_compose


[root@edgex compose-builder]# ls | grep 'docker-compose.yml'
docker-compose.yml
[root@edgex compose-builder]# vim docker-compose.yml 

name: edgex
services:
  app-mqtt-export:
    container_name: edgex-app-mqtt-export
    depends_on:
      consul:
        condition: service_started
        required: true
      core-data:
        condition: service_started
        required: true
    environment:
      EDGEX_PROFILE: mqtt-export
      EDGEX_SECURITY_SECRET_STORE: "false"
      SERVICE_HOST: edgex-app-mqtt-export
      WRITABLE_LOGLEVEL: INFO
      WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_BROKERADDRESS: MQTT_BROKER_ADDRESS_PLACE_HOLDER
      WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_TOPIC: edgex-events
    hostname: edgex-app-mqtt-export
    image: edgexfoundry/app-service-configurable:3.1.0
    networks:
      edgex-network: null
    ports:
      - mode: ingress
        host_ip: 127.0.0.1
        target: 59703
        published: "59703"
        protocol: tcp
    read_only: true
    restart: always
    security_opt:
      - no-new-privileges:true
    user: 2002:2001
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime
        read_only: true
        bind:
          create_host_path: true

......

2.修改配置

# 修改配置
WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_BROKERADDRESS导出地址及WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_TOPIC导出主题可以自定义以方便服务接受。
tcp://broker.mqttdashboard.com:1883
tcp://192.168.202.233:1884


[root@edgex compose-builder]# vim docker-compose.yml 

name: edgex
services:
  app-mqtt-export:
    container_name: edgex-app-mqtt-export
......

    environment:
      EDGEX_PROFILE: mqtt-export
      EDGEX_SECURITY_SECRET_STORE: "false"
      SERVICE_HOST: edgex-app-mqtt-export
      WRITABLE_LOGLEVEL: INFO
      WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_BROKERADDRESS: tcp://192.168.202.233:1884
      WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_TOPIC: edgex-events

3.启动 EdgeX Foundry

使用以下命令部署 EdgeX:

$ cd edgex-compose/compose-builder
$ docker compose pull
$ docker compose up -d


# 修改配置文件
替换IP地址 127.0.0.1 为 0.0.0.0


# 修改 mqtt-broker
WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_BROKERADDRESS: tcp://192.168.202.233:1884
# docker compose pull

# docker compose up -d

在这里插入图片描述
在这里插入图片描述

4.访问 UI

4.1. consul
# 访问地址
http://192.168.202.233:8500

在这里插入图片描述

4.2. EdgeX Console
# 访问地址
http://192.168.202.233:4000/

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.测试

在这里插入图片描述

{
	"apiVersion": "v3",
	"id": "e6d5b1a1-6c24-4f16-bfea-bad5ce2ff25b",
	"deviceName": "Random-Integer-Device",
	"profileName": "Random-Integer-Device",
	"sourceName": "Int64",
	"origin": 1708427453171230677,
	"readings": [{
		"id": "e270f251-2033-45b7-8374-41a788e17383",
		"origin": 1708427453171230677,
		"deviceName": "Random-Integer-Device",
		"resourceName": "Int64",
		"profileName": "Random-Integer-Device",
		"valueType": "Int64",
		"value": "1409412935702907573"
	}]
}


{
	"apiVersion": "v3",
	"id": "089838b3-6697-449f-ba24-fff2b67ff148",
	"deviceName": "Random-Integer-Device",
	"profileName": "Random-Integer-Device",
	"sourceName": "Int32",
	"origin": 1708427453171241607,
	"readings": [{
		"id": "dfef620a-1227-4e42-b4a9-ae035595f4f4",
		"origin": 1708427453171241607,
		"deviceName": "Random-Integer-Device",
		"resourceName": "Int32",
		"profileName": "Random-Integer-Device",
		"valueType": "Int32",
		"value": "1227083275"
	}]
}

......
# EdgeX Foundry

https://iothub.org.cn/docs/edgex/
https://iothub.org.cn/docs/edgex/device/export-mqtt/
### EdgeX Foundry 集成 MQTT 方法和配置教程 #### 设备接入准备 为了使EdgeX Foundry支持MQTT协议下的设备通信,需先确认环境已安装好必要的组件和服务。通常情况下,这涉及到部署EdgeX Foundry的核心微服务以及特定于MQTT的支持模块[^1]。 #### 安装与配置MQTT Broker 在开始之前,确保有一个可用的MQTT消息代理(Broker)。可以选用Mosquitto或其他兼容的MQTT服务器作为消息中心。对于本地测试而言,在同一台机器上运行Broker即可满足需求;而对于生产环境,则建议采用高可用性的集群方案来保障稳定性[^2]。 #### 启动EdgeX Foundry核心服务 按照官方文档指导完成EdgeX Foundry平台的基础搭建工作之后,重点在于激活那些负责处理外部传感器或执行器连接请求的服务实例——特别是`device-mqtt-go`这一部分。该服务专门用于管理基于MQTT协议的数据交换过程[^3]。 ```bash docker-compose -f edgex-res/docker-compose.yml up -d ``` 上述命令会拉取并启动一系列预定义好的容器镜像文件,其中包括了所有必需的核心功能单元及其依赖项。 #### 创建自定义Device Profile JSON文件 针对具体应用场景定制化描述目标硬件特性和行为模式的一组参数集合被称为“设备档案”。这里需要创建一个适用于所选型号的JSON格式模板,并上传至EdgeX系统内供后续操作引用。此步骤至关重要因为它直接影响到实际业务逻辑的设计思路和发展方向。 #### 注册新设备 利用RESTful API接口向EdgeX Foundry注册新的物理实体对象时,除了常规的身份验证信息外还需要指定先前建立过的Profile ID以及其他附加属性字段。这样做的目的是为了让整个框架清楚知道当前正在尝试关联的是哪一类装置类型。 ```json { "name": "MyMqttSensor", "description": "A sample sensor connected via MQTT protocol.", "adminState": "UNLOCKED", "operatingState": "ENABLED", "labels": ["temperature", "humidity"], "location": null, "serviceId": "<your-service-id>", "profileName": "<your-profile-name>" } ``` 以上是一个简化版的例子用来说明如何构建有效的POST请求体结构以便顺利完成这项任务。 #### 测试连通性 最后一步就是检验一切设置是否正常运作了。可以通过发送简单指令给远端节点或者订阅主题监听实时更新的方式来初步判断双方之间是否存在有效链路存在。如果遇到任何异常情况也不要慌张,仔细查阅日志记录往往能帮助快速定位问题所在之处。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoTHub - 物联网开源技术社区

支持开源技术! 传播开源文化!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值