EdgeX foundry樹莓派搭建與MQTT通訊


參考資料

使用容器方法管理和搭建EdgeX:https://docs.edgexfoundry.org/1.2/getting-started/quick-start/

什么是微服務,為什么要用微服務:https://www.zhihu.com/question/65502802

什么是MQTT,一個最簡單的demo是什么:https://www.jianshu.com/p/14b34e537fd4

針對樹莓派的EdgeX是如何的?這里使用的是樹莓派4B,將之前的系統重新刷成64位arm,便於使用。(EdgeX對64位arm支持度較高)

 

MQTT通信命令

訂閱

mosquitto_sub -d -t topic1 
mosquitto_sub -d -h localhost -p 1883 -i subscriber-test -t topic1

發布

mosquitto_pub -d -t topic1 -m "Hello MQTT"

  

樹莓派Ubuntu mate20系統安裝

1 選擇合適的鏡像燒錄工具

https://www.raspberrypi.org/downloads/

 

 

 2 下載鏡像

https://ubuntu-mate.org/download/arm64/focal/

 

 

3 使用鏡像燒錄工具打開鏡像,插入SD卡,開始燒錄。(鏡像下載比較慢,使用第二步的離線迅雷下載完以后直接燒錄比較好)

4 給樹莓派接上鼠標和鍵盤和重要的顯示器,上電后根據屏幕引導進行安裝即可。該系統對HDMI的輸出電流可能不夠,導致屏幕不亮。而從樹莓派直接引電增強HDMI信號則仍然不夠,需要外部引電(建議3A)。

5 排錯的方法:插上網口看網口燈是都閃爍,閃爍則系統安裝正確。樹莓派3B的系統直接裝到4B上是不行的,系統安裝不正確。

 

Docker安裝

按照其中提供的兩個鏈接進行安裝。https://docs.edgexfoundry.org/1.2/getting-started/quick-start/

其中第二個:docker-compose安裝,如果不正確,則使用pip3進行安裝(和python3有關)。

pip3可以通過apt-get 方式安裝。

pip3 install docker-compose

安裝完后通過以下命令查看

docker-compose --version

  

MQTT測試注意事項

如下鏈接中提供了MQTT的測試demo。其過程是通過重復向HiveMQ發布一個主題為EdgeXEvents,內容為一個json數據。

然后客戶端也通過HiveMQ連接到broker.mqttdashboard.com,訂閱EdgeXEvents主題,則可以獲取到json數據。

https://docs.edgexfoundry.org/1.2/getting-started/quick-start/

有兩點要注意:提供的HiveMQ網站,其js腳本中的JQuery可能會由於外網的原因無法加載,導致$符號無法識別,這時則需要更換網絡,以正確加載和連接。

另外是在docker-compose.yml文件中添加的一段配置,其中

image: edgexfoundry/docker-app-service-configurable:1.1.0

應該改為

image: edgexfoundry/docker-app-service-configurable-arm64:1.1.0

  

另外

判斷服務是否正常的方法是

docker-compose ps

如果某個進程卡掉,則該進程配置不正確,如mqtt

docker服務需要啟動才可運行EdgeX的容器

service docker start

  

 

2020年9月15日22:53:36

補充通信-邊緣到設備

之前的MQTT通信方式是:

樹莓派部署了EdgeXFoundry,其發送隨機json數據到Hive MQ的公共雲平台上,然后傳感器設備從雲平台上使用MQTT訂閱功能獲取。這樣不太符合邊緣計算的思路。

邊緣計算應該是將數據在邊緣節點直接處理,直接和傳感器設備交互。

 

准備工作:

consul監控服務。其端口是8500,因為監控了整個微服務的健康狀態,所以非常重要。如果哪個服務沒有ping通,則應該用以下等命令,分析日志。

docker-compose up -d
dokcer logs -f xxx  

UI管理界面端口4000,也是后面要用到的。

 

重要配置文件

需要另外寫到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
  Clients_VirtualDevice_Host: edgex-device-virtual
  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-arm64:1.2.0
    ports:
      - "8400:8400"
      - "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: arm64v8/redis:5.0.8-alpine
    ports:
      - "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-arm64:1.2.1
    ports:
      - "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-arm64:1.2.1
    ports:
      - "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-arm64:1.2.1
    ports:
      - "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-arm64:1.2.1
    ports:
      - "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-arm64:1.2.1
    ports:
      - "48080:48080"
      - "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-arm64:1.2.1
    ports:
      - "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-arm64:1.2.1
    ports:
      - "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-arm64:1.2.0
    ports:
      - "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

  rulesengine:
    image: emqx/kuiper:0.4.2-alpine
    ports:
      - "48075:48075"
      - "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-arm64:1.2.1
  #   ports:
  #     - "48075:48075"
  #   container_name: edgex-support-rulesengine
  #   hostname: edgex-support-rulesengine
  #   networks:
  #     - edgex-network
  #   depends_on:
  #     - app-service-rules

#################################################################
# Device Services
#################################################################

  device-virtual:
    image: edgexfoundry/docker-device-virtual-go-arm64:1.2.2
    ports:
    - "49990:49990"
    container_name: edgex-device-virtual
    hostname: edgex-device-virtual
    networks:
      - edgex-network
    environment:
      <<: *common-variables
      Service_Host: edgex-device-virtual
    depends_on:
      - consul
#      - logging  # uncomment if re-enabled remote logging
      - data
      - metadata

  device-rest:
    image: edgexfoundry/docker-device-rest-go-arm64:1.1.1
    ports:
      - "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-arm64:1.2.1
#    ports:
#      - "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-arm64:1.2.1
    ports:
      - "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-arm64:1.2.1
#    ports:
#      - "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-arm64:1.2.1
#    ports:
#      - "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

  app-service-mqtt:
    image: edgexfoundry/docker-app-service-configurable-arm64:1.1.0
    ports:
      - "48101:48101"
    container_name: edgex-app-service-configurable-mqtt
    hostname: edgex-app-service-configurable-mqtt
    networks:
      - edgex-network
    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
      Writable_Pipeline_Functions_MQTTSend_Addressable_Address: 192.168.20.122
      Writable_Pipeline_Functions_MQTTSend_Addressable_Port: 1883
      Writable_Pipeline_Functions_MQTTSend_Addressable_Protocol: tcp
      Writable_Pipeline_Functions_MQTTSend_Addressable_Publisher: edgex
      Writable_Pipeline_Functions_MQTTSend_Addressable_Topic: EdgeXEvent8
    depends_on:
      - consul
      - data

  ui:
    image: edgexfoundry/docker-edgex-ui-go-arm64:1.2.1
    ports:
      - "4000:4000"
    container_name: edgex-ui-go
    hostname: edgex-ui-go
    networks:
      - edgex-network

networks:
  edgex-network:
    driver: "bridge"

另外:

該文檔提供了一種MQTT交互的用例,但是起點較高,也不容易操作。建議繞過:https://docs.edgexfoundry.org/1.2/examples/Ch-ExamplesAddingMQTTDevice/

該文檔是最核心的文檔:https://github.com/edgexfoundry/edgex-ui-go/blob/master/docs/ExamplesAddingMQTTDevice/AddMQTTDeviceToEdgeX.md。根據該文檔,相對比較好操作。

 

 在真正啟動好docker容器們以后,檢查健康狀態。127.0.0.11:53的報錯,只是說該服務沒有ping通。該服務異常。可能是掛掉了,也很可能沒有配置正確。

正常以后,安裝核心文檔里進行配置。請經常檢查微服務的健康狀態。device-mqtt服務經常掛掉,那就刪掉,重啟一下容器看看,確保服務健康。如下圖,點里面的刪除即可。重啟以后文件夾自動生成。

 

 

另外注意事項

特別注意的是:樹莓派現在安裝的架構為arm64,所以在容器使用過程中,如核心文檔里的模擬設備,其是不能應用的。如需模擬設備,應該在局域網中創建一套pc的Ubuntu等,運行docker。

必要的查看路徑是:https://hub.docker.com/r/dersimn/mqtt-scripts,其包含了模擬設備的詳細說明。

 

另外,在調試必要的時候,使用mosquitto,手動訂閱和發布,確定問題出錯點。

另外,配置完UI以后,或者更新完以后,全部刷新網頁再執行操作,否則有些配置可能未生效。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM