首页 > 代码库 > 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
解压后:
  1. $ls
  2. HISTORY.rst NOTICE requests/ setup.cfg
  3. LICENSE PKG-INFO requests.egg-info/ setup.py*
  4. 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. export CC=arm-none-linux-gnueabi-gcc
  3. ../zlib-1.2.11/configure \
  4. --prefix=`pwd`
修改Makefile,修改AR和RANLIB如下:
  1. AR=arm-none-linux-gnueabi-ar
  2. RANLIB=arm-none-linux-gnueabi-ranlib
然后执行make && make install
  • 编译Python3:
  1. #!/bin/bash
  2. ../Python-3.6.0/configure \
  3. --host=arm-none-linux-gnueabi \
  4. --build=armv7 \
  5. --prefix=`pwd` \
  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"
  21. make -j4
  22. make install
  • 编译Python2:
mk1_conf.sh:
  1. #!/bin/bash
  2. ../Python-2.7.13/configure --prefix=`pwd` \
  3. --host=arm-none-linux-gnueabi \
  4. --build=x86_64-linux-gnu \
  5. --enable-ipv6 \
  6. --enable-shared \
  7. ac_cv_file__dev_ptmx="yes" \
  8. ac_cv_file__dev_ptc="no" \
  9. LDFLAGS="-L/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/lib \
  10. -L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \
  11. -L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \
  12. -L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib"\
  13. CPPFLAGS="-I/home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/include \
  14. -I/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/include \
  15. -I/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/include \
  16. -I/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/include"\
  17. LDLAST="-L/home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib \
  18. -L/home/pengdonglin/disk_ext/Third_Part/Termcap/aarch32/lib \
  19. -L/home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib"\
mk2_make.sh:
  1. #!/bin/bash
  2. make HOSTPYTHON=../x86_64/python \
  3. HOSTPGEN=../x86_64/Parser/pgen \
  4. BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \
  5. CROSS_COMPILE=arm-none-linux-gnueabi- \
  6. CROSS_COMPILE_TARGET=yes \
  7. HOSTARCH=arm-none-linux-gnueabi \
  8. BUILDARCH=x86_64-linux-gnu \
  9. -j4
mk3_install.sh:
  1. #!/bin/bash
  2. make install HOSTPYTHON=../x86_64/python \
  3. BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \
  4. CROSS_COMPILE=arm-none-linux-gnueabi- \
  5. CROSS_COMPILE_TARGET=yes \
  6. prefix=`pwd`
  • 然后修改mk_ramdisk.sh:
  1. #!/bin/bash
  2. thiry_part=/home/pengdonglin/disk_ext/Third_Part/
  3. cross_compile=/home/pengdonglin/disk_ext/Cross_Compiler/aarch32/
  4. sudo rm -rf rootfs
  5. sudo rm -rf tmpfs
  6. sudo rm -rf ramdisk*
  7. sudo losetup -d /dev/loop0
  8. sudo losetup /dev/loop0 ./fs_vexpress_1G.img
  9. sudo partprobe /dev/loop0
  10. sudo mkdir rootfs
  11. sudo cp ${thiry_part}/Busybox/aarch32/_install/* rootfs/ -raf
  12. sudo mkdir -p rootfs/proc/
  13. sudo mkdir -p rootfs/sys/
  14. sudo mkdir -p rootfs/tmp/
  15. sudo mkdir -p rootfs/root/
  16. sudo mkdir -p rootfs/var/
  17. sudo mkdir -p rootfs/mnt/
  18. sudo cp etc rootfs/ -arf
  19. sudo cp -arf ${cross_compile}/arm-2014.05/arm-none-linux-gnueabi/libc/lib rootfs/
  20. #Python3
  21. sudo mkdir -p rootfs/usr
  22. pushd rootfs/usr
  23. sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/bin .
  24. sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/lib .
  25. sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/include .
  26. sudo cp -raf /home/pengdonglin/disk_ext/Python/Python3/aarch32/share .
  27. #Python2
  28. sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/bin .
  29. sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/lib .
  30. sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/include .
  31. sudo cp -raf /home/pengdonglin/disk_ext/Python/Python2/aarch32/share .
  32. popd
  33. #SQlite3
  34. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/bin/* rootfs/bin/
  35. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/include/* rootfs/include/
  36. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/lib/* rootfs/lib/
  37. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/SQlite3/aarch32/share/* rootfs/usr/share
  38. #Readline
  39. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/include/ rootfs/include/
  40. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Readline/aarch32/lib/* rootfs/lib/
  41. #Zlib
  42. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/include/* rootfs/include/
  43. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/Zlib/aarch32/lib/* rootfs/lib/
  44. #BeautifulSoup4
  45. sudo cp -raf /home/pengdonglin/disk_ext/Python/BeautifulSoup4/beautifulsoup4-4.5.3/bs4 rootfs/usr/lib/python2.7/site-packages
  46. sudo cp -raf /home/pengdonglin/disk_ext/Python/BeautifulSoup4/beautifulsoup4-4.5.3/py3k/bs4 rootfs/usr/lib/python3.6/site-packages
  47. #Requests
  48. sudo cp -raf /home/pengdonglin/disk_ext/Python/Requests/requests-2.3.0/requests rootfs/usr/lib/python2.7/site-packages
  49. sudo cp -raf /home/pengdonglin/disk_ext/Python/Requests/requests-2.3.0/requests rootfs/usr/lib/python3.6/site-packages
  50. #Boa
  51. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/WebServer/Boa/boa-0.94.13/src/boa rootfs/bin
  52. sudo mkdir -p rootfs/usr/lib/boa
  53. sudo cp -raf /home/pengdonglin/disk_ext/Third_Part/WebServer/Boa/boa-0.94.13/src/boa_indexer rootfs/usr/lib/boa
  54. sudo mkdir -p rootfs/var/www/cgi-bin
  55. sudo mkdir -p rootfs/var/log/boa
  56. sudo rm -rf rootfs/lib/*.a
  57. sudo arm-none-linux-gnueabi-strip rootfs/lib/* rootfs/bin/*
  58. sudo arm-none-linux-gnueabi-strip rootfs/usr/lib/boa/*
  59. sudo arm-none-linux-gnueabi-strip rootfs/usr/lib/*
  60. sudo arm-none-linux-gnueabi-strip rootfs/usr/bin/*
  61. sudo arm-none-linux-gnueabi-strip rootfs/bin/*
  62. sudo mkdir -p rootfs/dev/
  63. sudo mknod rootfs/dev/tty1 c 4 1
  64. sudo mknod rootfs/dev/tty2 c 4 2
  65. sudo mknod rootfs/dev/tty3 c 4 3
  66. sudo mknod rootfs/dev/tty4 c 4 4
  67. sudo mknod rootfs/dev/console c 5 1
  68. sudo mknod rootfs/dev/null c 1 3
  69. sudo mkdir ./tmpfs/
  70. sudo mount -t ext4 /dev/loop0p1 ./tmpfs/
  71. sudo rm -rf ./tmpfs/*
  72. sudo cp -raf rootfs/* ./tmpfs/
  73. sleep 2
  74. sudo umount ./tmpfs/
  75. 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. import requests
  3. import time
  4. apiurl = "http://api.yeelink.net/v1.0/device/7445/sensor/405914/datapoints"
  5. apiheaders = {‘U-ApiKey‘:‘3957bd54eaddd2fda1f35841d52776d9‘}
  6. while True:
  7. r = requests.get(apiurl, headers=apiheaders)
  8. print(r.text)
  9. sw = r.json()
  10. if sw[‘value‘] == 1:
  11. print("Switch On")
  12. elif sw[‘value‘] == 0:
  13. print("Switch Off")
  14. time.sleep(2)
get_switch.py3:
  1. #!/usr/bin/env python3
  2. import requests
  3. import time
  4. apiurl = "http://api.yeelink.net/v1.0/device/7445/sensor/405914/datapoints"
  5. apiheaders = {‘U-ApiKey‘:‘3957bd54eaddd2fda1f35841d52776d9‘}
  6. while True:
  7. r = requests.get(apiurl, headers=apiheaders)
  8. print(r.text)
  9. sw = r.json()
  10. if sw[‘value‘] == 1:
  11. print("Switch On")
  12. elif sw[‘value‘] == 0:
  13. print("Switch Off")
  14. time.sleep(2)
四、测试
运行测试程序,然后在手机客户端或在网页端修改开关的状态:
技术分享

完。

Yeelink初步体验