網(wǎng)站首頁 編程語言 正文
最近嘗試學(xué)習(xí)協(xié)議適配框架,其中EdgeX作為一款開源的邊緣計(jì)算平臺(tái)受到大家的青睞,于是決定一試。目標(biāo)是實(shí)現(xiàn)基于EdgeX的基礎(chǔ)的南北MQTT通信功能,整體實(shí)現(xiàn)參考了一位大神的博客:
(13條消息) EdgeX 樹莓派實(shí)踐部署_無止無休-CSDN博客_edgex部署https://blog.csdn.net/bxjie/article/details/113860800由于大神寫的比較高階,作為小白的我踩了不少坑,下面分享給大家,僅供參考。
硬件
Raspberry Pi 4 Model B?
不一定非要最新的,不同的硬件版本配套不同的操作系統(tǒng)及其他軟件安裝版本
?microSD卡 128GB
沒必要買很大,主要看需求。最好不要低于8G
micro轉(zhuǎn)HDMI轉(zhuǎn)接頭
由于本人比較懶不想安遠(yuǎn)程控制軟件,于是必須有個(gè)連接顯示屏的轉(zhuǎn)接頭,具體規(guī)格因顯示屏而異
鍵盤、讀卡器、鼠標(biāo)、電源線(TypeC)、顯示屏
軟件
1.?格式化存儲(chǔ)卡
先用SD格式化軟件格式化存儲(chǔ)卡,如果后面安裝出現(xiàn)問題(比如:安錯(cuò)系統(tǒng),別問我怎么知道的...)想清空存儲(chǔ)卡可以先SD格式化再用diskgenius軟件恢復(fù)存儲(chǔ)卡系統(tǒng)分區(qū)。這部分網(wǎng)上很多參考,也很簡單。
2.?安裝arm64位操作系統(tǒng)
Index of /raspios_arm64/images/raspios_arm64-2021-05-28
下載.zip文件即可。
3.?燒錄系統(tǒng)
燒錄軟件也有很多選擇,我用的是官網(wǎng)的:
Raspberry Pi OS – Raspberry Pi
?至此燒錄完成。
?4.?初始化樹莓派
第一次使用的話會(huì)有一些設(shè)置選擇,可參考:
?如果想安裝一個(gè)中文輸入法,可參考:
樹莓派入門(八)—— 漢化Raspbian操作系統(tǒng)_bigmarshal的博客-CSDN博客_樹莓派漢化
輸入法需要reboot之后才生效。
5.?搭建EdgeX
按照大神的步驟可以開始搭建EdgeX了,為了整體流暢性,下面有一些步驟直接拷貝大神的
5.1 安裝docker-ce
$curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/raspbian/gpg | sudo apt-key add -
獲取docker-ce安裝包,下載下面三個(gè)文件并離線安裝:
https://download.docker.com/linux/debian/dists/buster/pool/edge/arm64/
$sudo dpkg -i containerd.io_1.3.7-1_arm64.deb
$sudo dpkg -i docker-ce-cli_19.03.13_3-0_debian-buster_arm64.deb
$sudo dpkg -i docker-ce_19.03.13_3-0_debian-buster_arm64.deb
設(shè)置docker鏡像:
$sudo cat /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
$sudo systemctl daemon-reload
$sudo systemctl restart docker
5.2 安裝docker-compose
$sudo apt-get install libffi-dev
$sudo apt-get install openssl
$pip3 install --default-timeout=100000 docker-compose
5.3 EdgeX搭建
根據(jù)官網(wǎng)的命令可以quick-start:
Quick Start - EdgeX Foundry Documentation
$curl https://raw.githubusercontent.com/edgexfoundry/developer-scripts/master/releases/geneva/compose-files/docker-compose-geneva-redis-no-secty-arm64.yml -o docker-compose.yml
此時(shí)報(bào)錯(cuò):
?直接搜索報(bào)錯(cuò)內(nèi)容說docker-compose沒有安裝成功,于是嘗試大神的解決方法:
$pip3 install --upgrade --default-timeout=100000 docker-compose -i http://pypi.douban.com/simple
出現(xiàn)新的錯(cuò)誤:
?這樣看是python版本不夠,不匹配pysistent。先嘗試升級(jí)python,遇到如下問題:
"sudo: /etc/sudoers is world writable\r\nsudo: no vaild sudoers sources found,quitting"
檢索錯(cuò)誤后發(fā)現(xiàn)原來是因?yàn)樵诎惭bdocker鏡像時(shí)為了寫入json文件和解決DNS污染問題而重寫入hosts中的raw.githubusercontent.com的IP地址(前面遇到的一個(gè)問題),而使得牽一發(fā)而動(dòng)全身。解決方法:
$pkexec chmod0440 /etc/sudoers
當(dāng)我以為可以簡簡單單升級(jí)python時(shí),天真了...
?升級(jí)也費(fèi)勁,同時(shí)發(fā)現(xiàn)pyrsistent竟然沒有,于是反向思維指定pyrsistent版本,并查看docker是否安裝,發(fā)現(xiàn)新的錯(cuò)誤:
docker.errors.DockerException:Error while fetching server API version:('Connection aborted'......
原因是docker_runner 沒有docker的運(yùn)行權(quán)限,解決方案:
vi /etc/group
...
docker:x:123:gitlab-runner
gitlab-runner:x:122:
...
至此,剛把獲取docker-compose.yml文件的命令的坑填好...
下面進(jìn)行EdgeX UI可視化,同時(shí)添加device-mqtt微服務(wù).
在docker_compose文件中有注釋mqtt,同時(shí)沒有ui命令, 需要取消相應(yīng)注釋,同時(shí)補(bǔ)充ui。參考大神的圖片:
本機(jī)輸入localhost:4000/可以看到前端展示了,登錄用本機(jī)賬戶和密碼即可。
?5.4 啟動(dòng)微服務(wù)
這一部分分為三個(gè)步驟:
1)北向基于 app-service-mqtt 實(shí)現(xiàn) EdgeXCore 的 device-random 服務(wù)
2)南向基于 DeviceDemo 模擬設(shè)備發(fā)送數(shù)據(jù)報(bào)文
3)南北打通
流程圖照搬大神的:
5.4.1 北向打通
這一步算是基礎(chǔ),因?yàn)楹竺娴暮芏鄬?shí)現(xiàn)步驟都類似,而且也是理解整體業(yè)務(wù)流程的第一步,所以值得好好琢磨一下。
首先安裝mosquitto:
$sudo apt-get install mosquitto
$sudo apt-get install mosquitto-clients
配置微服務(wù),可以新建一個(gè)docker-compose-less.yml,也可以在原來的yml文件里面粘貼。
app-service-mqtt:
image: edgexfoundry/docker-app-service-configurable-arm64:1.2.0
ports:
- "0.0.0.0: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: 172.17.0.1
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: EdgeXEvents
depends_on:
- consul
- data
啟動(dòng)mqtt broker(默認(rèn)port:1883):
$sudo mosquitto -v
啟動(dòng)mqtt微服務(wù):
$sudo docker-compose -f docker-compose-less.yml up app-service-mqtt
啟動(dòng)MQTT訂閱者:
$sudo mosquitto_sub -h 127.0.0.1 -p 1883 -t EdgeXEvents
5.4.2 南向打通
安裝golang環(huán)境,網(wǎng)上資料很多。
下載device demo程序:
$git clone https://github.com/edgexfoundry/device-mqtt-go
device demo需要修改的地方如下:
const (
?? ?brokerUrl ?= "127.0.0.1"
?? ?brokerPort = 1883
?? ?username ? = "admin"
?? ?password ? = "public"
)
func runCommandHandler() {
?? ?var mqttClientId = "DeviceCommandSubscriber"
?? ?var qos = 0
?? ?var topic = "CommandTopic"
func runDataSender() {
?? ?var mqttClientId = "DeviceIncomingDataPublisher"
?? ?var qos = byte(0)
?? ?var topic = "DataTopic"?? ?uri := &url.URL{
?? ??? ?Scheme: "tcp",
?? ??? ?Host: ? fmt.Sprintf("%s:%d", brokerUrl, brokerPort),
?? ??? ?User: ? url.UserPassword(username, password),
?? ?}?? ?client, err := createMqttClient(mqttClientId, uri)
?? ?defer client.Disconnect(5000)
?? ?if err != nil {
?? ??? ?fmt.Println(err)
?? ?}?? ?var data = make(map[string]interface{})
?? ?data["name"] = "device_name_1"
?? ?data["cmd"] = "randfloat32"
?? ?data["method"] = "get"?? ?for {
?? ??? ?data["randfloat32"] = rand.Float64()
?? ??? ?jsonData, err := json.Marshal(data)
?? ??? ?if err != nil {
?? ??? ??? ?fmt.Println(err)
?? ??? ?}
?? ??? ?client.Publish(topic, qos, false, jsonData)?? ??? ?fmt.Println(fmt.Sprintf("Send response: %v", string(jsonData)))
?? ??? ?time.Sleep(time.Second * time.Duration(15))
?? ?}}
?編譯device.go
go build
./mock
下載配置文檔 mqtt.test.device.profile.yml(博客最后給出文件)
在UI和consul上設(shè)置參數(shù)(見大神博客)
5.4.3 南北打通
啟動(dòng)兩個(gè)mqtt broker,以防之前的端口被占用,先查看一下:
ps -ef | grep mosquitto
如果有MQTT被占用,則kill it IPD
分別啟動(dòng)1883和1884的mqtt broker:
$sudo mosquitto -v
$sudo mosquitto -v -p 1884
啟動(dòng)docker-compose,并關(guān)閉device-random微服務(wù),并檢查容器此時(shí)開啟的服務(wù)
$sudo docker-compose up -d
$sudo docker stop edgx-device-random
$sudo docker-compose ps
啟動(dòng)北向app-service-mqtt以及mqtt訂閱者,注意此時(shí)的docer-compose里面的app-service-mqtt的port需要改為:1884
$sudo docker-compose -f docker-compose-less.yml up app-service-mqtt
$sudo mosquitto_sub -h 127.0.0.1 -p 1884 -t EdgeXEvents
啟動(dòng)南向:
go build
./mock
如果想查看docker中mqtt微服務(wù)的運(yùn)行情況?,可執(zhí)行:
$sudo docker-compose up device-mqtt
在UI上的配置操作和前面一樣,同樣參考大神博客即可,顯擺一張成功操作界面:)
命令行窗口的南北成功連接狀態(tài)也可參考大神博客,下面是我的一些隨便截圖:
?至此,基于EdgeX的簡單南北MQTT搭建成功~~歡迎一起探討~~
##mqtt.test.device.profile
name: "Test.Device.MQTT.Profile"
manufacturer: "Dell"
model: "MQTT-2"
labels:
- "test"
description: "Test device profile"
deviceResources:
- name: randfloat32
description: "device random number with Base64 encoding"
properties:
value:
{ type: "Float32", size: "4", readWrite: "R", defaultValue: "0.00", minimum: "100.00", maximum: "0.00", floatEncoding: "Base64" }
units:
{ type: "String", readWrite: "R", defaultValue: "" }
- name: randfloat64
description: "device random number with e notion"
properties:
value:
{ type: "Float64", size: "4", readWrite: "R", defaultValue: "0.00", minimum: "100.00", maximum: "0.00", floatEncoding: "eNotation" }
units:
{ type: "String", readWrite: "R", defaultValue: "" }
-
name: ping
description: "device awake"
properties:
value:
{ type: "String", size: "0", readWrite: "R", defaultValue: "oops" }
units:
{ type: "String", readWrite: "R", defaultValue: "" }
-
name: message
description: "device notification message"
properties:
value:
{ type: "String", size: "0", readWrite: "W" ,scale: "", offset: "", base: "" }
units:
{ type: "String", readWrite: "R", defaultValue: "" }
deviceCommands:
- name: testrandfloat32
get:
- { index: "1", operation: "get", deviceResource: "randfloat32"}
- name: testrandfloat64
get:
- { index: "1", operation: "get", deviceResource: "randfloat64"}
-
name: testping
get:
- { index: "1", operation: "get", deviceResource: "ping"}
-
name: testmessage
get:
- { index: "1", operation: "get", deviceResource: "message"}
set:
- { index: "1", operation: "set", deviceResource: "message"}
coreCommands:
- name: testrandfloat32
get:
path: "/api/v1/device/{deviceId}/testrandfloat32"
responses:
-
code: "200"
description: "get the random float32 value"
expectedValues: ["randfloat32"]
- code: "500"
description: "internal server error"
expectedValues: []
- name: testrandfloat64
get:
path: "/api/v1/device/{deviceId}/testrandfloat64"
responses:
- code: "200"
description: "get the random float64 value"
expectedValues: ["randfloat64"]
- code: "500"
description: "internal server error"
expectedValues: []
-
name: testping
get:
path: "/api/v1/device/{deviceId}/testping"
responses:
-
code: "200"
description: "ping the device"
expectedValues: ["ping"]
- code: "500"
description: "internal server error"
expectedValues: []
-
name: testmessage
get:
path: "/api/v1/device/{deviceId}/testmessage"
responses:
-
code: "200"
description: "get the message"
expectedValues: ["message"]
- code: "500"
description: "internal server error"
expectedValues: []
put:
path: "/api/v1/device/{deviceId}/testmessage"
parameterNames: ["message"]
responses:
-
code: "204"
description: "set the message."
expectedValues: []
- code: "500"
description: "internal server error"
expectedValues: []
原文鏈接:https://blog.csdn.net/m0_61835993/article/details/122129914
相關(guān)推薦
- 2022-07-21 Pandas文件讀寫操作
- 2022-03-12 用C語言實(shí)現(xiàn)圣誕樹(簡易版+進(jìn)階版)_C 語言
- 2022-12-22 Python使用imagehash庫生成ahash算法的示例代碼_python
- 2022-09-18 C++如何實(shí)現(xiàn)二叉樹鏈表_C 語言
- 2022-04-08 從頭學(xué)習(xí)C語言之字符串處理函數(shù)_C 語言
- 2022-02-14 linux系統(tǒng)之進(jìn)程管理詳解_Linux
- 2022-09-08 Go語言里切片slice的用法介紹_Golang
- 2022-06-02 jquery實(shí)現(xiàn)淘寶商品圖片局部放大_jquery
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支