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模块
解压后:
$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:
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:
mk1_conf.sh:
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 -j4
mk3_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)
四、测试
运行测试程序,然后在手机客户端或在网页端修改开关的状态,测试程序会联网获得开关的状态:
完。
本文来自博客园,作者:摩斯电码,未经同意,禁止转载