1. 目标
本文以树莓派为硬件,搭建一个本地的httpserver,用于产品中的各种测试。树莓派有了httpserver后,可以跨平台访问,实现了跨平台访问硬件的目标。
2. 清单
这里列举所需要的清单:
- 2016-9-28 jessie-lite树莓派系统镜像
- Qt5+boost+opencv
- xvfb,实现后台运行带窗口的界面
- daemontool管理后台服务器
- wifi-access-point脚本,一键设置树莓派为wifi热点
3. 步骤
上述的开发环境,我已将他们全部集成到一个shell脚本中,可在httpserver下,可以一键设置好开发环境。:
1. 配置开发环境
#!/bin/bash
#
# This version uses September 2016 rpi jessie image, please use this image
#
if [ "$EUID" -ne 0 ]
then echo "Must be root"
exit
fi
if [[ $# -lt 1 ]];
then echo "You need to pass a password!"
echo "Usage:"
echo "sudo $0 yourChosenPassword [apName]"
exit
fi
APPASS="$1"
APSSID="rPi3"
if [[ $# -eq 2 ]]; then
APSSID=$2
fi
apt-get remove --purge hostapd -y
apt-get install hostapd dnsmasq -y
cat > /etc/systemd/system/hostapd.service <<EOF
[Unit]
Description=Hostapd IEEE 802.11 Access Point
After=sys-subsystem-net-devices-wlan0.device
BindsTo=sys-subsystem-net-devices-wlan0.device
[Service]
Type=forking
PIDFile=/var/run/hostapd.pid
ExecStart=/usr/sbin/hostapd -B /etc/hostapd/hostapd.conf -P /var/run/hostapd.pid
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/dnsmasq.conf <<EOF
interface=wlan0
dhcp-range=10.0.0.2,10.0.0.5,255.255.255.0,12h
EOF
cat > /etc/hostapd/hostapd.conf <<EOF
interface=wlan0
hw_mode=g
channel=10
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
wpa_passphrase=$APPASS
ssid=$APSSID
EOF
sed -i -- 's/allow-hotplug wlan0//g' /etc/network/interfaces
sed -i -- 's/iface wlan0 inet manual//g' /etc/network/interfaces
sed -i -- 's/ wpa-conf \/etc\/wpa_supplicant\/wpa_supplicant.conf//g' /etc/network/interfaces
cat >> /etc/network/interfaces <<EOF
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
# Added by rPi Access Point Setup
allow-hotplug wlan0
iface wlan0 inet static
address 10.0.0.1
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
EOF
echo "denyinterfaces wlan0" >> /etc/dhcpcd.conf
systemctl enable hostapd
sudo apt-get install qt5-default
sudo apt-get install qtcreator
sudo apt-get install libqt5svg5* libqt5webkit5* libqt5serialport5*
sudo apt-get install qtcreator
sudo apt-get install libboost1.50-all
sudo apt-get install xvfb
sudo apt-get install daemontools daemontools-run
echo "All done! Please reboot"
2. 设置后台运行脚本
使用xvfb,可以在没有桌面系统的情况下,照样调用Qt的一些窗口库(由于应用中,要用到一些带窗口的类的功能)。调用execut.sh脚本如下:
#!/bin/sh
echo "Execut the service by daemontools"
echo "Next will start the linebot"
cd /home/pi/raspberrylinebot/build/
echo "Really exec the linebot"
sudo xvfb-run --server-args="-screen 0 1024x768x24" ./LineBot
echo "The linebot has finish started!"
下一步就是设置daemontools来对后台程序进行管理。当后台的Qt应用程序自己当掉了,daemontools会自动在5秒后重启应用。
sudo mkdir -p /service/yourapp-serice
sudo vi /service/youapp-service/run
sudo chmod 755 /service/youapp-service/run
cd /etc/service/
sudo ln -s /service/yourapp-service .
在/service/youapp-service/run中,run是一个shell脚本,在这里调用使用exec来调用上一步骤中的execut.sh脚本。
3. 备份你的系统
使用备份脚本:
#!/bin/bash
VERSION=1.5
# Version 1.5 2016/09/09
# * Remove any leading /dev/ from dest disk.
# * Warn dest disk may be a partition if it ends with a digit.
# Version 1.4 2016/03/23
# * Use -F on mkfs.ext4 and avoid the question.
# Version 1.3 2016/03/16
# * don't > /dev/null for the mkfs.ext4 since mkfs can ask a question
# Version 1.2 2015/02/17
# * add -x option
# * tweak some echo messages
# Version 1.1 2015/02/16
# * Do not include dphys swapfile in the clone.
# * Add a missing -s flag to one of the parted commands.
# * Dump parted stderr to /dev/null since it now complains about destination
# disk partitions ending outside of disk before I get chance to resize
# the partitions.
# * Strip trailing s from PART_START - (fdisk now doesn't seem to accept...)
#
PGM=`basename $0`
RSYNC_OPTIONS="--force -rltWDEgopt"
# List of extra dirs to create under /mnt.
OPTIONAL_MNT_DIRS="clone mnt sda sdb rpi0 rpi1"
# Where to mount the disk filesystems to be rsynced.
CLONE=/mnt/clone
CLONE_LOG=/var/log/$PGM.log
HOSTNAME=`hostname`
SRC_BOOT_PARTITION_TYPE=`parted /dev/mmcblk0 -ms p | grep "^1" | cut -f 5 -d:`
SRC_ROOT_PARTITION_TYPE=`parted /dev/mmcblk0 -ms p | grep "^2" | cut -f 5 -d:`
if [ `id -u` != 0 ]
then
echo -e "$PGM needs to be run as root.\n"
exit 1
fi
if ! rsync --version > /dev/null
then
echo -e "\nOoops! rpi-clone needs the rsync program but cannot find it."
echo "Make sure rsync is installed:"