[TUT] Build Private Git Server with Raspberry Pi

# install Git
sudo apt-get install wget git-core
# add new user and user group to your pi
sudo adduser --system --shell /bin/bash --gecos 'git version control by pi' --group --home /home/git git
# set password
sudo passwd git
# swap to new user
su git
mkdir test.git
cd test.git
git --bare init
# clone
git clone ssh://git@[your IP]:[your port]/home/git/test.git
# push
git remote add pi ssh://git@[your IP]:[your port]/home/git/test.git
git add .
git commit -am "Initial"
git push pi master

Reference
[1] 用树莓派搭建Git私有服务器

[GCP] 搭建 VPN 服务器 – 在线代理篇

区别与其它代理服务器,在线代理的优点在于不用安装额外的客户端以及配置就直接可以访问被墙的网络,但部分模块可能会无法加载出来,比如 xgeeky.com/proxy

phproxy下载:phproxy

定位到空间的根目录,新建一个文件夹 proxy
下载 proxy.zip 并解压到 proxy 文件夹下
设置好相关权限即所属者
# 新建文件夹并设置归属
sudo mkdir proxy & cd proxy
sudo chown www-data:www-data proxy
# 下载并解压proxy.zip
wget http://xgeeky.com/wp-content/uploads/2017/12/phproxy.zip
unzip phproxy.zip
# 设置归属
sudo chown www-data:www-data *

相关连接
[1] [GCP] 搭建 VPN 服务器 – 创建虚拟机实例
[2] [GCP] 搭建 VPN 服务器 – Shadowsocks篇
[3] [GCP] 搭建 VPN 服务器 – IPsec VPN篇

[GCP] 搭建 VPN 服务器 – IPsec VPN篇

感谢 hwdsl2 的贡献,使得 IPsec VPN 的创建更加简单。

# 下载脚本
wget https://git.io/vpnsetup -O vpnsetup.sh
# 如果需要自定义用户名密码之类,可以先编辑脚本,将里面的YOUR_IPSEC_PSK, YOUR_USERNAME 以及 YOUR_PASSWORD的值替换成自己的,然后运行
nano -w vpnsetup.sh
# 运行脚本
sudo sh vpnsetup.sh

基本上只要耐心等待个几分钟就自动创建完成了。当然对于如何连接到vpn服务器。作者的Github上也有详细的说明。

相关连接
[1] [GCP] 搭建 VPN 服务器 – 创建虚拟机实例
[2] [GCP] 搭建 VPN 服务器 – Shadowsocks篇
[3] [GCP] 搭建 VPN 服务器 – 在线代理篇

参考
[1] hwdsl2 – setup-ipsec-vpn

[GCP] 搭建 VPN 服务器 – Shadowsocks篇

Shadowsocks的创建相对简单,其VPN服务器的支持性也不错。基本只要在对应的平台上安装上客户端,成功配置就可以连接到VPN服务器了。

# 更新系统
sudo apt-get update
# 安装 pip
sudo curl -O sudo apt-get install python-pip
# 使用pip 安装 Shadowsocks
sudo pip install shadowsocks
sudo nano /etc/shadowsocks.json

进入编辑器后复制以下内容,记得替换密码 “mypassword” 成你的密码,之后按 Ctrl+o 保存,Ctrl+x 退出。

{
    "server":"0.0.0.0",
    "server_port":8388,
    "local_address": "127.0.0.1",
    "local_port":1080,
    "password":"mypassword",
    "timeout":600,
    "method":"aes-256-cfb",
    "fast_open": false
}
# 新建脚本
sudo nano /etc/init.d/shadowsocks

进入编辑器后复制以下内容,之后按 Ctrl+o 保存,Ctrl+x 退出。

#!/bin/sh
### BEGIN INIT INFO
# Provides:          shadowsocks
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start shadowsocks 
# Description:       start shadowsocks
### END INIT INFO

start(){
    ssserver -c /etc/shadowsocks.json -d start
}

stop(){
    ssserver -c /etc/shadowsocks.json -d stop
}

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
reload)
     stop
     start
     ;;
*)
    echo "Usage: $0 {start|reload|stop}"
    exit 1
    ;;
esac
# 修改权限
sudo chmod +x /etc/init.d/shadowsocks
# 添加 开机启动
sudo update-rc.d shadowsocks defaults
# 启动 服务
sudo service shadowsocks start

之后就可以下载相应平台的客户端配置好就可以连接到vpn服务器了。

相关连接
[1] [GCP] 搭建 VPN 服务器 – 创建虚拟机实例
[2] [GCP] 搭建 VPN 服务器 – IPsec VPN篇
[3] [GCP] 搭建 VPN 服务器 – 在线代理篇

参考
[1] 用Google Cloud Platform搭建Shadowsocks服务教程
[2] 设置 shadowsocks server 开机启动

[GCP] 搭建 VPN 服务器 – 创建虚拟机实例

在gcp上(谷歌云)上设置一个vpn服务器可能是最爽的事情了。基于google 1年的免费额,你可以在gcp上创建一个专属vpn私服,甚至可以做到流畅1080p的YouTube播放。

1 端口映射
转到 “VPC Network – Firewall rule” 并添加一下防火墙规则,即允许所有端口。

2 创建虚拟机实例
转到 “Compute Engine – VM instances” 并创建一个实例,参数可以按照一下选择:
Name (名称): 随意
Zone (区域): 选择 asia-east1-a
Machine type (机器类型): micro 即可,一个共享cpu,600mb 内存
Boot disk (启动盘): 选择 Debian 8
Firewall (防火墙): 勾上允许HTTP以及HTTPS
展开选项卡,选择 Networking (网络) – External IP (外网IP): 这里我们需要选择创建一个IP并分配给该虚拟机。这样这个虚拟机才能被外网访问到。

点击保存后,虚拟机即会开始启动。点击SSH就能以网页的形式进入我们所熟悉的SSH界面了。

相关连接
[1] [GCP] 搭建 VPN 服务器 – Shadowsocks篇
[2] [GCP] 搭建 VPN 服务器 – IPsec VPN篇
[3] [GCP] 搭建 VPN 服务器 – 在线代理篇

[GCP] Run tensorflow with GPU on Google Cloud Platform

To create a VM instance, you have to register on the Google cloud platform, and once you registered, you will get a 300$ free trial. I won’t include the registration part here as it is very easy.

1 External IP addresses
Navigate to “VPC Network – External IP addresses” and apply for a new static IP and name it as you want
The result will look like this, in this case, i named it to taiwan as i choose from asia areas:

2 Firewall rules
Navigate to “VPC Network – Firewall rule” and add a firewall rule like this:

3 Apply an API to use GPUs
You have to choose a right area as some areas do not provide GPU usage and the price could be various. In this case, we choose “asia-east1-a” and decide to use its Tesla P100
Navigate to “IAM & admin – Quotas” and select the API you want to apply, you will receive an email from Google, then the follow the instructions to use the GPU you want (usually, just click the link provided in the email).

4 Create an instance
Navigate to “Compute Engine – VM instances” and create an vm instance.
In order to the use GPU we applied, you have to choose the same area.
Hit the “Customize” button and customize your machine type, then expand the GPU section to use a P100.
For the operating system, just choose Ubuntu 16, you can also use an SSD or expand the boot disk size to 20 GB if you like.
For the Firewall section, just tick both the HTTP and HTTPS traffic.
You may also have to expand the next section and configure your network interface to use static IP (optional).
Do not be afraid of the price as Google will only charge you from the 300$ when you are running the device and it is charged by minutes.

5 CUDA® Toolkit 8.0.
Once you have created the instance, click SSH button to open an SSH console and follow the following commands.

sudo apt-get update
sudo curl -O http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
sudo dpkg -i ./cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda-8-0 -y

6 cuDNN v6 for CUDA 8.0
We have to use the Jupyter Notebook to upload our installation package first. To do this, you can click this link and download one. Remeber, you have to create a NVIDIA account if do not have one. Then, agree terms and choose the following one to download, which is the “cuDNN v6.0 Library for Linux”.

# The file name could be different, be careful.
# Download the cudnn package with wget. You can get the download link when you are downlowning a cudnn file.
wget https://...../cudnn-8.0-linux-x64-v6.0.tgz.xxxxxxxxxxx
# Rename the file
mv cudnn-8.0-linux-x64-v6.0.tgz.xxxxxxxxxxx cudnn-8.0-linux-x64-v6.0.tgz
# Unzip the file and install cudnn
tar -xzvf cudnn-8.0-linux-x64-v6.0.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
# Edit .bashrc
nano ~/.bashrc
# Add the following two lines to the file
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
# Press Ctrl+O to save changes and Ctrl+X to exit the editor
# Update changes
source ~/.bashrc

7 Tensorflow

sudo pip3 install tensorflow-gpu

Now you can have fun with your VM instance.
To save your 300$, do not to forget to shutdown your VM instance after using it.

[RPi] 为树莓派添加DIY智能散热风扇

普通散热风扇只有两个线,即VCC电源以及GND接地,而树莓派的GPIO最大只能输出几十毫安,所以根本无法直接通过GPIO来控制风扇。
所以,我们需要加装一个三极管使用小电流来控制大电流。

三极管在很多旧设备里面可以找到,这里以 1AM 示例(从USB支付钥匙拆的)。然后以以下示意图接上三极管。

GitHub: https://github.com/xswxm/Smart_Fan_for_Raspberry_Pi.git

这次一共写了两个版本,一个是普通版本,只能开关风扇,另一个PWM版本支持PWM调速。笔者的风扇使用PWM调速时,能够良好的工作,但有滋滋的噪声,这可能与风扇的质量有关,或许也可以后期改进电路解决。

sudo apt-get update
sudo apt-get install python python-dev python-pip
# 安装模块
sudo pip install rpi.gpio
sudo apt-get install pigpio python-pigpio
# 查看帮助文档
python fan.py -h
# 以默认配置运行
python fan.py
# 后台运行
nohup python fan.py &
# 设置pin为24,每2秒检测一下CPU温度,若超过50度则开启风扇
python fan.py -t 50 -i 2 -p 24
# 启用 pigpio 服务
sudo pigpiod
# 查看帮助文档
python fan_pwm.py -h
# 以默认配置运行
python fan_pwm.py
# 后台运行
nohup python fan_pwm.py &
# 设置pin为24,每2秒检测一下CPU温度,若超过50度则开启风扇,同时设置温度每增加1摄氏度,风扇速度增加10%
python fan_pwm.py -t 50 -i 2 -p 24 -s 10

[RPi] 树莓派添加samba文件共享服务

1. 安装Samba

sudo apt-get install samba samba-common-bin

2. 为samba添加用户,可以使用现有账户,也可以创建专属账户

添加专属账户(如果你只想要添加已存在的账户,可以跳过这一步)

# 添加账户,username即你想要添加的账户名
sudo useradd username
# 建立共享文件夹即修改用户组
sudo mkdir -p /home/username
sudo chown -R username:username /home/username/

为samba添加账户

# 添加账户,username是你想要添加到samba的账户,该步骤会要求设置密码,需要注意的是如果你samba下的用户名的密码区别于树莓派系统下的用户名的密码
sudo smbpasswd -a username

3. 修改配置文件

# 对原配置文件做一备份
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
# 创建一个新的配置文件
sudo nano /etc/samba/smb.conf

配置文件可以照如下修改,然后按 Ctrl+o 保存,Ctrl+x退出

[pi]
    comment= My Samba
    path = /home/username
    valid users = username
    browseable = yes
    writable = yes

部分参数解释:
pi: 显示的名称
comment: 显示名称下的注释
path: 共享的路径
vaild users: 具有操作权限的用户名
browseable: 是否可以浏览
writable: 是否可以写入

4. 重启samba服务

sudo service smbd restart

5. 使用samba服务

Windows用户: 打开计算机,在地址栏输入 ‘\\youripaddress\’(不含引号,youripaddress是你的树莓派IP地址)即可找到共享的文件夹
Linux用户: 打开文件浏览器,在地址栏 ‘smb://youripaddress/’(不含引号,youripaddress是你的树莓派IP地址)即可
输入对应的用户名和密码即可浏览操作拥有权限的文件及文件夹

[12864/SSD1306] Play Snake on Raspberry Pi

System Monitor for Raspberry Pi with 12864/SSD1306, and you can download all the files from GitHub.

Demo
Demo

Setting Up
Configure your 128464 by following this tutorial: https://learn.adafruit.com/ssd1306-oled-displays-with-raspberry-pi-and-beaglebone-black/usage?view=all

# Ports used for the Display. You may have to change them to yours
RST = 25
DC = 24
# Configurations of this game
# You may check the preference below and alter the values if you want
display_interval = 0.05                # Refresh display every 0.05 second => FPS = 20
size = 2                               # Size of a snake, 2 means one point occupies four pixels (2x2)
# Initialize the boundary of the gaming area
area = (0, 0, disp.width*2/3/size-1, (disp.height-2)/size-1)    # Gameing area in points with size
boundary = (0, 0, (area[2]+1)*size+1, (area[3]+1)*size+1)       # Gameing boundary
boundary_cross = True                  # snake is able to cross the boundary 
# Block setting
block_mode = False                     # Do not create blocks
# Initialize a snake
snake = []                             # snake
snake0 = (area[2]/2, area[3]/2)        # snake's head (first pixel)
snake1 = snake0                        # snake's tail (last pixel)
l = 4                                  # snake's length = 4
interval_max = 1
interval_min = 0.05
interval = interval_max                # move the snake every 1 second
direction = (1, 0)                     # direction = Right
food = snake0                          # normal food
food_score = 1                         # each food worth 1 score
sfood = ()                             # initialize a empty special food
sfood_lifetime = 10                    # special food exists for 10 seconds
sfood_count = 0                        # to count the time of the special food
sfood_interval = 0.25                  # refresh the special food every 0.25 second
sfood_time = 30                        # Possible to generate a special food every 30 seconds
sfood_score = 5                        # each special food worth 5 scores
blocks = []                            # blocks for blocking the snake
scores = [0, 10, 50, 100, 150, 250, 350, 550, 750, 950, 1200]
levels = [0, 1,  2,  3,   4,   5,   6,   7,   8,   9,   10]
score = 0
level = 0

How to Use

# Run directly with diffculity = 0
sudo python snake.py
# Run directly with diffculity = 1
sudo python snake.py 1
# Run directly with diffculity = 2
sudo python snake.py 2

How to Play
Control: Press ‘w’, ‘a’, ‘s’, ‘d’
Speed up/remove effect: Press ‘q’
Exit game: Press ‘x’

License
—-
MIT