EdgeX Foundry - 导出数据到 HTTP 服务

本文详细介绍了如何在EdgeXFoundry中使用docker-compose部署虚拟设备服务,以及如何通过SpringBoot开发HTTP服务来接收并处理导出的数据。重点涉及安装步骤、配置修改和接口测试等内容。

# EdgeX Foundry

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

一、概述

1.安装说明

安装方式:

  • 部署虚拟设备服务 ds-virtual 模拟设备
  • EdgeX 导出数据到 HTTP 服务
  • 使用 Spring Boot 开发 HTTP 服务,接收导出数据

2.HTTP 服务

Spring Boot 程序

@RestController
public class EdgexController {

    @RequestMapping("/income")
    public void edgexCallback(@RequestBody HashMap hashMap) {

        System.out.println(hashMap);

    }
}
# 导出数据接口
http://192.168.3.4:8888/income

在这里插入图片描述

二、安装部署

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-http 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-http no-secty
echo MQTT_VERBOSE=
MQTT_VERBOSE=
docker compose  -p edgex -f docker-compose-base.yml -f add-device-virtual.yml -f add-asc-http-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-http-export:
    container_name: edgex-app-http-export
    depends_on:
      consul:
        condition: service_started
        required: true
      core-data:
        condition: service_started
        required: true
    environment:
      EDGEX_PROFILE: http-export
      EDGEX_SECURITY_SECRET_STORE: "false"
      SERVICE_HOST: edgex-app-http-export
      WRITABLE_LOGLEVEL: INFO
      WRITABLE_PIPELINE_FUNCTIONS_HTTPEXPORT_PARAMETERS_URL: http://EXPORT_HOST_PLACE_HOLDER:7770
    hostname: edgex-app-http-export
    image: edgexfoundry/app-service-configurable:3.1.0
    networks:
      edgex-network: null
    ports:
      - mode: ingress
        host_ip: 127.0.0.1
        target: 59704
        published: "59704"
        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_HTTPEXPORT_PARAMETERS_URL: http://192.168.10.131:8080/income这里配置了我们的导出端点,即http://192.168.10.131:8080/income,这里是我自定义的一个springboot应用


WRITABLE_PIPELINE_FUNCTIONS_HTTPEXPORT_PARAMETERS_URL: http://192.168.3.4:8888/income


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

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

    environment:
      EDGEX_PROFILE: http-export
      EDGEX_SECURITY_SECRET_STORE: "false"
      SERVICE_HOST: edgex-app-http-export
      WRITABLE_LOGLEVEL: INFO
      WRITABLE_PIPELINE_FUNCTIONS_HTTPEXPORT_PARAMETERS_URL: http://192.168.3.4:8888/income

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


# 修改 http接口
WRITABLE_PIPELINE_FUNCTIONS_HTTPEXPORT_PARAMETERS_URL: http://192.168.3.4:8888/income
# 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.测试

在这里插入图片描述

{
	profileName = Random - Boolean - Device, apiVersion = v3, readings = [{
		id = 638 d5e03 - 5 f65 - 4007 - 95 f6 - 856986e cf3b0,
		origin = 1708432044141665586,
		deviceName = Random - Boolean - Device,
		resourceName = Bool,
		profileName = Random - Boolean - Device,
		valueType = Bool,
		value = true
	}], origin = 1708432044141665586, id = 5 c05ac22 - 59e6 - 41e e - 8 f9b - c3973b0e17d6, sourceName = Bool, deviceName = Random - Boolean - Device
} 


{
	profileName = Random - Float - Device, apiVersion = v3, readings = [{
		id = 33e07 d1d - dc31 - 4e5 f - b4ca - 4 d2505da81cf,
		origin = 1708432044120816042,
		deviceName = Random - Float - Device,
		resourceName = Float64,
		profileName = Random - Float - Device,
		valueType = Float64,
		value = -1.669694e+308
	}], origin = 1708432044120816042, id = bc40b33a - 9270 - 40 a2 - 90 cb - 510995 d92a02, sourceName = Float64, deviceName = Random - Float - Device
} 


{
	profileName = Random - Integer - Device, apiVersion = v3, readings = [{
		id = 450 d2e8d - 057 d - 4e85 - a09e - d9165d97473c,
		origin = 1708432044127607940,
		deviceName = Random - Integer - Device,
		resourceName = Int8,
		profileName = Random - Integer - Device,
		valueType = Int8,
		value = 93
	}], origin = 1708432044127607940, id = 292 b0bd7 - 8e a1 - 4 aa4 - a903 - 462 df4361169, sourceName = Int8, deviceName = Random - Integer - Device
} 


{
	profileName = Random - UnsignedInteger - Device, apiVersion = v3, readings = [{
		id = 403e305 c - 300 d - 468 a - a057 - edc97a0b3cdf,
		origin = 1708432044131510252,
		deviceName = Random - UnsignedInteger - Device,
		resourceName = Uint8,
		profileName = Random - UnsignedInteger - Device,
		valueType = Uint8,
		value = 223
	}], origin = 1708432044131510252, id = f8ed3c1b - cdb6 - 4189 - bf76 - 3 b3d209bffd0, sourceName = Uint8, deviceName = Random - UnsignedInteger - Device
} 

......
# EdgeX Foundry

https://iothub.org.cn/docs/edgex/
https://iothub.org.cn/docs/edgex/device/export-http/
<think>我们正在处理一个关于EdgeX Foundry配置电表数据采集并上传至MQTT的问题。根据用户的问题,我们需要提供配置步骤。 由于EdgeX Foundry是一个开源的物联网边缘计算平台,它支持设备管理、数据采集和导出到各种目标,包括MQTT代理。 步骤通常包括: 1. 安装EdgeX Foundry。 2. 配置设备服务(Device Service)来连接电表(假设电表支持某种协议,如Modbus)。 3. 创建设备配置文件(Device Profile)和添加设备(Device),以定义电表的数据点和属性。 4. 配置消息总线(Message Bus)将数据导出到外部MQTT代理。 但是,EdgeX Foundry默认使用Redis作为消息总线,要导出到MQTT,我们需要使用EdgeX导出服务(Export Services),特别是应用服务(Application Service)。 在EdgeX Geneva版本之后,推荐使用应用服务可配置管道(App Service Configurable)来设置数据导出。 具体步骤: 1. 安装EdgeX Foundry:根据官方文档安装EdgeX Foundry。 2. 安装设备服务:例如,如果电表使用Modbus协议,则安装Modbus设备服务。 3. 配置Modbus设备服务:定义电表的连接信息(如IP、端口等)。 4. 创建设备配置文件和设备:在EdgeX的元数据服务中创建。 5. 配置应用服务导出到MQTT:使用App Service Configurable,配置它从EdgeX消息总线(如Redis)读取数据,然后发送到MQTT代理。 详细步骤: 步骤1:安装EdgeX Foundry 参考EdgeX官方文档进行安装,可以使用Docker容器方式。 步骤2:安装Modbus设备服务 同样,使用Docker镜像安装Modbus设备服务。 步骤3:配置Modbus设备服务 修改Modbus设备服务的配置文件,添加电表设备的信息,如: - 设备名称 - Modbus从机地址 - 连接参数(IP地址、端口等) - 定义要读取的寄存器和数据点 步骤4:创建设备配置文件和设备 使用EdgeX的API或通过UI(如果有)创建设备配置文件和设备。设备配置文件定义了电表的数据模型(如电压、电流等),设备则代表具体的电表实例。 步骤5:配置应用服务导出到MQTT 这里我们使用App Service Configurable。首先,我们需要创建一个配置文件,指定输入(来自EdgeX消息总线)和输出(MQTT代理)。 示例配置文件(例如:export-to-mqtt.yml)可能包含: ``` ApplicationName: "export-to-mqtt" Service: Host: "localhost" Port: 48100 Protocol: "http" # 连接到EdgeX的消息总线 MessageBus: Type: "redis" SubscribeHost: Host: "localhost" Port: 6379 Protocol: "redis" # 配置导出到MQTT Writable: LogLevel: "DEBUG" Pipeline: ExecutionOrder: "TransformToJSON, MQTTSend" Functions: TransformToJSON: Parameters: Type: "json" MQTTSend: Parameters: BrokerAddress: "tcp://<MQTT Broker Address>:<Port>" Topic: "edgex/events" ClientId: "edgex-export-client" AutoReconnect: "true" Retain: "false" SkipVerify: "true" PersistOnError: "false" ``` 然后启动这个应用服务,使用上述配置文件。 注意:上述配置文件中的`<MQTT Broker Address>`和`<Port>`需要替换为实际的MQTT代理地址和端口。 步骤6:验证数据流 检查MQTT代理是否接收到来自EdgeX的事件数据。 引用EdgeX Foundry的官方文档和资源,我们可以确保步骤的准确性。 由于用户问题中提到了两个引用,但引用内容与问题关联不大,我们主要依据EdgeX官方文档。 生成相关问题: 1. 如何为EdgeX Foundry创建设备配置文件? 2. 如何配置EdgeX Foundry的应用服务导出数据到其他目标? 3.EdgeX Foundry中,如何处理设备服务采集的数据?</think>### EdgeX Foundry 配置电表数据采集并上传至 MQTT 的步骤 #### 1. **环境准备** - 安装 EdgeX Foundry 核心服务(推荐使用 Docker 容器): ```bash docker run -d --name edgex-core --network host edgexfoundry/docker-edgex-core:latest ``` - 安装 MQTT 代理(如 Mosquitto): ```bash docker run -d --name mqtt-broker -p 1883:1883 eclipse-mosquitto ``` #### 2. **配置设备服务** - **添加电表设备服务**(以 Modbus 电表为例): ```bash docker run -d --name edgex-device-modbus --network host edgexfoundry/device-modbus:latest ``` - **创建设备配置文件**(YAML 格式): ```yaml name: "ElectricMeter-Profile" deviceResources: - name: "Voltage" properties: valueType: "Float32" readWrite: "R" scale: 0.1 # 电压缩放系数 ``` 上传配置文件到 EdgeX: ```bash curl -X POST http://localhost:48081/api/v2/deviceprofile/uploadfile -F "file=@/path/to/electric_meter.yml" ``` #### 3. **注册电表设备** 通过 EdgeX REST API 注册设备: ```bash curl -X POST http://localhost:48081/api/v2/device \ -H 'Content-Type: application/json' \ -d '{ "name": "Meter-01", "serviceName": "edgex-device-modbus", "profileName": "ElectricMeter-Profile", "protocols": { "modbus-tcp": { "Address": "192.168.1.100", "Port": "502", "UnitID": "1" } } }' ``` #### 4. **配置 MQTT 导出服务** - **启用应用服务**(App Service Configurable): ```bash docker run -d --name edgex-app-mqtt --network host \ -v /path/to/export-config:/res edgexfoundry/app-service-configurable:latest \ --cp=consul.http://localhost:8500 --registry ``` - **配置导出规则**(修改 `configuration.toml`): ```toml [Writable.Pipeline] ExecutionOrder = "TransformToJSON, MQTTSend" [Writable.Pipeline.Functions.MQTTSend] [Writable.Pipeline.Functions.MQTTSend.Parameters] BrokerAddress = "tcp://mqtt-broker:1883" Topic = "edgex/meter/data" ClientId = "edgex-exporter" ``` #### 5. **验证数据流** - 订阅 MQTT 主题检查数据: ```bash mosquitto_sub -t "edgex/meter/data" -v ``` - 预期输出(JSON 格式): ```json { "device": "Meter-01", "readings": [{"name": "Voltage", "value": "220.5"}] } ``` #### 6. **故障排除** - **设备未连接**:检查 `edgex-device-modbus` 日志: ```bash docker logs edgex-device-modbus ``` - **MQTT 连接失败**:确认 Broker 地址和端口正确,防火墙放行 1883 端口[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IoTHub - 物联网开源技术社区

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

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

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

打赏作者

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

抵扣说明:

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

余额充值