Raspberry pi 使用 blynk fot python
安装pip包管理工具
sudo apt-get install python3-pip
安装Python client for Blynk
pip3 install blynk-library-python
由于使用Blynk本地服务器,需要修改库文件的服务器IP,找出文件路径
#查看pip安装列表,安装路径
pip3 list
pip3 show blynk-library-python
License: MIT
Location: /home/pi/.local/lib/python3.7/site-packages
找到路径,编辑库文件,修改服务器IP、端口
cd /home/pi/.local/lib/python3.7/site-packages
nano BlynkLib.py
import socket class Blynk(BlynkProtocol): def __init__(self, auth, **kwargs): self.server = kwargs.pop('server', '192.168.1.30') self.port = kwargs.pop('port', 8080) BlynkProtocol.__init__(self, auth, **kwargs)
试运行例子 python3 1.py,Token在手机app上查看(点击复制):Devices-AUTH TOKEN
import BlynkLib # Initialize Blynk blynk = BlynkLib.Blynk('YourAuthToken') # Register Virtual Pins @blynk.VIRTUAL_WRITE(1) def my_write_handler(value): print('Current V1 value: {}'.format(value)) @blynk.VIRTUAL_READ(2) def my_read_handler(): # this widget will show some time in seconds.. blynk.virtual_write(2, int(time.time())) while True: blynk.run()
顺利连接上服务器,手机上显示设备在线。
___ __ __ / _ )/ /_ _____ / /__ / _ / / // / _ \/ '_/ /____/_/\_, /_//_/_/\_\ /___/ for Python v0.2.0 (Linux)