blinker App连接树莓派控制led

python版本:3.8.1,低于等于3.7的安装blinker库好像不支持。

blinker版本:0.3.0

树莓派4B raspbian系统

一.安装python3.8.1

1.国内找个源下载python3.8.1

2.解压

tar zxvf Python-3.8.0.tgz

3.执行配置文件

cd Python-3.8.1

sudo ./configure

4.安装

sudo make install

二.安装blinker

1.克隆blinker的python库:

git clone https://github.com/blinker-iot/blinker-py

cd blinker-py

2.安装依赖

sudo pip3.8 install -r requirements.txt

3.安装库

sudo python3.8 setup.py install

sudo pip3.8 install --upgrade .

这一步用python3.7会报错ModuleNotFoundError: No module named '_ctypes'

三.源码

直接用点灯科技的wifi例程就好:

from blinker import Device,ButtonWidget,NumberWidget
import RPi.GPIO as GPIO

auth = "3bc1c2e1c567" 

device = Device(auth)

GPIO.setmode(GPIO.BCM) #参考gpio编程手册,用板载序号方便编程,也可以用BCM编码
GPIO.setwarnings(False)
GPIO.setup(27,GPIO.OUT,initial=GPIO.LOW)
 
button1 = device.addWidget(ButtonWidget('btn-123'))
button2 = device.addWidget(ButtonWidget('btn-abc'))
number1 = device.addWidget(NumberWidget('num-abc'))

num = 0
light_State = 0

async def button1_callback(msg):
    global num
    num += 1
    await number1.text("num").value(num).update()


async def button2_callback(msg):
    global light_State
    light_State = 1-light_State
    GPIO.output(27,light_State)

async def heartbeat_func(msg):
    print("Heartbeat func received: {0}".format(msg))

async def ready_func():
    # 获取设备配置信息
    print(vars(device.config))

button1.func = button1_callback
button2.func = button2_callback

device.heartbeat_callable = heartbeat_func
device.ready_callable = ready_func

if __name__ == '__main__':
    while True:
        device.run()

auth是在app上添加设备提供的凭证。

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值