輕松使用阿裡雲完成智能小車
智能小車,自娛自樂,希望通過該分享幫助更多愛好者們化繁為簡,待我完成到第三季,我将會把程式放出來拱大家使用,我準備分三季來實作,APP背景部署在阿裡小程式雲,通過阿裡雲MQTT消息隊列服務,來完成指令控制,并實作事件日志機制。
第一季實作小車的控制目标
1、實作小車4驅控制
2、實作攝像頭實時檢視,借助反向代理來完成内外網的通信
3、實作指令通過阿裡雲微消息隊列MQTT來互動
4、設計APP端的控制界面實作
第二季實作目标
1、實作攝頭雲端控制
2、人臉檢測
3、打包容器
4、熱體紅外+超音波測距+濕度溫度
第三季實作目标
1、思考中
2、第一季開發準備
3、硬體
- 樹莓派3b+
- 小車底座
- L298N電機驅動子產品
- 杜邦線
- 充電寶
- 6v~50V電源
- 樹莓派專用攝像頭 Raspberry Pi Camera
- 阿裡小程式雲應用
- 支付寶小程式開發者工具
整體效果
1.樹莓派上電,将充電寶用usb線将樹莓派于其連接配接,可看到樹莓派紅色訓示燈常亮,綠色訓示燈閃爍。如果發現紅色訓示燈忽然熄滅或者亮點很低,請檢查充電寶輸出電壓以及電流是否太低
2.插入樹莓派攝像頭,注意不要插反,排線金屬側正對插排口金屬側,插入後扣緊排口
3.gpio與電機驅動子產品in口連接配接。這邊我們選擇GPIO5、GPIO6、GPIO12、GPIO13、GPIO19、GPIO16、GPIO26、GPIO20 (GIPIO與針腳值不一緻,對應針腳位置請參考下圖,針腳39,40的位置在USB口旁邊) 與驅動子產品的IN1~IN8連接配接 (連接配接順序請自行調整,驅動子產品IN1的數字量輸入控制OUT1輸出,OUT1-OUT2如果連接配接同一個電機的輸入和輸出端,那麼IN1-IN2對應輸入數字信号[1,0]則代表前進,[0,1]代表後退,[0,0]或者[1,1]代表停止,以此類推IN1~IN8,OUT1~OUT8)
雲應用是面向小程式應用場景,為開發者提供的一鍵建構後端應用運作環境、後端服務部署、運維監控等能力的一站式小程式部署服務。
我們需要申請一個雲應用充當小程式的伺服器以及fpc伺服器。現在阿裡推出繁星計劃,可以免費申請2個月的測試環境以及一個月的生産環境,無需申請,直接在
應用界面建立新運用,建立詳情請參考
文檔在
控制台擷取伺服器ip、使用者名(預設root)、密碼(需要修改預設密碼,然後重新開機伺服器),通過SSH工具連接配接,用于部署下面步驟
MJPG-Streamer是一個指令行應用程式,它将JPEG幀從一個或多個輸入插件複制到多個輸出插件。它可用于通過基于IP的網絡将JPEG檔案從網絡攝像頭流式傳輸到各種類型的檢視器,如Chrome,Firefox,Cambozola,VLC,mplayer和其他能夠接收MJPG流的軟體。
-
開啟樹莓派攝像頭
樹莓派攝像頭總線是預設沒開啟的,請在指令行中開啟,進入後,選擇
項,再選擇camera
,然後重新開機enable
sudo raspi-config
2.安裝必要的庫
sudo apt-get update
sudo apt-get install subversion
sudo apt-get install libjpeg8-dev
sudo apt-get install imagemagick
sudo apt-get install libv4l-dev
sudo apt-get install cmake
sudo apt-get install git
3.git開源的project到本地,編譯
sudo git clone https://github.com/jacksonliam/mjpg-streamer.git
cd mjpg-streamer/mjpg-streamer-experimental
make all
sudo make install
4.解決V4L driver不支援的問題
sudo wget http://www.linux-projects.org/listing/uv4l_repo/lrkey.asc &&
sudo apt-key add ./lrkey.asc
5.修改sources.list,在末尾添加源,并安裝uv4l uv4l-raspicam,然後重新開機
sudo nano /etc/apt/sources.list
#末尾添加,儲存
deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/ wheezy main
sudo apt-get update
sudo apt-get install uv4l uv4l-raspicam
sudo reboot
6.重新開機之後:
sudo pkill uv4l
sudo apt-get update
sudo apt-get install uv4l-uvc
sudo apt-get install uv4l-xscreen
sudo apt-get install uv4l-mjpegstream
sudo reboot
7.再一次重新開機之後開啟視訊流服務
sudo modprobe bcm2835-v4l2
./mjpg_streamer -i "./input_uvc.so -d /dev/video0 -n -y -f 25 -r 640x480" -o "./output_http.so -n -w /usr/local/www"
8.然後打開浏覽器輸入:
http://raspberry-ip-address:8080/?action=stream應該就可以看到Raspberry Pi Camera采集的動态視訊了,延遲大概不超過0.8秒吧。
frp是一個快速反向代理,可幫助您将NAT或防火牆後面的本地伺服器暴露給Internet。截至目前,它支援tcp&udp以及http和https協定,其中請求可以通過域名轉發到内部服務。
-
服務端部署 frps
在購買的服務端(有公網IP)部署frps,用于穿透樹莓派本地的視訊流
wget --no-check-certificate https://raw.githubusercontent.com/clangcn/onekey-install-shell/master/frps/install-frps.sh -O ./install-frps.sh
chmod 700
./install-frps.sh
./install-frps.sh install
然後一直回車(預設配置),知道全部安裝完成。安裝成功截圖
2.啟動frps
frps start
檢視frps版本号
frps -v
我這邊安裝的是
version 0.20.0
,用戶端也要安裝對應版本
3.樹莓派部署frpc
由于我們伺服器部署frps的版本是
version 0.20.0
,是以我們本地也要部署相同的版本
wget https://github.com/fatedier/frp/releases/download/v0.20.0/frp_0.20.0_linux_arm.tar.gz
tar zxvf frp_0.20.1_linux_arm.tar.gz
cd frp_0.14.1_linux_arm
vi frpc.ini
修改 frpc.ini 檔案
# frpc.ini
[common]
#伺服器IP
server_addr = x.x.x.x
#伺服器fpcs服務對應端口Bind port
server_port = 5443
#服務端token
oken = 你的服務端token
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
#[]内名稱不能重複,标簽作用
[web_raspberry_web]
type = http
#端口号 對應本機視訊web伺服器的端口
local_port = 8080
#配置穿透外網通路域名,可設定多個
custom_domains = raspberry.你的域名.com
修改好frpc.ini後,啟動frpc用戶端。如果想在背景運作,可在指令後加
&
./frpc -c frpc.ini
将你的的域名主機記錄
raspberry
A 記錄解析到你的伺服器IP x.x.x.x
Nginx (engine x) 是一個高性能的HTTP和反向代理web伺服器,同時也提供了IMAP/POP3/SMTP服務。
Ubuntu16.04安裝及配置nginx
- 安裝gcc g++的依賴庫
apt-get install build-essential
apt-get install libtool
- 安裝pcre依賴庫
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
- 安裝zlib依賴庫
apt-get install zlib1g-dev
4.安裝ssl依賴庫
apt-get install openssl
安裝nginx
#下載下傳最新版本:
wget http://nginx.org/download/nginx-1.16.0.tar.gz
#解壓:
tar -zxvf nginx-1.11.3.tar.gz
#進入解壓目錄:
cd nginx-1.11.3
#配置:
./configure --prefix=/usr/local/nginx
#編輯nginx:
make
注意:這裡可能會報錯,提示“pcre.h No such file or directory”,具體詳見:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory
需要安裝 libpcre3-dev,指令為:sudo apt-get install libpcre3-dev
#安裝nginx:
sudo make install
#啟動nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置檔案的路徑,不加的話,nginx會自動加載預設路徑的配置檔案,可以通過 -h檢視幫助指令。
#檢視nginx程序:
ps -ef|grep nginx
在浏覽器輸入
http://raspberry.你的域名.com:8080
出現頁面則nginx部署成功
建立一個
index.html
檔案,用于測試頁面,内容如下
<html>
<head>
<meta name="generator"
content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" />
<meta http-equiv="Content-Type" content="text/html;" charset="UTF-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
<meta http-equiv="refresh" content="3" />
<title>樹莓派實時監控站</title>
</head>
<body>
<center>
<font size="20" face="微軟雅黑" color="#0074CD">
<b>樹莓派<br>
實時監控站</b>
</font>
<hr />
frameLabelStart--frameLabelEnd <!--修改此處-->
<br />
<p>
<font size="5" color="#269C5D">
<b>Design by James</b>
</font>
</p>
<p>
<font size="5" color="#269C5D">
<i>a Web Monitoring Based on Raspberry Pi</i>
</font>
</p>
</center>
</body>
</html>
将該檔案替換在nginx配置的web路徑的index.html檔案,例如
/var/www/index.html
嘗試一下能否通路,如果成功出現樹莓派實時監控頁面,則web頁面部署成功
微消息隊列 MQTT 是阿裡雲推出的一款面向移動網際網路以及物聯網領域的輕量級消息中間件,針對移動網際網路以及物聯網 IoT 場景的消息傳輸特點,支援了包括 MQTT、STOMP、GB-808、新能源國标等主流通信協定
-
建立服務
打開
MQTT控制台 ,需要登入阿裡雲賬号,若沒賬号請自行申請。登入後請根據阿裡雲 mqtt說明書 建立mqtt服務。建立完畢後如下圖,主要用到參數有 執行個體id、公網接入點、消息存儲執行個體、Topic、Group
海創開發平台是海創科技有限公司開發的一款快速程式設計平台,該平台開發的元件能快速與PLC、GPIO、各式裝置進行通訊,有豐富的API支援,基本你們想到的事情它都能幹,非常适合做物聯網的網關控制中心
-
導入程式
打開海創物聯開發平台的控制頁
http:// 樹莓派ip:1880/#flow/fd2b8cdb.5b63b
複制以下代碼,打開開發平台,點選右上角菜單,選擇導入-剪切闆,然後粘貼儲存,即可看到工作區新增了一個新的流程,該流程用于接受MQTT指令控制小車
[{"id":"9e9c968e.24e348","type":"rpi-gpio out","z":"d99269c6.a2d0b8","name":"","pin":"29","set":true,"level":"0","freq":"","out":"out","x":720,"y":140,"wires":[]},{"id":"37f46ac8.666436","type":"rpi-gpio out","z":"d99269c6.a2d0b8","name":"","pin":"31","set":true,"level":"0","freq":"","out":"out","x":720,"y":180,"wires":[]},{"id":"5f130d8f.f55d84","type":"function","z":"d99269c6.a2d0b8","name":"控制","func":"switch(msg.payload)\n{\n case '0':return [{payload:0},{payload:0},{payload:0},{payload:0},{payload:0},{payload:0},{payload:0},{payload:0}];\n case '1':return [{payload:0},{payload:1},{payload:0},{payload:1},{payload:0},{payload:1},{payload:0},{payload:1}];\n case '2':return [{payload:1},{payload:0},{payload:1},{payload:0},{payload:1},{payload:0},{payload:1},{payload:0}];\n case '4':return [{payload:0},{payload:0},{payload:0},{payload:0},{payload:1},{payload:0},{payload:1},{payload:0}];\n case '3':return [{payload:1},{payload:0},{payload:1},{payload:0},{payload:0},{payload:0},{payload:0},{payload:0}];\n}\n","outputs":8,"noerr":0,"x":510,"y":320,"wires":[["9e9c968e.24e348","676a7be6.b97e14"],["37f46ac8.666436","676a7be6.b97e14"],["2e8cc1ec.6369ce","676a7be6.b97e14"],["c7700a0b.47b378","676a7be6.b97e14"],["676a7be6.b97e14","482e8885.76d548"],["676a7be6.b97e14","62b4d0fc.a93f3"],["676a7be6.b97e14","fd3c406.31fc4c"],["676a7be6.b97e14","6ac88490.ba21bc"]]},{"id":"2e8cc1ec.6369ce","type":"rpi-gpio out","z":"d99269c6.a2d0b8","name":"","pin":"32","set":true,"level":"0","freq":"","out":"out","x":720,"y":240,"wires":[]},{"id":"c7700a0b.47b378","type":"rpi-gpio out","z":"d99269c6.a2d0b8","name":"","pin":"33","set":true,"level":"0","freq":"","out":"out","x":720,"y":280,"wires":[]},{"id":"8e757771.a599e8","type":"inject","z":"d99269c6.a2d0b8","name":"","topic":"停止","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":260,"wires":[["5f130d8f.f55d84"]]},{"id":"89e33a84.467878","type":"inject","z":"d99269c6.a2d0b8","name":"","topic":"前進","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":320,"wires":[["5f130d8f.f55d84"]]},{"id":"e521ba16.452cc8","type":"inject","z":"d99269c6.a2d0b8","name":"","topic":"後退","payload":"2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":380,"wires":[["5f130d8f.f55d84"]]},{"id":"676a7be6.b97e14","type":"debug","z":"d99269c6.a2d0b8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":580,"wires":[]},{"id":"482e8885.76d548","type":"rpi-gpio out","z":"d99269c6.a2d0b8","name":"","pin":"35","set":true,"level":"0","freq":"","out":"out","x":720,"y":340,"wires":[]},{"id":"62b4d0fc.a93f3","type":"rpi-gpio out","z":"d99269c6.a2d0b8","name":"","pin":"36","set":true,"level":"0","freq":"","out":"out","x":720,"y":380,"wires":[]},{"id":"fd3c406.31fc4c","type":"rpi-gpio out","z":"d99269c6.a2d0b8","name":"","pin":"37","set":true,"level":"0","freq":"","out":"out","x":720,"y":440,"wires":[]},{"id":"6ac88490.ba21bc","type":"rpi-gpio out","z":"d99269c6.a2d0b8","name":"","pin":"38","set":true,"level":"0","freq":"","out":"out","x":720,"y":480,"wires":[]},{"id":"6473261a.673168","type":"inject","z":"d99269c6.a2d0b8","name":"","topic":"左轉","payload":"3","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":440,"wires":[["5f130d8f.f55d84"]]},{"id":"73eb851c.b88ddc","type":"inject","z":"d99269c6.a2d0b8","name":"","topic":"右轉","payload":"4","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":480,"wires":[["5f130d8f.f55d84"]]},{"id":"bf85c267.64f96","type":"mqtt in","z":"d99269c6.a2d0b8","name":"","topic":"","qos":"2","broker":"bf40108b.620c2","x":235,"y":160,"wires":[["5f130d8f.f55d84","8a1c007d.34912"]]},{"id":"8a1c007d.34912","type":"debug","z":"d99269c6.a2d0b8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":435,"y":180,"wires":[]},{"id":"c1f327d8.b01818","type":"inject","z":"d99269c6.a2d0b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":40,"wires":[["c4d9812a.7b54b"]]},{"id":"c4d9812a.7b54b","type":"mqtt out","z":"d99269c6.a2d0b8","name":"","topic":"","qos":"","retain":"","x":415,"y":40,"wires":[]},{"id":"bf40108b.620c2","type":"mqtt-broker","z":"","name":"","broker":"","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
導入後效果如下
-
修改節點屬性
導入的流程不能直接使用,需要修改一些節點屬性配置。
輕按兩下mqtt out節點,在屬性窗點選伺服器右側的那個小标簽按鈕進入mqtt服務配置。填好伺服器(阿裡mqtt執行個體的外網http接入點)、端口号(預設1883)、用戶端id(
"Group ID@@@任意id"
用戶端ID在每個執行個體每個主題中隻能唯一,重複情況下後面連接配接的自動被阿裡斷開)、使用者名(這邊我們使用的是簽名鑒權模式,Token鑒權模式參考
"Signature|
Access Key
|
執行個體 ID
")、密碼(參考阿裡的
簽名機制,快捷生成簽名請點選
簽名校驗),點選儲存傳回mqtt屬性配置頁,填寫主題(阿裡的Topic ID),QOS選擇
1
(至少上傳一次)。mqtt in節點類似,由于接受消息是用于控制小車,是以QOS選擇
2
(僅分發一次),以下是配置詳情
PIN控制着電機驅動子產品進而控制小車的動作,前進(四個輪子前進)、後退(四個輪子後退)、左轉(右側輪子轉)、右轉(左側輪子轉)。假設相鄰的兩個OUT口連接配接一個電機,那麼驅動子產品的控制邏輯為
IN1 | IN2 | ~ |
---|---|---|
數字信号 | 1 | |
電機 | 前進 | |
後退 | ||
停止 | ||
根據該邏輯配置function節點(示例已包含,請根據自己的接法設定GPIO口的信号輸出)
-
部署調試
點選菜單欄的部署按鈕對程式部署,程式正式運作
單擊定時器後mqtt out節點會将測試使用的時間戳上傳到阿裡的mqtt伺服器,然後mqtt in會訂閱接受到該消息,并在右側調試視窗輸出。正式使用資料請輸入
、
1
2
3
使用mqtt發送正式消息,
操作結果