1. 程式人生 > >Yeelink初步體驗

Yeelink初步體驗

手機客戶端 雲平臺 嵌入式 物聯網 開發

閱讀目錄(Content)

  • 環境

  • 概述

  • 正文

回到頂部(go to top)

環境

Qemu: 2.8.0

開發板:vexpress-ca9

回到頂部(go to top)

概述

前面的博文已經使我們的虛擬開發板具備了訪問外網的目的,離物聯網越來越近了。要玩物聯網,Yeelink不得不說,它提供了雲平臺,可以將設備的數據上傳到雲端,然後可以通過手機客戶端或在網頁端查看或修改,從而達到用手機客戶端跟嵌入式設備通信的目的。

下面我們實現一個非常簡單的功能: 添加一個虛擬開關,通過Yeelink的手機客戶端或在網頁端可以查看和修改這個開端的狀態,而我們的板子會每隔2s聯網讀取一次這個虛擬開關的狀態,最後板子根據開關的狀態打印不同的提示。

回到頂部(go to top)

正文

一、首先登陸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

解壓後:

$lsHISTORY.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/bash2 3 export CC=arm-none-linux-gnueabi-gcc4 5 ../zlib-1.2.11/configure \6     --prefix=`pwd`

技術分享

修改Makefile,修改AR和RANLIB如下:

AR=arm-none-linux-gnueabi-arRANLIB=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 -j424 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/bash2 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/loop010 sudo losetup /dev/loop0 ./fs_vexpress_1G.img11 sudo partprobe /dev/loop012 13 sudo mkdir rootfs14 sudo cp ${thiry_part}/Busybox/aarch32/_install/*  rootfs/ -raf15 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/ -arf24 25 sudo cp -arf ${cross_compile}/arm-2014.05/arm-none-linux-gnueabi/libc/lib rootfs/26 27 #Python328 sudo mkdir -p rootfs/usr29 pushd rootfs/usr30 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 #Python236 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 popd41 42 #SQlite343 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/share47 48 #Readline49 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 #Zlib53 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 #BeautifulSoup457 sudo cp -raf /home/pengdonglin/disk_ext/Python/BeautifulSoup4/beautifulsoup4-4.5.3/bs4  rootfs/usr/lib/python2.7/site-packages58 sudo cp -raf /home/pengdonglin/disk_ext/Python/BeautifulSoup4/beautifulsoup4-4.5.3/py3k/bs4  rootfs/usr/lib/python3.6/site-packages59 60 #Requests61 sudo cp -raf /home/pengdonglin/disk_ext/Python/Requests/requests-2.3.0/requests  rootfs/usr/lib/python2.7/site-packages62 sudo cp -raf /home/pengdonglin/disk_ext/Python/Requests/requests-2.3.0/requests  rootfs/usr/lib/python3.6/site-packages63 64 #Boa65 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/WebServer/Boa/boa-0.94.13/src/boa rootfs/bin66 sudo mkdir -p rootfs/usr/lib/boa67 sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/WebServer/Boa/boa-0.94.13/src/boa_indexer rootfs/usr/lib/boa68 sudo mkdir -p rootfs/var/www/cgi-bin69 sudo mkdir -p rootfs/var/log/boa70 71 sudo rm -rf rootfs/lib/*.a72 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 180 sudo mknod rootfs/dev/tty2 c 4 281 sudo mknod rootfs/dev/tty3 c 4 382 sudo mknod rootfs/dev/tty4 c 4 483 sudo mknod rootfs/dev/console c 5 184 sudo mknod rootfs/dev/null c 1 385 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 292 sudo umount ./tmpfs/93 94 sudo losetup -d /dev/loop0

技術分享

3、測試

技術分享

 1 [[email protected] ]# 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 [[email protected] ]# python2 8 Python 2.7.13 (default, May  6 2017, 00:11:41) 
 9 [GCC 4.8.3 20140320 (prerelease)] on linux210 Type "help", "copyright", "credits" or "license" for more information.11 >>> import requests12 >>>

技術分享

三、編寫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)

技術分享

四、測試

運行測試程序,然後在手機客戶端或在網頁端修改開關的狀態,測試程序會聯網獲得開關的狀態:

技術分享

完。


Yeelink初步體驗