首页 > 代码库 > Yeelink初步体验
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:
#!/bin/bash
export CC=arm-none-linux-gnueabi-gcc
../zlib-1.2.11/configure \
--prefix=`pwd`
修改Makefile,修改AR和RANLIB如下:
AR=arm-none-linux-gnueabi-ar
RANLIB=arm-none-linux-gnueabi-ranlib
然后执行make && make install
- 编译Python3:
#!/bin/bash
../Python-3.6.0/configure \
--host=arm-none-linux-gnueabi \
--build=armv7 \
--prefix=`pwd` \
--enable-ipv6 \
--enable-shared \
ac_cv_file__dev_ptmx="yes" \
ac_cv_file__dev_ptc="no" \
LDFLAGS="-L/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib"\
CPPFLAGS="-I/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/include \
-I/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/include \
-I/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/include \
-I/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/include"\
LDLAST="-L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib"
make -j4
make install
- 编译Python2:
mk1_conf.sh:
#!/bin/bash
../Python-2.7.13/configure --prefix=`pwd` \
--host=arm-none-linux-gnueabi \
--build=x86_64-linux-gnu \
--enable-ipv6 \
--enable-shared \
ac_cv_file__dev_ptmx="yes" \
ac_cv_file__dev_ptc="no" \
LDFLAGS="-L/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib"\
CPPFLAGS="-I/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/include \
-I/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/include \
-I/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/include \
-I/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/include"\
LDLAST="-L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \
-L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib"\
mk2_make.sh:
#!/bin/bash
make HOSTPYTHON=../x86_64/python \
HOSTPGEN=../x86_64/Parser/pgen \
BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \
CROSS_COMPILE=arm-none-linux-gnueabi- \
CROSS_COMPILE_TARGET=yes \
HOSTARCH=arm-none-linux-gnueabi \
BUILDARCH=x86_64-linux-gnu \
-j4
mk3_install.sh:
#!/bin/bash
make install HOSTPYTHON=../x86_64/python \
BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \
CROSS_COMPILE=arm-none-linux-gnueabi- \
CROSS_COMPILE_TARGET=yes \
prefix=`pwd`
- 然后修改mk_ramdisk.sh:
#!/bin/bash
thiry_part=/home/pengdonglin/disk_ext/Third_Part/
cross_compile=/home/pengdonglin/disk_ext/Cross_Compiler/aarch32/
sudo rm -rf rootfs
sudo rm -rf tmpfs
sudo rm -rf ramdisk*
sudo losetup -d /dev/loop0
sudo losetup /dev/loop0 ./fs_vexpress_1G.img
sudo partprobe /dev/loop0
sudo mkdir rootfs
sudo cp ${thiry_part}/Busybox/aarch32/_install/* rootfs/ -raf
sudo mkdir -p rootfs/proc/
sudo mkdir -p rootfs/sys/
sudo mkdir -p rootfs/tmp/
sudo mkdir -p rootfs/root/
sudo mkdir -p rootfs/var/
sudo mkdir -p rootfs/mnt/
sudo cp etc rootfs/ -arf
sudo cp -arf ${cross_compile}/arm-2014.05/arm-none-linux-gnueabi/libc/lib rootfs/
#Python3
sudo mkdir -p rootfs/usr
pushd rootfs/usr
sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/bin .
sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/lib .
sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/include .
sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/share .
#Python2
sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/bin .
sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/lib .
sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/include .
sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/share .
popd
#SQlite3
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/bin/* rootfs/bin/
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/include/* rootfs/include/
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/lib/* rootfs/lib/
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/share/* rootfs/usr/share
#Readline
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/include/ rootfs/include/
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib/* rootfs/lib/
#Zlib
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/include/* rootfs/include/
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib/* rootfs/lib/
#BeautifulSoup4
sudo cp -raf /home/pengdonglin/disk_ext/Python/BeautifulSoup4/beautifulsoup4-4.5.3/bs4 rootfs/usr/lib/python2.7/site-packages
sudo cp -raf /home/pengdonglin/disk_ext/Python/BeautifulSoup4/beautifulsoup4-4.5.3/py3k/bs4 rootfs/usr/lib/python3.6/site-packages
#Requests
sudo cp -raf /home/pengdonglin/disk_ext/Python/Requests/requests-2.3.0/requests rootfs/usr/lib/python2.7/site-packages
sudo cp -raf /home/pengdonglin/disk_ext/Python/Requests/requests-2.3.0/requests rootfs/usr/lib/python3.6/site-packages
#Boa
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/WebServer/Boa/boa-0.94.13/src/boa rootfs/bin
sudo mkdir -p rootfs/usr/lib/boa
sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/WebServer/Boa/boa-0.94.13/src/boa_indexer rootfs/usr/lib/boa
sudo mkdir -p rootfs/var/www/cgi-bin
sudo mkdir -p rootfs/var/log/boa
sudo rm -rf rootfs/lib/*.a
sudo arm-none-linux-gnueabi-strip rootfs/lib/* rootfs/bin/*
sudo arm-none-linux-gnueabi-strip rootfs/usr/lib/boa/*
sudo arm-none-linux-gnueabi-strip rootfs/usr/lib/*
sudo arm-none-linux-gnueabi-strip rootfs/usr/bin/*
sudo arm-none-linux-gnueabi-strip rootfs/bin/*
sudo mkdir -p rootfs/dev/
sudo mknod rootfs/dev/tty1 c 4 1
sudo mknod rootfs/dev/tty2 c 4 2
sudo mknod rootfs/dev/tty3 c 4 3
sudo mknod rootfs/dev/tty4 c 4 4
sudo mknod rootfs/dev/console c 5 1
sudo mknod rootfs/dev/null c 1 3
sudo mkdir ./tmpfs/
sudo mount -t ext4 /dev/loop0p1 ./tmpfs/
sudo rm -rf ./tmpfs/*
sudo cp -raf rootfs/* ./tmpfs/
sleep 2
sudo umount ./tmpfs/
sudo losetup -d /dev/loop0
3、测试
[root@vexpress ]# python3
Python 3.6.0 (default, May 6 2017, 00:08:14)
[GCC 4.8.3 20140320 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>
[root@vexpress ]# python2
Python 2.7.13 (default, May 6 2017, 00:11:41)
[GCC 4.8.3 20140320 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>
三、编写python程序获取开关状态
get_switch.py2:
#!/usr/bin/env python2
import requests
import time
apiurl = "http://api.yeelink.net/v1.0/device/7445/sensor/405914/datapoints"
apiheaders = {‘U-ApiKey‘:‘3957bd54eaddd2fda1f35841d52776d9‘}
while True:
r = requests.get(apiurl, headers=apiheaders)
print(r.text)
sw = r.json()
if sw[‘value‘] == 1:
print("Switch On")
elif sw[‘value‘] == 0:
print("Switch Off")
time.sleep(2)
get_switch.py3:
#!/usr/bin/env python3
import requests
import time
apiurl = "http://api.yeelink.net/v1.0/device/7445/sensor/405914/datapoints"
apiheaders = {‘U-ApiKey‘:‘3957bd54eaddd2fda1f35841d52776d9‘}
while True:
r = requests.get(apiurl, headers=apiheaders)
print(r.text)
sw = r.json()
if sw[‘value‘] == 1:
print("Switch On")
elif sw[‘value‘] == 0:
print("Switch Off")
time.sleep(2)
四、测试
运行测试程序,然后在手机客户端或在网页端修改开关的状态:
完。
Yeelink初步体验
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。