Building Qemu Based RaspberryPI Development Environment

First download the latest image from http://www.raspberrypi.org/downloads, mine is Wheezy. And we also have to download the qemu-compatible kernel from following address:

	$ wget http://xecdesign.com/downloads/linux-qemu/kernel-qemu

Change the img file according to http://localhost/blog/2013/09/04/qemu-for-raspberrypi/, follow this tutorial, you have to change the img file size and its content, but we have to do some modifications. The run-qemu file is changed to following:

	#!/bin/bash
	USERID=$(whoami)
	
	# Get name of newly created TAP device; see https://bbs.archlinux.org/viewtopic.php?pid=1285079#p1285079
	precreationg=$(/usr/bin/ip tuntap list | /usr/bin/cut -d: -f1 | /usr/bin/sort)
	sudo /usr/bin/ip tuntap add user $USERID mode tap
	postcreation=$(/usr/bin/ip tuntap list | /usr/bin/cut -d: -f1 | /usr/bin/sort)
	IFACE=$(comm -13 <(echo "$precreationg") <(echo "$postcreation"))
	
	# This line creates a random mac address. The downside is the dhcp server will assign a different ip each time
	printf -v macaddr "52:54:%02x:%02x:%02x:%02x" $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff )) $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff ))
	# Instead, uncomment and edit this line to set an static mac address. The benefit is that the dhcp server will assign the same ip.
	# macaddr='52:54:be:36:42:a9'
        macaddr = '52:54:79:3c:80:c0'
	 
	qemu-system-arm -net nic,macaddr=$macaddr -net tap,ifname="$IFACE" -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" $*
	  
	sudo ip link set dev $IFACE down &> /dev/null
	sudo ip tuntap del $IFACE mode tap &> /dev/null

In router, we add the static address for mac corresponding to 10.0.0.168, you can alter it according to your habit.

Now we want to disable the X at every startup and use vnc instead. And we can overlocking the raspberryPI to 1000MHZ, this will greatly improve the performance.

Enable the vncserver:

	$ apt-get install tightvncserver vim 

Now startup the vncserver and use vncviewer to view the desktop:

/images/Screenshot-QEMU.png

No, the correct method is listed as:

	cat /eroot@raspberrypi:~# cat /etc/init.d/startvnc 
	#!/bin/sh -e
	### BEGIN INIT INFO
	# Provides:          vncserver
	# Required-Start:    networking
	# Default-Start:     3 4 5
	# Default-Stop:      0 6
	### END INIT INFO
	
	PATH="$PATH:/usr/X11R6/bin/"
	
	# The Username:Group that will run VNC
	export USER="pi"
	#${RUNAS}
	
	# The display that VNC will use
	DISPLAY="1"
	
	# Color depth (between 8 and 32)
	DEPTH="16"
	
	# The Desktop geometry to use.
	#GEOMETRY="<WIDTH>x<HEIGHT>"
	#GEOMETRY="800x600"
	#GEOMETRY="1024x768"
	GEOMETRY="1280x1024"
	
	# The name that the VNC Desktop will have.
	NAME="my-vnc-server"
	
	OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
	
	. /lib/lsb/init-functions
	
	case "$1" in
	start)
	log_action_begin_msg "Starting vncserver for user '${USER}' on   localhost:${DISPLAY}"
	su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
	;;
	
	stop)
	log_action_begin_msg "Stoping vncserver for user '${USER}' on localhost:${DISPLAY}"
	su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
	;;
	
	restart)
	$0 stop
	$0 start
	;;
	esac
	
	exit 0

And now we can add this script into /etc/rc.local as:

	# Start the vncserver here:
	/etc/init.d/startvnc start

So everytime we startup the qemu based raspberryPI, we can easily attached to its geometry, and we can easily adapt the resolution.

Moving Weather App to BBB

Following is the steps for moving the weather app to BBB(BeagleBone Black) ###Apache Configuration Create the site definition file under /etc/apache2/sites-available:

	$ cp default nanjing

Edit the nanjing file with the following content:

	$ cat nanjing
	<VirtualHost *:7778>
		ServerAdmin webmaster@localhost
	        ServerName nanjing
	        ServerAlias nanjing.weather
	
		DocumentRoot /srv/www1
		<Directory />
			Options FollowSymLinks
			AllowOverride None
		</Directory>
		<Directory /srv/www1/>
			Options Indexes FollowSymLinks MultiViews
			AllowOverride None
			Order allow,deny
			Allow from all
		</Directory>
	
		ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
		<Directory "/usr/lib/cgi-bin">
			AllowOverride None
			Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
			Order allow,deny
			Allow from all
		</Directory>
	
		ErrorLog ${APACHE_LOG_DIR}/error.log
	
		# Possible values include: debug, info, notice, warn, error, crit,
		# alert, emerg.
		LogLevel warn
	
		CustomLog ${APACHE_LOG_DIR}/access.log combined
	</VirtualHost>

Then we have to edit the /etc/apache2/ports.conf file, to add the newly-added site definition file.

	$ cat ports.conf
	NameVirtualHost *:7777
	Listen 7777
	NameVirtualHost *:7778
	Listen 7778
	NameVirtualHost *:7779
	Listen 7779
	NameVirtualHost *:7780
	Listen 7780

Now you already could visit the site of http://Your_Ip_Address:7777 for the nanjing weather infos. ###Script Moving Be sure following modules has been installed:< br />

	$ pip install django pywapi pinyin
	$ pip install BeautifulSoup

Add the crontab tasks:

	0 */1 * * * python /root/code/genhtml.py
	15 */1 * * * python /root/code/genhtml_bj.py
	30 */1 * * * python /root/code/genhtml_changsha.py

Now the script will be run every hour at 0/15/30 minutes, enjoy it. Notice the timezone should be correctly configured:

	$  dpkg-reconfigure tzdata
	# Choose Asia/Shanghai

Deploy Weather APP on RaspberryPI

Since I enabled the RaspberryPI and disabled the PogoPlug, I have to move the Weather App on RaspberryPI. The main difference lies on the python version, on PogoPlug the default python version is python2.7, while on RaspberryPI it’s python3.3, thus I have to use the virtualenvironment of Python.

###Setup the virtualenv Following is the steps for setting up the virtual environment for python2.7 on ArchLinux:

	$ mkdir ~/pyves
	$ cat >~/.virtualenvrc <<EOF
	export WORKON_HOME="$HOME/pyves"
	export PROJECT_HOME="$HOME/pyves"
	source /usr/bin/virtualenvwrapper.sh
	EOF
	$ source  ~/.virtualenvrc
	$ cat >>~/.bashrc <<EOF
	source $HOME/.virtualenvrc
	EOF
	$ mkvirtualenv --python=/usr/bin/python2.7 v27
	$ workon v27
	$ pip install pywapi django sqlite3

###Deployment Python Script Copy the script to some directory, mine is under /root/code/weather. If we want cron to call the virtual environment, we have to create a script and in the script to call the python script.

	#!/bin/bash
	# Export everything.
	source /root/.bashrc
	# Change to the virtual environment
	workon v27
	# Call python
	python /root/code/weather/genhtml.py

Now we add this script to crontab, at every whole point, each hour it will run once.

	crontab -e
	0 */1 * * * /root/code/weather/NJ.sh
	15 */1 * * * /root/code/weather/BJ.sh
	30 */1 * * * /root/code/weather/CS.sh

Now at every 0/15/30, our cron job will be run, and retrieve the data generate the flot image for displaying.

Miragating from ASP to Wordpress(2)

Recent days I am doing a data migration from asp website to WordPress, Following is how-to. ###Database First we have to download the whole website content from the server, in my situation, the website contains a database called “xxxx.mdb”, I downloaded this database and renamed its name to “origin.mdb" We can use Microsoft Access for opening this database file, and from the left side panel we can see all of the tables. We know the about/news1/news2/news3/news4 table which contains the actual webpages content. We can see the picture of news1 which displayed as following: /images/tablesInAccess.jpg ###CSV Preparation In a Ubuntu machine, we install mdbtools via following command:

	$ sudo apt-get install mdbtools

Export the specified tables from the mdb file:

	$ mdb-export origin.mdb news > news.csv

The CSV file is seperated via semicolon, just linke following:

	$ cat news.csv
	id,title,entitle,url,body,enbody,zz,hit,data,ssfl,img,ly,enly,color,encolor,tuijian,px_id,pass,kin
	58,"关于海外游学",,,"<DIV><B><FONT size=2 face=Arial>关于游学</FONT></B></DIV>
	<DIV><FONT size=2><FONT face=Arial><STRONG></STRONG><B></B></FONT></FONT>&n
	............

Now the news.csv contains all of the content which contains in the news table. ###WordPress Plugins Installation Install the WP Ultimate CSV Importer for importing the csv into the WordPress: This Plug-ins is fairly frendly, we simply upload the csv onto the plug-in, the plugin will analyse the csv’s content,and begin to mapping the database fields. /images/csvimport.jpg

Notice: You can import the csv into posts or pages, here I choose “Pages”, and it got the Parent Pages, so first you have to create the parent page, just as following: /images/parentpage.jpg

The content of parent page named “[child_pages]” is also another plug-in, this plug-in is called “Child Pages Shortcode”, after installed it, you can get the “screenshots” of the child pages. The actual effects is listed as following picture: /images/childpageshot.jpg

Another plug-in is called “Children Index Shortcode”. This plug-in will generate all of the child pages’ hyper-links. You can call this plug-in via insert “children-index” into the content of the parent page. ###Change The Theme You can easily install themes in wordpress, via Appearence->Theme. Currently I use “BlackBird Wordpress Theme”, the default suport of chinese is pretty good. Things to be do:

  1. Check all of the images.
  2. Beautify the pages.
  3. Customize the pages.

Migrating from ASP to WordPress(1)

Recently I will moving an old-style website from ASP to WorldPress, that’s a funny work. This series will record the steps for migrating. ###Preparation First I got all of the files which locates in the ASP.NET Server, the database is very simple, it’s a mdb file, which could be viewed by Microsoft Access.

The static files and images could also be downloaded from the server, thus the materials is enough for building a whole-new WorldPress based website.

###Using Python To Generate Content All of the webpage information are located in the mdb file, thus we have to do some dealing with this database file. Python could easily retrieve the data out from the database. Install pyodbc:

	$ pip install pyodbc

Notice when you are using Microsoft Access for visiting the database, the system will automatically generate a ldb file for you.

In Linux, the pyodbc will encouter un-predicted problems, so I use ActivePython in Windows for analyzing the mdb file.

	$ pypm search pyodbc
	$ pypm install pyodbc

Now begin to analyse the database

TBD