po 主是苦逼碼農(nóng)一枚趴久,軟件工程師廷臼,做服務(wù)端方向蝗柔,主要寫 nodejs & golang 偶爾寫點(diǎn) python,所以這是一篇剛剛擁有第一個開發(fā)板的小白寫的小白文情龄,以上是背景
前段時間為了改善生活買了一塊樹莓派3迄汛,裝了 debian 8 jessie捍壤。
1. 換清華的源:
sudo vi /etc/apt/sources.list
注釋掉其他的源插入這兩個:
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ jessie main non-free contrib
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ jessie main non-free contrib
同時注釋掉下面這個文件里面的源,不然還是會優(yōu)先讀這個
sudo vi /etc/apt/sources.list.d/raspi.list
2. 使用 ssh-tunnel 做內(nèi)網(wǎng)穿透 (遠(yuǎn)程控制自己家里的樹莓派):
主要參照的是 這篇文章 鞍爱,大致思路如下
利用 ssh 反向隧道的方式鹃觉,通過一臺外網(wǎng) vps 做跳板,遠(yuǎn)程直接通過 ssh 連接到放在 NAT 之后的樹莓派
有兩個奇妙的點(diǎn)
- 使用 autossh 保持 ssh 隧道硬霍,因?yàn)?ssh 會超時斷開帜慢,所以用 autossh 來保持連接
- 實(shí)測發(fā)現(xiàn) autossh 的 -f 后臺模式有點(diǎn)問題,暫時通過 tmux 保證這個 autossh 持續(xù)運(yùn)行唯卖,接下來準(zhǔn)備做成系統(tǒng)服務(wù)開機(jī)啟動
具體步驟
準(zhǔn)備:
1. 將樹莓派中的 id_rsa.pub 公鑰放進(jìn) vps粱玲,讓樹莓派 ssh 訪問 vps 的時候可以不用輸入密碼
2. 安裝 autossh (sudo apt-get install autossh || sudo yum install autossh)
3. 安裝 tmux (sudo apt-get install tmux || sudo yum install tmux)
運(yùn)行:
# --------------- 樹莓派上運(yùn)行
tmux
autossh -p 22 -M 6777 -NR 6766:localhost:22 -i /home/pi/.ssh/id_rsa [vps user]@[vps ip]
# 6777 監(jiān)控服務(wù)狀態(tài)
# vps 6766 端口映射到樹莓派的 22 端口
# -i 是指定 id_rsa 文件路徑,避免不同用戶運(yùn)行造成的 ssh 密鑰錯誤
# --------------- 需要遠(yuǎn)程連接樹莓派的終端上運(yùn)行
ssh [vps user]@[vps ip]
# 先連上 vps
ssh -p 6766 [pi user]@localhost
# 在 vps 上連接本地的 6766 端口拜轨,作用等效于連上映射的樹莓派 22 端口
3. 連接 SIM 900
淘寶鏈接就不放了抽减,不過就算做硬廣應(yīng)該也沒人看見吧 (手動滑稽
大致思路如下
Raspberry pi 3 GPIO Pins 里面的 8/10 口連 SIM 900 的 TTL 接口,使用 minicom 監(jiān)控 SIM 900 的輸出橄碾,調(diào)試的時候可以直接向串口里面寫 AT 指令
這是最坑的一個步驟卵沉,因?yàn)榫W(wǎng)上大部分的資料都是說的 pi 3 早期系統(tǒng),或者直接就是 pi 2法牲。
根史汗!本!就拒垃!沒停撞!用!(來自小白浪費(fèi)兩天時間的咆哮
碰到的問題如下:
pi 3 集成了藍(lán)牙悼瓮,占據(jù)了主要的 uart 口戈毒,所以沒法兒像 pi 2 一樣直接使用 TTL 需要的 TXD & RXD。
-
網(wǎng)上大部分現(xiàn)有的文檔横堡,都是在教大家怎么禁用藍(lán)牙模塊埋市,然后將 ttyAMA0 重新映射到 TXD & RXD 口上。
但是命贴!我想用藍(lán)牙暗勒!為啥我要放棄藍(lán)牙P刂搿(其實(shí)是嘗試了好幾種方法都不成功萬般無奈
有一種方法是將內(nèi)核頻率降低 core_freq=250培己,用來避免頻率波動,從而順利使用 uart1 接口胚泌,不過也是犧牲了一定的性能。
禁用藍(lán)牙模塊是有效的肃弟,推薦看 這篇文章 反正我是嘗試失敗了才推薦給大家的玷室。
重頭戲來了
看了這幾篇之后 帖一 帖二 帖三 才有點(diǎn)明白 pi 3 里面 uart 接口的關(guān)系零蓉,主要是二、三穷缤,引用一段:
The Broadcom SOC's in all Pi's come with 2 serial ports, a full port and a mini port. Before the model 3, neither of theses were used internally, so the full port was routed out to the GPIO connector, and called ttyAMA0.
The Pi 3 requires a serial port to talk to it's Bluetooth device, and the Pi's engineers decided to use the full serial port to talk to the Bluetooth chip as it was more capable then the mini serial port, and so the mini serial port was brought out to the GPIO connector, and called ttyS0. This has caused a few problems.
The mini serial port's baud rate is hard wired to the GPU's clock, and so when the GPU's clock speed changed, the baud rate of ttyS0 changed as well. The Pi 3 adjusts it's clock frequencies as demand goes up and down, and with it, ttyS0's baud rate. This is probably what caused the scrambled data in your first post.
The first fix for this problem was to lock down the GPU's clock to a fixed frequency. This was done by adding the line: "core_freq=250" to 'config.txt'. This locks the baud rate of ttyS0, at the cost of a small decrease in the Pi's performance.
The second fix the Pi engineers came up with, was the problem of software backward compatibility with previous model Pi's. Any software written using ttyAMA0 would not work on a Pi 3B, as it calls it's port ttyS0. The second fix was to rename the serial ports on all models to 'serial0', and 'serial1'. On a Pi 3, ttyS0 is mapped to serial0, and on previous models ttyAMA0 is mapped to serial0. This means that software which refers to serial0 will work on all models.
Finally, the line "enable_uart=1" automatically locks the core frequency in a Pi 3, so "core_freq=250" is no longer required. Enabling the serial port in "Preferences" (raspi-config) will automatically add this line if required.
So, to summarize, to use the serial port on all model Pi's running an updated OS, add the line: "enable_uart=1" to '/boot/config.txt, and refer to the port as '/dev/serial0' in code. SystemD normally starts a terminal on this serial port which you are using but, if you don't want a terminal remove the section "console=serial0,115200" from '/boot/cmdline.txt'.
Using this procedure, Bluetooth works normally on the Pi 3.
簡而言之就是:
pi 3 現(xiàn)在是有兩個 uart 口的敌蜂,主要的 uart 給了藍(lán)牙模塊,所以正常來說 ttyAMA0 也就是 serial1 是不推薦使用的津肛。
推薦使用 pi 3 的 mini-uart章喉,而且是可以不用手動降頻使用的,因?yàn)樾掳姹纠锩嫒绻蜷_了 uart 口身坐,其實(shí)是會鎖定內(nèi)核頻率的秸脱。我們要連的還是 RXD TXD,只不過 pi 3 里面這兩個口被映射到了 mini-uart (ttyS0 | serial0)部蛇。
在系統(tǒng)設(shè)置里面 enable serial port摊唇,或者把 enable_uart=1 加到 /boot/config.txt 里面碌廓,mini-uart 就可以正常訪問了斗这。
po 主直接把 minicom 的串口設(shè)為 '/dev/serial0' 也就是 mini-uart糊识,波特率不變還是 115200 8N1剧董,可以達(dá)到 pi 2 里面使用 ttyAMA0 類似的效果通砍。
po 主的流程
接線:# 不確定后續(xù)撥打電話之類的夠不夠深浮,現(xiàn)在是為了省一個外接電源秕硝,所以直接用樹莓派給 SIM900 供電
TTL
pi TXD(8) 連 SIM900 RXD
pi RXD(10) 連 SIM900 TXD
POWER
pi 5V(2) 連 SIM900 VCC
pi GND(6) 連 SIM900 GND
樹莓派:
# 安裝 minicom 用來監(jiān)控 uart 口
sudo apt-get install minicom
# 調(diào)整響應(yīng)的端口暴浦,并且關(guān)閉硬件監(jiān)控警绩,以便于之后可能需要用 minicom 輸入
sudo minicom -s
Serial port setup
Serial Device 改為 /dev/serial0
Hardware Flow Control 改為 No
Save setup as dfl
Exit from Minicom
# 調(diào)整完畢崇败,可以直接看到輸出了
minicom
2017/06/22 更新
連上 minicom 一直不停的有亂碼出現(xiàn)。檢查了波特率房蝉,而且SIM900本身也是自適應(yīng)的僚匆,心酸。
2017/07/03 更新
買了一根 USB 轉(zhuǎn) TTL搭幻,接上樹莓派馬上就可以用了咧擂。。檀蹋。minicom 也能自動識別了松申,我是傻逼嗎!
PS 還買了邏輯分析儀俯逾,果然也沒派上用場贸桶。因?yàn)橛?virtualbox 裝windows 好像邏輯分析儀的采樣率出了點(diǎn)問題,暫時就不用了桌肴。
未完待續(xù)
- 接下來要完善 autossh 穩(wěn)定內(nèi)網(wǎng)穿透皇筛。
- 在 vps 上再搞一個隧道(開個口,文章的說法)做到可以遠(yuǎn)程直接連 vps 固定端口就轉(zhuǎn)到了 pi 3 上坠七,不用現(xiàn)在的這種先 ssh 到 vps 上水醋,再 ssh 到 pi 3旗笔。
- 在 SIM 900 上搞點(diǎn)事情