# RPI setting
###### tags: `RPI`
部分內容 中文
https://medium.com/@linnil1/rpi-%E5%BF%AB%E9%80%9F%E5%88%9D%E5%A7%8B%E5%8C%96%E8%A8%AD%E5%AE%9A-831c971045d5
## First thing
https://sunnyhsieh.wordpress.com/2017/07/12/raspberry-pi-lane-tracking-car-raspberry-pi-configuration/
* type `passwd` to change password
* auto login(Optional)
http://www.linuxproblem.org/art_9.html
Some basic command
```
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install tmux vim-gtk
```
## rpi wifi setting
* scan wifi
`sudo iwlist wlan0 scan`
* modify file to auto connect to wifi
`sudo vim /etc/network/interfaces`
change wlan0 like this
```
auto lo
iface lo inet loopback
iface eth0 inet manual
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
```
* and set wifi network
`sudo vim /etc/wpa_supplicant/wpa_supplicant.conf`
add this to bottom of file
```
network={
ssid="name"
psk="password"
priority=1
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
}
network={
ssid="name"
psk="password"
priority=2
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
}
```
* check if config file is valid
```
wpa_cli -i wlan0 reconfigure
sudo systemctl restart networking
sudo systemctl restart networking
```
* check ip
`ifconfig wlan0`
`curl ipinfo.io/ip`
## Change hostname
`sudo raspi-config` Network options -> Hostname -> `[your hostname]`
And reboot.
## when locale warning
`echo 'export LC_ALL=C' >> ~/.bashrc`
## Upgrade python(Optional)
default python version is 3.5.3
so i upgrade using source
install 3.6
```
sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libreadline6-dev libdb5.3-dev libgdbm-dev libexpat1-dev liblzma-dev
```
```
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
tar xzvf Python-3.6.2.tgz
cd Python-3.6.2/
./configure
make
sudo make altinstall
```
https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f
and add this two line in `~/.bashrc`
``` bash
alias python3='python3.6'
alias pip3='pip3.6'
```
## Install opencv
### For ubuntu 18.04 at python3.6
`pip3 install opencv-python` is OK
https://github.com/skvark/opencv-python
you can try it if it can work, then you just jump below step away.
### For rpi with python2
`sudo apt-get install python-opencv`
### For rpi with python3(Recommand)
Install some packages(I just install all posibile packages, maybe some is not nesscery)
``` shell
sudo apt install -y python3-pip libatlas-base-dev webp libjasper-dev libilmbase-dev libopenexr-dev libavcodec-dev libavformat-dev libswscale-dev libqtgui4 libqt4-test libgstreamer1.0-dev
pip3 install numpy opencv-python
```
testing
`python3 -c 'import numpy;import cv2'`
reference: https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu/
and install other things may also useful
``` shell
sudo apt install python3-tk
pip3 install pillow matplotlib
```
I found that if you install other balst library, it will break your numpy.
### For RPI and installed opencv from source
https://sunnyhsieh.wordpress.com/2017/07/15/raspberry-pi-lane-tracking-car-install-opencv/
**be care for the path** in `cmake -D ...`
`sudo apt-get install libcanberra-gtk3-module`
`sudo pip3 install numpy`
```
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/Download/opencv_contrib-3.2.0/modules \
-D BUILD_EXAMPLES=OFF \
-D WITH_JPEG=ON \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.6/site-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.6/site-packages \
-D PYTHON3_EXECUTABLE=/usr/local/bin/python3.6 \
-D PYTHON3_INCLUDE_DIR=/usr/local/include/python3.6m \
..
```
after make
you should beware of this output

this is small change from the above link and
https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f
http://answers.opencv.org/question/38362/opencv-camera-interfacing/
http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/
## webcam (without opencv)
https://www.raspberrypi.org/documentation/usage/webcams/
`sudo apt-get install fswebcam`
test it without opencv
`sudo apt-get eog`
log in with `ssh -X pi@192.168.xx.xx`
```
fswebcam test.jpg
ego test.jpg
```
## time zone
configure time zone
`sudo dpkg-reconfigure tzdata`
check it
`date`
`python3 -c 'import time;print(time.strftime("%c"))'`
## opencv usage
**make sure you log in with `ssh -X pi@192.168.xx.xx`**
* exmaple
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html
* capture image and output
use can test to capture image from webcam
``` python3
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imshow('image', frame)
cv2.waitKey(0)
```

* using video stream
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html

However it will show
`Corrupt JPEG data: 2 extraneous bytes before marker 0xd2`
you can ignore it
i found that `VideoCapture` will fail **sometime**
```
cap = cv2.VideoCapture(0)
while not cap.isOpened():
print("Reset")
cap = cv2.VideoCapture(0)
```

all script
{%gist linnil1/3e1dbc2abbf1765120f1f0f18d0f7ac1%}
## run script when boot
`sudo vim /etc/rc.local`
add this line
`su pi -c "tmux new-session -d 'python3 /home/pi/connect.py'" &`
## upload it by gdrive
* install
https://github.com/prasmussen/gdrive
download it
``` sh
mv gdrive-linux-rpi gdrive
chmod +x gdrive
./gdrive about
sudo ln -s gdrive /usr/local/bin/
gdrive mkdir image-data
```
* upload methods
upload it by sync folder
`gdrive sync upload local-folder gdrive-foler-id`
or file by file upload (can be concurrent)
write in python3
``` python3
import subprocess
from os.path import expanduser, join
def upload(path):
rep = subprocess.run(["gdrive", "upload", path, "--delete"], stdout=subprocess.PIPE)
rep = str(rep.stdout, "utf-8")
if "Failed" in rep:
return False
else:
return True
```
* using concurrent
{%gist linnil1/dadfb9229cf27f164e38d45beefdfa14%}
## upload by sfpt
`sftp -P Port user@IP`
upload file
`put where path/file`
upload folder
`put -r path/ where`
because if we want to upload it automatically
so we need to auto input password
### sshpass
our solution is use sshpass
` sudo apt-get install sshpass`
`sshpass -f password_file sftp -o BatchMode=no -b COMMAND_FILE USER@HOST`
in `password_file`
write you password inside
and in `COMMAND_FILE`
put want you want to write
like:
```
cd where
put -r path/ where
```
## crontab
if you are first time
`sudo service crontab start`
to start this service
To add task
write `cron_job`
```
* * * * * (cd /home/linnil1/python/test/ && ./hi >> cron.txt)
```
(we use `cd` to move to our working dictionary,
this is very helpful for relative path)
You can substitude any code in `()`.
run it: `crobtab cron_job`
[tutoiral](https://www.computerhope.com/unix/ucrontab.htm)
test it
https://crontab.guru/
## picam
https://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/
**sometime my picam is easy to fail
you should check very carefully about phsical connection**
c++
https://github.com/cedricve/raspicam
if cmake cannot work,
add `set(raspicam_DIR "/usr/local/lib/cmake")` before `find_package`
use opencv VideoCaputre
just type `sudo modprobe bcm2835-v4l2`
and you can use it :laughing:
## backup rpi by SDcard
https://www.raspberrypi.org/forums/viewtopic.php?p=239331
for windows
https://sourceforge.net/projects/win32diskimager/files/latest/download
## GPS
`sudo apt-get install gpsd gpsd-clients python-gps`
set `/etc/default/gpsd` like this
```
START_DAEMON="true"
GPSD_OPTIONS="n"
DEVICES="/dev/ttyUSB0"
USBAUTO="false"
GPSD_SOCET="/var/run/gpsd.sock"
```
use `cpgs -s` to check
and python code
https://gist.githubusercontent.com/wolfg1969/4653340/raw/142eb5746619257b0cd4e317fd8f5fd63ddf2022/gpsdData.py
## some thing i don't know(Don't use, I very not sure)
* when some website cannot connect
edit `sudo vim /etc/resolv.conf`
comment all
add this
`nameserver 8.8.8.8`
* slow ping and drop package
(I solved beacuse it is caused by my dorm network)
`sudo iwconfig wlan0 power off`
https://github.com/RPi-Distro/repo/issues/28
`sudo apt-get install x11-apps`
`sudo rpi-update`