本文讲述如何在树莓派3B+上编译edgex go版本代码,并运行edgex 微服务,达到在树莓派3B+上完成edgex API walkthrough测试的目标。
Edgex foundry是一个Linux 基金会运营的开源边缘计算物联网软件框架项目,该项目的核心是基于与硬件和操作系统完全无关的参考软件平台建立的互操作框架,使能即插即用的组件生态系统,统一市场,加速物联网方案的部署。EdgeX Foundry 使有意参与的各方在开放与互操作的物联网方案中自由协作,无论他们是使用公开标准或私有方案。详情请参看项目官方网站https://www.edgexfoundry.org/。
一台raspberry 3B +。早期的树莓派板子不支持,因为edgex需要运行在64位系统上。
一台PC用于搭建mqtt 代理服务器,安装ubuntu系统。
一台windows PC用于安装Postman
从https://github.com/Crazyhead90/pi64/releases/download/2018-04-17/Bamarni-desktop-full64bit-pi3bplus-V2.zip下载系统映像。将下载文件解压即可得到系统映像,然后参考https://blog.csdn.net/kxwinxp/article/details/78370913树莓派系统安装方式安装。
我们将在树莓派系统上编译edgex-go版本代码,需要先安装一些依赖包。
sudo apt update
sudo apt install build-essential git wget libzmq3-dev
系统没有安装ssh服务器和vim,执行以下命令安装
sudo apt install openssh-server vim-tinya
编译比较消耗内存,创建一个2G的交换分区:
sudo touch /tmp/theswap
sudo chmod 600 /tmp/theswap
sudo dd if=/dev/zero of=/tmp/theswap bs=1M count=2048
sudo mkswap /tmp/theswap
sudo swapon /tmp/theswap
安装go和golide环境
从源码安装以确保安装是指定版本:
wget https://dl.google.com/go/go1.10.1.linux-arm64.tar.gz
sudo tar -C /usr/local -xvf go1.10.1.linux-arm64.tar.gz
cat >> ~/.bashrc << 'EOF'
export GOPATH=$HOME/go
export PATH=/usr/local/go/bin:$PATH:$GOPATH/bin
EOF
source ~/.bashrc
安装Glide:
curl https://glide.sh/get | sh
获取源码和依赖包:
go get github.com/edgexfoundry/edgex-go
编译源码:
cd ~/go/src/github.com/edgexfoundry/edgex-go
make prepare
make build
EdgeX 使用MongoDB作为本地存储的数据库。
安装MongoDB:
sudo apt install mongodb-server
确认已运行:
systemctl status mongodb
初始化数据库:
wget https://github.com/edgexfoundry/docker-edgex-mongo/raw/master/init_mongo.js
sudo -u mongodb mongo < init_mongo.js
cd ~/go/src/github.com/edgexfoundry/edgex-go/bin
./edgex-launch.sh
若没有错误log打印在控制台,至此edgex在树莓派上已经运行起来。
Postman是edgex官方推荐的restful api调试工具。https://www.getpostman.com/apps下载适合自己系统的安装包并安装。
Mosquitto是一个实现了MQTT3.1协议的代理服务器,本例中用来转发edgex导出的mqtt消息。找一台PC,安装mosquitto。以ubuntu 系统为例:
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquito
sudo service mosquitto status
本例使用最简单的配置用于测试,修改mosquitto配置文件/etc/mosquitto/mosquitto.conf,将mosquitto端口配为1883,使能匿名登录。mosquitto.conf一份例子如下:
mosquitto -c /etc/mosquitto/mosquitto.conf
在运行mosquitto代理服务的PC上使用mosquitto_sub命令创建一个订阅者,订阅a/b/c主题的mqtt消息,edgex将以a/b/c主题导出数据:
mosquitto_sub -t a/b/c -q 1
参考https://docs.edgexfoundry.org/Ch-Walkthrough.html进行API walkthrough测试,将例子API中localhost改为树莓派3B+的IP地址,比如
http://localhost:48081/api/v1/addressable 修改为http://192.168.0.115:48081/api/v1/addressable。
在Register an Export Client时需要将mqtt broker配置为上文搭建的mosquitto代理服务器,MQTT topic配置为 a/b/c。
POST to http://192.168.0.115:48071/api/v1/registration
BODY: {"name":"MyMQTTTopic","addressable":{"name":"MyMQTTBroker","protocol":"TCP","address":"39.106.61.190","port":1883,"publisher":"EdgeXExportPublisher",
"user":"hukfgtoh","password":"mypass","topic":"a/b/c"},"format":"JSON","enable":true,"destination":"MQTT_TOPIC"}
其中39.106.61.190是运行mosquitto代理服务PC的IP。至此整个测试环境搭建完成。
在postman中发送如下命令:
POST to http://localhost:48080/api/v1/event
BODY: {"device":"countcamera1","readings":[{"name":"humancount","value":"5"},{"name":"caninecount","value":"3"}]}
可以在mqtt订阅者日志中看到收到消息,格式如下:
https://www.hackster.io/mhall119/running-edgex-on-a-raspberry-pi-d35dd5#toc-get-the-edgex-foundry-source-code-5
https://docs.edgexfoundry.org/Ch-Walkthrough.html
https://blog.csdn.net/kxwinxp/article/details/78370913
https://github.com/edgexfoundry/edgex-go/blob/master/docs/getting-started/Ch-GettingStartedGoDevelopers.rst