raspberryPI GPIO Programming on ArchLinux

###Python Preparation Install python

	$ pacman -S python

Install pip

	$ pacman -S python-pip

Install virtualenv, before install virtualenv, be sure to update your time. 1970’s time will get ssl error.

	$ date -s "8 Dec 2013 16:09:40"
	$ pip install --upgrade virtualenv virtualenvwrapper

Prepare virtualenv:

	[root@alarmpi ~]# mkdir ~/pyv
	[root@alarmpi ~]# vim ~/.virtualenvrc
	export WORKON_HOME="/root/pyv"
	export PROJECT_HOME="/root/pyv"
	source /usr/bin/virtualenvwrapper.sh 
	[root@alarmpi ~]# source ~/.virtualenvrc

Now your virtualenv is OK. Make a virtualenv for python2.7

	$ mkvirtualenv --python=/usr/bin/python2.7 v27
	cdvirtualenv

Add environment variables into your ~/.bashrc

	export WORKON_HOME=$HOME/pyv
	export PIP_VIRTUALENV_BASE=$WORKON_HOME
	source /usr/bin/virtualenvwrapper.sh

Next time if you want to enter virtualenv, simply type:

	$ workon v27

Install rpi.gpio

	$ pip install rpi.gpio

###Use rpi.gpio on raspberryPI TBD. I don’t know why but my raspberyPI can’t work properly on 1602 LCD. So I begin to test GPIO ###Use WiringPi for programming Preparation:

	$ pacman -S git gcc make

Download the packages:

	$ git clone git://git.drogon.net/wiringPi
	$ cd wiringPi
	$ git pull origin
	$ ./build

Testing the WiringPi:

	[root@alarmpi gpio]# ./gpio readall
	+----------+-Rev1-+------+--------+------+-------+
	| wiringPi | GPIO | Phys | Name   | Mode | Value |
	+----------+------+------+--------+------+-------+
	|      0   |  17  |  11  | GPIO 0 | OUT  | High  |
	|      1   |  18  |  12  | GPIO 1 | IN   | Low   |
	|      2   |  21  |  13  | GPIO 2 | IN   | Low   |
	|      3   |  22  |  15  | GPIO 3 | IN   | Low   |
	|      4   |  23  |  16  | GPIO 4 | IN   | Low   |
	|      5   |  24  |  18  | GPIO 5 | IN   | Low   |
	|      6   |  25  |  22  | GPIO 6 | IN   | Low   |
	|      7   |   4  |   7  | GPIO 7 | IN   | Low   |
	|      8   |   0  |   3  | SDA    | ALT0 | High  |
	|      9   |   1  |   5  | SCL    | ALT0 | High  |
	|     10   |   8  |  24  | CE0    | ALT0 | High  |
	|     11   |   7  |  26  | CE1    | ALT0 | High  |
	|     12   |  10  |  19  | MOSI   | ALT0 | Low   |
	|     13   |   9  |  21  | MISO   | ALT0 | Low   |
	|     14   |  11  |  23  | SCLK   | ALT0 | Low   |
	|     15   |  14  |   8  | TxD    | ALT0 | High  |
	|     16   |  15  |  10  | RxD    | ALT0 | High  |
	+----------+------+------+--------+------+-------+

Simply testing a LED tutorial:
Wiring image:
sourceledgpio.jpg

Then Writing following script:

#!/bin/bash
/root/wiringPi/gpio/gpio mode 0 out
while true then
do
	/root/wiringPi/gpio/gpio write 0 1
	sleep 1
	/root/wiringPi/gpio/gpio write 0 0
	sleep 1
done

Now you will get a shining LED.

Linux Sound

###ALSA普通用户无声 只有root才能听到声音,其他一概是哑巴,解决方案:

	$ sudo apt-get install acl
	$ sudo setfacl -m u:Your_Username:rw /dev/snd/*

等于说赋予了普通用户(Your_Username)访问/dev/snd下所有设备的读写权限。这时候打开mplayer就可以听到MP3播放声了。 ###使用tsocks和ssh转发穿越防火墙

	ssh -qTfnN -D 1394 xxx@xxx.xxx.xxx.xxx

这样可以在本地打开一个socks代理,127.0.0.1:1394 安装tsocks

	$ sudo apt-get install tsocks

配置tsocks

	# vim /etc/tsocks.conf
	# Local networks
	# For this example this machine can directly access 192.168.0.0/255.255.255.0 
	# (192.168.0.*) and 10.0.0.0/255.0.0.0 (10.*)
	
	local = 192.168.0.0/255.255.255.0
	local = 10.0.0.0/255.0.0.0
	server = 127.0.0.1
	server_port = 1394
	server_type = 5

试听音乐:

	tsocks mplayer http://www.live365.com/play/wkhr

但是,用vlc总是提示失败。 ###安装archlinux for raspberryPI 下载并解压得到img文件,而后:

	[Trusty@XXXyyy arch_rasp]$ fdisk -l archlinux-hf-2013-07-22.img
	
	Disk archlinux-hf-2013-07-22.img: 1.8 GiB, 1960837120 bytes, 3829760 sectors
	Units: sectors of 1 * 512 = 512 bytes
	Sector size (logical/physical): 512 bytes / 512 bytes
	I/O size (minimum/optimal): 512 bytes / 512 bytes
	Disklabel type: dos
	Disk identifier: 0x00057540
	
	Device                       Boot     Start       End  Blocks  Id System
	archlinux-hf-2013-07-22.img1           2048    186367   92160   c W95 FAT32 (LBA)
	archlinux-hf-2013-07-22.img2         186368   3667967 1740800   5 Extended
	archlinux-hf-2013-07-22.img5         188416   3667967 1739776  83 Linux

拷贝出根分区:

	$ mount ./archlinux-hf-2013-07-22.img -o offset=96468992 /mnt
	$ tar cjvf mnt.tar.bz2 /mnt

然后将mnt.tar.bz2解压到某硬盘分区,则可以用该硬盘启动raspberryPI

	$ pacman -Syu --noconfirm
	$ pacman -S tigervnc
	$ pacman -S alsa-utils lxde mplayer vlc vim 
   	$ pacman -S xf86-video-fbdev
   	$ pacman -S alsa-utils alsa-firmware alsa-lib alsa-plugins
	$ pacman -S sudo

然后我们可以编辑出一个vncserver

	$ vncserver
	$ vim /root/.vnc/xstartup
	[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
	xsetroot -solid grey
	xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
	#twm &
	startlxde &
	$ vncserver -kill :1
	$ vncserver

添加普通用户的权限和vnc:

	$ visudo
	root ALL=(ALL) ALL
	Trusty ALL=(ALL) ALL
	Trusty ALL=(ALL) NOPASSWD: ALL

###Start playing on boot 每次允许自动登录:

	[Trusty@XXXyyy ~]$ sudo mkdir /etc/systemd/system/getty@tty1.service.d
	[Trusty@XXXyyy ~]$ cat /etc/systemd/system/getty\@tty1.service.d/autologin.conf 
	[Service]
	ExecStart=
	ExecStart=-/usr/bin/agetty --autologin Trusty --noclear %I 38400 linux

安装screen

	$ pacman -S screen

增加自定义服务:

	[root@alarmpi ~]# cat /etc/systemd/system/radio.service
	[Unit]
	Description=My Radio
	After=network.target
	
	[Service]
	RemainAfterExit=yes
	ExecStart=/usr/bin/autoradio &
	
	[Install]
	WantedBy=multi-user.target 

编写autoradio脚本:

	[root@alarmpi ~]# cat /usr/bin/autoradio 
	#!/bin/bash
	screen -d -m sudo -u Trusty cvlc --aout oss http://www.live365.com/play/wkhr --http-proxy=http://10.0.0.221:9001

如果想添加管理

	[root@alarmpi ~]# cat /usr/bin/autoradio 
	#!/bin/bash
	screen -d -m sudo -u Trusty cvlc --aout oss http://www.live365.com/play/wkhr --http-proxy=http://10.0.0.221:9001 --http-port=5202 --http-password=xxxxxx

好了,这个Internet Radio就写好了。Enjoy it!!!

Enable spice for qemu virtual machine

###Package Installation Install virt-viewer for browsing the virtual machine desktop. For default spicec is not OK.

	$ pacman -S gtk-vnc
	$ yaourt -S spice-gtk3
	$ yaourt -S virt-viewer

Install virt-manager

	[root@DashArch Trusty]# pacman -S virt-manager 
	[root@DashArch Trusty]# systemctl start libvirtd.service
	[root@DashArch Trusty]# systemctl enable  libvirtd.service
	ln -s '/usr/lib/systemd/system/libvirtd.service' '/etc/systemd/system/multi-user.target.wants/libvirtd.service'
	[root@DashArch Trusty]# ps -ef | grep libvirt
	root      8852     1  5 15:23 ?        00:00:00 /usr/bin/libvirtd -p /var/run/libvirtd.pid

###启动支持spice Server的qemu -vga qxl -spice port=5988,disable-ticketing将使能spice

	./run-qemu -boot d  -m 1024 -enable-kvm -drive file=./fpgawindows.qcow2,if=ide -drive file=./fake.qcow2,if=virtio -cdrom ./virtio-win-0.1-74.iso -usb -vga qxl -spice port=5988,disable-ticketing -localtime

在本地或者远程访问spice server:

	$ spice -h 127.0.0.1 -p 5988

但是ArchLinux上的X Server会出现问题,具体解决方案未明了。然而在Ubuntu上则是可以顺利访问的。 ###启动支持VNC的Qemu

	./run-qemu -boot d  -m 1024 -enable-kvm -drive file=./fpgawindows.qcow2,if=ide -drive file=./fake.qcow2,if=virtio -cdrom ./virtio-win-0.1-74.iso -usb -vga std -nographic  -localtime -vnc :33
启动后vncviewer :33则可连接到qemu的窗口

###启动Qemu,以std方式 全新启动:

	./run-qemu -boot d  -m 1024 -enable-kvm -drive file=./fpgawindows.qcow2,if=ide -drive file=./fake.qcow2,if=virtio -cdrom ./virtio-win-0.1-74.iso -usb -vga std  -localtime `

保存状态: ctl+alt+2

	$ savevm booted

启动到上一次保存的状态:

	$  ./run-qemu -boot d  -m 1024 -enable-kvm -drive file=./fpgawindows.qcow2,if=ide -drive file=./fake.qcow2,if=virtio -cdrom ./virtio-win-0.1-74.iso -usb -vga std  -localtime --loadvm booted

Install Qemu virtio driver under windows

Download the iso file from the redhat repository:
http://alt.fedoraproject.org/pub/alt/virtio-win/latest/images/images/images/bin/src/
Start the qemu with the following command :

 	./run-qemu -hda fpgawindows.qcow2 -m 1024 -cdrom ./virtio-win-0.1-74.iso -drive file=./fake.qcow2,if=ide

In run-qemu, the actual command is:

	qemu-system-i386 -net nic,model=virtio,macaddr=$macaddr -net tap,ifname="$IFACE" $*

Then, follow the following images to operate:

qemu1.jpg

qemu2.jpg

qemu3.jpg

qemu4.jpg

qemu5.jpg

autossh multiple connection?

Strangely, I cannot enable the multiple SSH connections on OpenWRT.
The configuration file is listed as:

	config autossh
		option ssh	'-N -T -R 4381:localhost:22 root@XXX.xxx.xxx.xxx '
		option gatetime	'0'
		option monitorport	'20000'
		option poll	'600'
	#config autossh
	#	option ssh	'-L -N -T 10.0.0.1:9009:1XX.XX.XX.XXX:8000 xxx@xxx.xxx.xxx.xxx '
	#	option gatetime	'0'
	#	option monitorport	'20001'
	#	option poll	'600'

But only 1 connection could be enabled.
I doubt this is because of the startup scripts for /etc/init.d/autossh. I should change its methods.

	start() {
		config_load 'autossh'
		config_foreach start_instance 'autossh'
	}

But how to read and display the configuration files? It seems the multiple selections is hard to config…..