# Raspberry Pi 紀錄
###### tags: `software` `electrical_system` `NTURT`
[NTU ME 20190227 - HackMD](https://hackmd.io/2vZnbo9rSRGnNQhpTUtQAA?view)
[VS Code 使用紀錄](https://factual-chameleon-835.notion.site/VS-Code-e0f77315cda443ecb0467595c268c19a)
[Linux使用紀錄](https://www.notion.so/Linux-361153cd39084995ab5440debb3b12a1)
[Shell Script](https://www.notion.so/Shell-Script-744f97e8a5594d90bbfe874983acec38)
[Raspberry PI - increase swap size](https://wpitchoune.net/tricks/raspberry_pi3_increase_swap_size.html)
---
# 系統安裝
## **安裝 Raspbian 系統**
> [[樹莓派][Raspberry Pi] 樹莓派無頭式安裝教學(一)- wifi + ssh](http://khanwhlee.blogspot.com/2017/05/wifi-ssh.html)
## How to install Ubuntu on Raspberry Pi 4
> [How to install Ubuntu Desktop on Raspberry Pi 4 | Ubuntu](https://ubuntu.com/tutorials/how-to-install-ubuntu-desktop-on-raspberry-pi-4#1-overview)
適用任何映像檔
# 系統設定
## **檢查硬碟使用量 df 指令教學與指令稿範例**
> [Linux 檢查硬碟使用量 df 指令教學與指令稿範例 - G. T. Wang](https://blog.gtwang.org/linux/linux-df-command-check-disk-space-usage-tutorial-script-example/)
## **把 Raspberry Pi 變成中文介面、安裝中文輸入法**
> [【樹莓派教學#3】把你的 Raspberry Pi 變成中文介面、安裝中文輸入法 - TechApple.com](https://www.techapple.com/archives/17272)
## **Raspberry Pi 在命令列設定 WiFi 無線網路**
> [樹莓派 Raspberry Pi 在命令列設定 WiFi 無線網路的指令教學 - G. T. Wang](https://blog.gtwang.org/iot/raspberry-pi/raspberry-pi-wireless-configuration-command-tutorial/)
> [[基礎] 命令列設置無線網路 - Raspberry Pi台灣樹莓派](https://www.raspberrypi.com.tw/2152/setting-up-wifi-with-the-command-line/)

## 設定開機自動執行 Python 程式
>[在樹莓派 (Raspberry Pi)上設定開機自動執行 Python 程式](https://medium.com/@chungyi410/在樹莓派-raspberry-pi-上設定開機自動執行-python-程式-56ccf89cffc5)
備註: `/home/pi/inv/mqtt-ledv4-test.py` Python 程式的第一行需為 `#!/usr/bin/python3` ,並且需將 `mqtt-ledv4-test.py` 檔案設定為可以執行。
```
chmod +x /home/pi/inv/mqtt-ledv4-test.py
```
設定完成後,可以用指令 `sudo service mypython start` 測試設定是否正常。正常的話,就可以重新開機後,再用指令 `ps -ax|grep python` 檢查程式是否有自動執行。
>[將成品帶到學校Demo----開機自動執行](https://blog.xuite.net/quickroc/blog/383991319)
```bash
sudo crontab -e
```
尾端加入
```bash
@reboot python /home/pi/inv/mqtt-ledv4-test.py &
```
要怎麼讓它停止呢
在文字模式下鍵入以下指令:
```bash
ps aux | grep python3
```
例如:找到前方的執行緒編號381
sudo kill 381
## 安裝 Python3
```bash=
$ sudo apt-get update
$ sudo apt-get install python3
$ sudo apt-get install python3-pip
$ sudo pip3 install pyserial
$ pip3 install --user --upgrade pip
$ pip3 install numpy
```
### 移除 Python3
```bash=
$ dpkg —list
$ sudo apt-get --purge remove python3
```
# 跟 RPi 連線
## 使用網路線讓電腦與樹莓派進行連線(For Mac OS)
[**使用網路線讓電腦與樹莓派進行連線
(For Mac OS)**](https://www.notion.so/For-Mac-OS-091f554e14604bebbf51ac30e44ae391)
## **SSH失效解決方法**
> [SSH連線出現錯誤 WARNING REMOTE HOST IDENTIFICATION HAS CHANGED - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天](https://ithelp.ithome.com.tw/articles/10083004)
> [SSH連現時出現「WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!」解決辦法 @ 一段輝煌的回憶,一個冬日的故事 :: 痞客邦 ::](https://cometlc.pixnet.net/blog/post/5453854)
```bash
ssh-keygen -R 遠端伺服器的IP或網址
```
## 使用USBtoTTL讓電腦與樹莓派進行連線
(For Mac OS)
[使用USBtoTTL讓電腦與樹莓派進行連線
(For Mac OS)](https://www.notion.so/USBtoTTL-For-Mac-OS-24179104690d492398efb9ba295926eb)
# 檔案傳輸
## 使用FileZilla與樹莓派互傳檔案
[使用FileZilla與樹莓派互傳檔案](https://www.notion.so/FileZilla-887bea713c7742be8af0674c38ea4319)
## **將檔案複製到電腦桌面**
輸入
```bash
$ scp run_this_0731.py AlexKao@192.168.2.101:/Users/AlexKao/Desktop
```
即可將 `run_this_0731.py` 檔案複製到電腦桌面
# **備份SD card**
[**備份SD card**](https://www.notion.so/SD-card-e871a70aac82447d85f647befcb88d3e)
# **查看USB埠狀態**
> [树莓派usb端口固定并绑定](https://www.mscto.com/op/372394.html)
> [【动手实践】树莓派USB端口绑定方法 by xinlong](https://zhuanlan.zhihu.com/p/110289208)
> [[基礎] 命令列設置無線網路 - Raspberry Pi台灣樹莓派](https://www.raspberrypi.com.tw/2152/setting-up-wifi-with-the-command-line/)
```bash
$ ls /dev/tty*
```
```bash
$ lsusb
```
# **GPS資料格式**
> [【電腦】NMEA標準格式 @ 銀質空間 :: 痞客邦 ::](https://annheilong.pixnet.net/blog/post/24919514)
# RS485CAN-HAT-user-manual
[RS485CAN-HAT-user-manual](https://www.notion.so/RS485CAN-HAT-user-manual-baac5dfb3e7c4ce6a51c01ff3b148d2e)
## 疑難排解
[疑難排解](https://www.notion.so/91d1906b38f54e8687b4004cfeda53a1)
# **樹莓派 RPI.GPIO 使用**
> [3、樹莓派 RPI.GPIO 使用手冊](https://www.itread01.com/content/1543537390.html)
## **Raspberry Pi 的GPIO腳位功能**
> [Raspberry Pi GPIO Programming in C | Big Mess o' Wires](https://www.bigmessowires.com/2018/05/26/raspberry-pi-gpio-programming-in-c/)


## `GPIO.BOARD` 與 `GPIO.BCM` 設定有何不同
`GPIO.BOAR` 選項是指定在電路版上接脚的號碼
`GPIO.BCM` 選項是指定GPIO後面的號碼
## 為輸出通道指定一個初始值
```python
GPIO.setup(channel, GPIO.OUT, initial=GPIO.HIGH)
```
## 設定多個GPIO腳
```python
chan_list = [ 11 ,12 ]
#加你想盡可能多的渠道!
#你可以用元組代替,即:chan_list =(11,12)
GPIO.setup(chan_list, GPIO.OUT)
```
## **上拉/下拉電阻**
```python
GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# or
GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
```
## **Raspberry Pi 按鈕實作**
> [Raspberry Pi 3 Model B 與瞬時按鈕 - 妖恫程式部落](https://blog.everlearn.tw/當-python-遇上-raspberry-pi/raspberry-pi-3-model-b-與瞬時按鈕)
## RFID on RPi 使用經驗
[RFID on RPi 使用經驗](https://www.notion.so/RFID-on-RPi-8b821f820fea43ef8c4ec3f781ef1665)
## RPi Ubuntu 20.04 usage note
[Rpi 3B Ubuntu Server 20.04](https://hackmd.io/Iehbi9OfTKG1IZ7fWGrYbA)
## RPi ROS1 Docker Container
[Docker Dev Workspace](https://hackmd.io/y3WZKds5TY6IXS10zPGupg)
# 郭哲明的RPi notes
## 2021/11/05 initiallization
```bash=
# make download folder
cd && mkdir Downloads
# update and upgrade apt
sudo apt update && sudo apt upgrade
# install commonly used packages
sudo apt install vim tmux zsh git python3-pip
# change shell to zsh
chsh -s $(which zsh)
```
```bash=+
# install package manager (vundle) for vim
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
# install package manager (tpm) for tmux
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# install package manager (Oh-my-zsh) for zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# install zsh-autosuggestion for zsh
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# install powerlevel10k for zsh
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
# get my configuration files
cd ~/Downloads && git clone https://github.com/chemmy-jack/dotfiles.git
# setup my tmux and vim setting
cd ~/Downloads/dotfiles
cp .tmux.conf ~/
cp .vimrc ~/
# open vim and type :PluginInstall
# open tmux and type prefix+I
cd ~ && vim .zshrc
# change ZSH_THEME="powerlevel10k/powerlevel10k"
# add git and zsh-autosuggestions to ZSH_PLUGINS
# generate ssh key pair
ssh-keygen -t rsa
# set up ssh for github (should add pubkey to github account first)
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
```
[如果想在tmux看到溫度點我](https://github.com/kolach/tmux-temp)
## usefull commands and shortcuts
```bash=
# check temperture
vcgencmd measure_temp
# monitor
htop
# change console font
sudo dpkg-reconfigure console-setup
# kernel message buffer
dmesg
# (at a terminal local, not in Rpi server) send ssh key
# (for me public key is ~/.ssh/id_rsa.pub)
ssh-copy-id -i [public key] pi@[ip of rpi]
# copy via ssh
scp
# better way of cp
rsync
```
* netstat -tulpn, ip show (ifconfig for older version), systerctl (service for older version), uname -a, du(disk usage), df(disk freespace)
* lsblk, blkid, swapon, swapoff
* [some command] | grep [something u want]
* for rapi only
* vcgencmd
* get_throttled (check undervoltage) ([explained](https://forum.libreelec.tv/thread/17860-how-to-interpret-rpi-vcgencmd-get-throttled/))
* measure_temp (cpu temperature)
* raspi-config
Some usefull shorcuts
* ctrl + w, u, k, e, a
* ctrl + C, V
* ctrl + n, p
* ctrl + y
## gps (topgnss)
* [official website](www.topgnss.com)
* [page for GN200](https://www.topgnss.com/product1/showproduct.php?id=39)
* [USB_with_LINUX(GPS.G4-CS-07059).pdf](https://www.topgnss.com/upload/file/202107/USB_with_LINUX(GPS.G4-CS-07059).pdf)
* [u-blox 5 - Receiver Description](http://courses.cs.tau.ac.il/embedded/projects/fall2011/gps_tracker/files/u-blox5_Protocol_Specifications(GPS.G5-X-07036).pdf)
* 我搞了很久直到我仔細看了[這篇](https://learn.adafruit.com/adafruit-ultimate-gps-hat-for-raspberry-pi/use-gpsd)才成功抓到資料
* ==seems to be usefull==
* edit /lib/systemd/system/gpsd.socket
* 改為ListenStream=0.0.0.0:2947
首先要讓gpsd讀到gps sonsor的資料:
```bash=
# the manual way
# check gps connected on
ls -l /dev/serial*
# install gpsd
sudo apt install gpsd gpsd-clients
# close default service bcause it will interfere with following manual setup
sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket
# run gpsd
sudo gpsd /dev/ttyACM0 -F /var/run/gpsd.sock
# if "NO_FIX", try this
sudo killall gpsd
sudo gpsd /dev/ttyACM0 -F /var/run/gpsd.sock
```
```bash=
# use default way
sudo systemctl enable gpsd.socket
sudo systemctl start gpsd.socket
sudo systemctl status gpsd.sockt # can check status
# reset gpsd
sudo dpkg-reconfigure gpsd
```
```bash=
# show gps data
gpsmon
cgps # simplified of gpsmon
```
:::danger
要確認gps收訊好不好,本人本來好幾天嘗試多次只成功一次,gps從原本窗內移到窗外就順了,這中間距離只差了不到1公尺。
:::
:::warning
gps時好時壞,有時要開開關關幾次才行
:::
python要讀得到要裝這個套件
```bash=+
pip3 install gpsd-py3
```