環境 Qemu: 2.8.0 開發板:vexpress-ca9 概述 前面的博文已經使我們的虛擬開發板具備了訪問外網的目的,離物聯網越來越近了。要玩物聯網,Yeelink不得不說,它提供了雲平臺,可以將設備的數據上傳到雲端,也可以聯網獲得雲端的數據,我們可以通過手機客戶端或在網頁端查看或修改,從而達 ...
環境
Qemu: 2.8.0 開發板:vexpress-ca9概述
前面的博文已經使我們的虛擬開發板具備了訪問外網的目的,離物聯網越來越近了。要玩物聯網,Yeelink不得不說,它提供了雲平臺,可以將設備的數據上傳到雲端,也可以聯網獲得雲端的數據,我們可以通過手機客戶端或在網頁端查看或修改,從而達到用手機客戶端跟嵌入式設備通信的目的。 下麵我們實現一個非常簡單的功能: 添加一個虛擬開關,通過Yeelink的手機客戶端或在網頁端可以查看和修改這個開端的狀態,而我們的板子會每隔2s聯網讀取一次這個虛擬開關的狀態,最後板子根據開關的狀態列印不同的提示。正文
一、首先登陸Yeelink官網,手動添加一個device,然後在該device下麵再創建一個sensor用來代表虛擬開關。 二、為Python添加requests模塊 Requests 是用Python語言編寫,基於urllib,採用Apache2 Licensed開源協議的HTTP庫。它比urllib更加方便,可以節約我們大量的工作,完全滿足HTTP測試需求。 1、下載requests模塊 下載地址: https://pypi.python.org/pypi/requests/2.3.0 或 requests-2.3.0.tar.gz 解壓後:$ls HISTORY.rst NOTICE requests/ setup.cfg LICENSE PKG-INFO requests.egg-info/ setup.py* MANIFEST.in README.rst requirements.txt test_requests.py*
其中的requests既可以用於Python2也可以用於Python3,然後將這個requests目錄拷貝到對應Python的site-packages下麵。
在導入requests模塊的時候,會提示找不到zlib模塊。 2、重新編譯Python,支持zlib 下載zlib: http://www.zlib.net/zlib-1.2.11.tar.gz 或 zlib-1.2.11.tar.gz- 編譯zlib:
1 #!/bin/bash 2 3 export CC=arm-none-linux-gnueabi-gcc 4 5 ../zlib-1.2.11/configure \ 6 --prefix=`pwd`
修改Makefile,修改AR和RANLIB如下:
AR=arm-none-linux-gnueabi-ar RANLIB=arm-none-linux-gnueabi-ranlib然後執行make && make install, 安裝完成之後不要忘了把lib下的so和include下的頭文件拷貝到板子上。
- 編譯Python3:
1 #!/bin/bash 2 3 ../Python-3.6.0/configure \ 4 --host=arm-none-linux-gnueabi \ 5 --build=armv7 \ 6 --prefix=`pwd` \ 7 --enable-ipv6 \ 8 --enable-shared \ 9 ac_cv_file__dev_ptmx="yes" \ 10 ac_cv_file__dev_ptc="no" \ 11 LDFLAGS="-L/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/lib \ 12 -L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \ 13 -L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \ 14 -L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib"\ 15 CPPFLAGS="-I/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/include \ 16 -I/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/include \ 17 -I/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/include \ 18 -I/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/include"\ 19 LDLAST="-L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \ 20 -L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \ 21 -L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib" 22 23 make -j4 24 make install
安裝完成後,也可以直接將lib/python3.6/lib-dynload/zlib.cpython-36m-arm-linux-gnueabi.so拷貝到板子上面的/usr/lib/python3.6/lib-dynload/下麵即可, 而不必全部更新
- 編譯Python2:
1 #!/bin/bash 2 3 ../Python-2.7.13/configure --prefix=`pwd` \ 4 --host=arm-none-linux-gnueabi \ 5 --build=x86_64-linux-gnu \ 6 --enable-ipv6 \ 7 --enable-shared \ 8 ac_cv_file__dev_ptmx="yes" \ 9 ac_cv_file__dev_ptc="no" \ 10 LDFLAGS="-L/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/lib \ 11 -L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \ 12 -L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \ 13 -L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib"\ 14 CPPFLAGS="-I/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/include \ 15 -I/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/include \ 16 -I/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/include \ 17 -I/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/include"\ 18 LDLAST="-L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \ 19 -L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \ 20 -L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib"\mk2_make.sh:
1 #!/bin/bash 2 3 make HOSTPYTHON=../x86_64/python \ 4 HOSTPGEN=../x86_64/Parser/pgen \ 5 BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \ 6 CROSS_COMPILE=arm-none-linux-gnueabi- \ 7 CROSS_COMPILE_TARGET=yes \ 8 HOSTARCH=arm-none-linux-gnueabi \ 9 BUILDARCH=x86_64-linux-gnu \ 10 -j4mk3_install.sh:
1 #!/bin/bash 2 3 make install HOSTPYTHON=../x86_64/python \ 4 BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \ 5 CROSS_COMPILE=arm-none-linux-gnueabi- \ 6 CROSS_COMPILE_TARGET=yes \ 7 prefix=`pwd`
安裝完成後,也可以直接將lib/python2.7/lib-dynload/zlib.so拷貝到開發板的/usr/lib/python2.7/lib-dynload/下麵,而不用全部更新
- 然後修改mk_ramdisk.sh:
1 #!/bin/bash 2 3 thiry_part=/home/pengdonglin/disk_ext/Third_Part/ 4 cross_compile=/home/pengdonglin/disk_ext/Cross_Compiler/aarch32/ 5 6 sudo rm -rf rootfs 7 sudo rm -rf tmpfs 8 sudo rm -rf ramdisk* 9 sudo losetup -d /dev/loop0 10 sudo losetup /dev/loop0 ./fs_vexpress_1G.img 11 sudo partprobe /dev/loop0 12 13 sudo mkdir rootfs 14 sudo cp ${thiry_part}/Busybox/aarch32/_install/* rootfs/ -raf 15 16 sudo mkdir -p rootfs/proc/ 17 sudo mkdir -p rootfs/sys/ 18 sudo mkdir -p rootfs/tmp/ 19 sudo mkdir -p rootfs/root/ 20 sudo mkdir -p rootfs/var/ 21 sudo mkdir -p rootfs/mnt/ 22 23 sudo cp etc rootfs/ -arf 24 25 sudo cp -arf ${cross_compile}/arm-2014.05/arm-none-linux-gnueabi/libc/lib rootfs/ 26 27 #Python3 28 sudo mkdir -p rootfs/usr 29 pushd rootfs/usr 30 sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/bin . 31 sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/lib . 32 sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/include . 33 sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/share . 34 35 #Python2 36 sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/bin . 37 sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/lib . 38 sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/include . 39 sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/share . 40 popd 41 42 #SQlite3 43 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/bin/* rootfs/bin/ 44 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/include/* rootfs/include/ 45 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/lib/* rootfs/lib/ 46 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/share/* rootfs/usr/share 47 48 #Readline 49 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/include/ rootfs/include/ 50 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib/* rootfs/lib/ 51 52 #Zlib 53 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/include/* rootfs/include/ 54 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib/* rootfs/lib/ 55 56 #BeautifulSoup4 57 sudo cp -raf /home/pengdonglin/disk_ext/Python/BeautifulSoup4/beautifulsoup4-4.5.3/bs4 rootfs/usr/lib/python2.7/site-packages 58 sudo cp -raf /home/pengdonglin/disk_ext/Python/BeautifulSoup4/beautifulsoup4-4.5.3/py3k/bs4 rootfs/usr/lib/python3.6/site-packages 59 60 #Requests 61 sudo cp -raf /home/pengdonglin/disk_ext/Python/Requests/requests-2.3.0/requests rootfs/usr/lib/python2.7/site-packages 62 sudo cp -raf /home/pengdonglin/disk_ext/Python/Requests/requests-2.3.0/requests rootfs/usr/lib/python3.6/site-packages 63 64 #Boa 65 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/WebServer/Boa/boa-0.94.13/src/boa rootfs/bin 66 sudo mkdir -p rootfs/usr/lib/boa 67 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/WebServer/Boa/boa-0.94.13/src/boa_indexer rootfs/usr/lib/boa 68 sudo mkdir -p rootfs/var/www/cgi-bin 69 sudo mkdir -p rootfs/var/log/boa 70 71 sudo rm -rf rootfs/lib/*.a 72 sudo arm-none-linux-gnueabi-strip rootfs/lib/* rootfs/bin/* 73 sudo arm-none-linux-gnueabi-strip rootfs/usr/lib/boa/* 74 sudo arm-none-linux-gnueabi-strip rootfs/usr/lib/* 75 sudo arm-none-linux-gnueabi-strip rootfs/usr/bin/* 76 sudo arm-none-linux-gnueabi-strip rootfs/bin/* 77 78 sudo mkdir -p rootfs/dev/ 79 sudo mknod rootfs/dev/tty1 c 4 1 80 sudo mknod rootfs/dev/tty2 c 4 2 81 sudo mknod rootfs/dev/tty3 c 4 3 82 sudo mknod rootfs/dev/tty4 c 4 4 83 sudo mknod rootfs/dev/console c 5 1 84 sudo mknod rootfs/dev/null c 1 3 85 86 sudo mkdir ./tmpfs/ 87 sudo mount -t ext4 /dev/loop0p1 ./tmpfs/ 88 sudo rm -rf ./tmpfs/* 89 sudo cp -raf rootfs/* ./tmpfs/ 90 91 sleep 2 92 sudo umount ./tmpfs/ 93 94 sudo losetup -d /dev/loop0
3、測試
1 [root@vexpress ]# python3 2 Python 3.6.0 (default, May 6 2017, 00:08:14) 3 [GCC 4.8.3 20140320 (prerelease)] on linux 4 Type "help", "copyright", "credits" or "license" for more information. 5 >>> import requests 6 >>> 7 [root@vexpress ]# python2 8 Python 2.7.13 (default, May 6 2017, 00:11:41) 9 [GCC 4.8.3 20140320 (prerelease)] on linux2 10 Type "help", "copyright", "credits" or "license" for more information. 11 >>> import requests 12 >>>三、編寫python程式獲取開關狀態 get_switch.py2:
1 #!/usr/bin/env python2 2 3 import requests 4 import time 5 6 apiurl = "http://api.yeelink.net/v1.0/device/7445/sensor/405914/datapoints" 7 apiheaders = {'U-ApiKey':'3957bd54eaddd2fda1f35841d52776d9'} 8 9 while True: 10 r = requests.get(apiurl, headers=apiheaders) 11 print(r.text) 12 sw = r.json() 13 if sw['value'] == 1: 14 print("Switch On") 15 elif sw['value'] == 0: 16 print("Switch Off") 17 time.sleep(2)get_switch.py3:
1 #!/usr/bin/env python3 2 3 import requests 4 import time 5 6 apiurl = "http://api.yeelink.net/v1.0/device/7445/sensor/405914/datapoints" 7 apiheaders = {'U-ApiKey':'3957bd54eaddd2fda1f35841d52776d9'} 8 9 while True: 10 r = requests.get(apiurl, headers=apiheaders) 11 print(r.text) 12 sw = r.json() 13 if sw['value'] == 1: 14 print("Switch On") 15 elif sw['value'] == 0: 16 print("Switch Off") 17 time.sleep(2)
四、測試 運行測試程式,然後在手機客戶端或在網頁端修改開關的狀態,測試程式會聯網獲得開關的狀態: 完。