本文主要是介绍天猫精灵、Blinker 控制esp32自带灯熄灭---Micropython版本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
参考:https://github.com/blinker-iot/blinker-mpy
https://diandeng.tech/doc/mpy-support
WeChat_20220812205426
1、首先去上面github把源码下载,然后对应这几个目录传到esp32上去
tianmao.py是主函数
tianmao.py源码
#!/usr/bin/env python
# -*- coding: utf-8 -*-from machine import Pinfrom Blinker.Blinker import Blinker, BlinkerButton, BlinkerNumber, BlinkerAliGenie
from Blinker.BlinkerConfig import *
from Blinker.BlinkerDebug import *#from BlinkerAdapters.BlinkerWiFi import MQTTClientsprint(BlinkerAliGenie.payload)
# 此处填入blinker app中获取的密钥
auth = 'Your Device Secret Key'ssid = 'Your WiFi network SSID or name'pswd = 'Your WiFi network WPA password or WEP key'BLINKER_DEBUG.debugAll()Blinker.mode('BLINKER_WIFI')
Blinker.aliType('BLINKER_ALIGENIE_LIGHT')
Blinker.begin(auth, ssid, pswd)button1 = BlinkerButton('btn-abc')
number1 = BlinkerNumber('num-abc')counter = 0
pinValue = 0
oState = 'on'p2 = Pin(2, Pin.OUT)
p2.value(pinValue)def aligeniePowerState(state):global oStateprint("#"*8,state)BLINKER_LOG('need set power state: ', state)if state == BLINKER_CMD_ON:p2.value(1)BlinkerAliGenie.powerState("on")BlinkerAliGenie.print()oState = 'on'print(BLINKER_CMD_ON,"aaaaaaa")elif state == BLINKER_CMD_OFF:p2.value(0)BlinkerAliGenie.powerState("off")BlinkerAliGenie.print()oState = 'off'print(BLINKER_CMD_OFF,"sssssss")def aligenieQuery(queryCode):global oStateBLINKER_LOG('AliGenie Query codes: ', queryCode)print("*"*8,queryCode)if queryCode == BLINKER_CMD_QUERY_ALL_NUMBER :BLINKER_LOG('AliGenie Query All')BlinkerAliGenie.powerState(oState)BlinkerAliGenie.print()elif queryCode == BLINKER_CMD_QUERY_POWERSTATE_NUMBER :BLINKER_LOG('AliGenie Query Power State')BlinkerAliGenie.powerState(oState)BlinkerAliGenie.print()else :BlinkerAliGenie.powerState(oState)BlinkerAliGenie.print()def button1_callback(state):''' '''print("&"*8)BLINKER_LOG('get button state: ', state)button1.icon('icon_1')button1.color('#FFFFFF')button1.text('Your button name or describe')button1.print(state)global pinValuepinValue = 1 - pinValuep2.value(pinValue)def data_callback(data):global counterprint("+"*8)BLINKER_LOG('Blinker readString: ', data)counter += 1number1.print(counter)#print(Blinker.aliParse())
button1.attach(button1_callback)
Blinker.attachData(data_callback)#BlinkerAliGenie.attachColor(aligenieColor)
BlinkerAliGenie.attachPowerState(aligeniePowerState)
BlinkerAliGenie.attachQuery(aligenieQuery)if __name__ == '__main__':#msg = MQTTClients()#msg = Blinker.aliParse()while True:Blinker.run()
2、blinker软件设置与天猫精灵app设置
blinker软件:
下载注册,创建设备,记录秘钥
天猫精灵app:
添加设备搜索blinker,点击点灯科技绑定同步设备即可
3、源码运行问题,因为这个mpy版本基本没有维护,运行后天猫精灵语音是控制不了,需要更改BlinkerAdapters下的BlinkerWiFi.py文件
——————————————————————————————
还有其他方法不用自带的回调函数;自己定义去处理天猫精灵云端下来的指令
BlinkerAliGenie.attachPowerState(aligeniePowerState)
BlinkerAliGenie.attachQuery(aligenieQuery)
方法还是改动两个文件,这两个也是上面的两个文件:
1)tianmao.py
注释掉这天猫精灵相关的两个回调函数
#!/usr/bin/env python
# -*- coding: utf-8 -*-from machine import Pinfrom Blinker.Blinker import Blinker, BlinkerButton, BlinkerNumber, BlinkerAliGenie
from Blinker.BlinkerConfig import *
from Blinker.BlinkerDebug import *#from BlinkerAdapters.BlinkerWiFi import MQTTClientsprint(BlinkerAliGenie.payload)
# 此处填入blinker app中获取的密钥
auth = 'Your Device Secret Key'ssid = 'Your WiFi network SSID or name'pswd = 'Your WiFi network WPA password or WEP key'BLINKER_DEBUG.debugAll()Blinker.mode('BLINKER_WIFI')
Blinker.aliType('BLINKER_ALIGENIE_LIGHT')
Blinker.begin(auth, ssid, pswd)button1 = BlinkerButton('btn-abc')
number1 = BlinkerNumber('num-abc')counter = 0
pinValue = 0
oState = 'on'p2 = Pin(2, Pin.OUT)
p2.value(pinValue)def aligeniePowerState(state):global oStateprint("#"*8,state)BLINKER_LOG('need set power state: ', state)if state == BLINKER_CMD_ON:p2.value(1)BlinkerAliGenie.powerState("on")BlinkerAliGenie.print()oState = 'on'print(BLINKER_CMD_ON,"aaaaaaa")elif state == BLINKER_CMD_OFF:p2.value(0)BlinkerAliGenie.powerState("off")BlinkerAliGenie.print()oState = 'off'print(BLINKER_CMD_OFF,"sssssss")def aligenieQuery(queryCode):global oStateBLINKER_LOG('AliGenie Query codes: ', queryCode)print("*"*8,queryCode)if queryCode == BLINKER_CMD_QUERY_ALL_NUMBER :BLINKER_LOG('AliGenie Query All')BlinkerAliGenie.powerState(oState)BlinkerAliGenie.print()elif queryCode == BLINKER_CMD_QUERY_POWERSTATE_NUMBER :BLINKER_LOG('AliGenie Query Power State')BlinkerAliGenie.powerState(oState)BlinkerAliGenie.print()else :BlinkerAliGenie.powerState(oState)BlinkerAliGenie.print()def button1_callback(state):''' '''print("&"*8)BLINKER_LOG('get button state: ', state)button1.icon('icon_1')button1.color('#FFFFFF')button1.text('Your button name or describe')button1.print(state)global pinValuepinValue = 1 - pinValuep2.value(pinValue)def data_callback(data):global counterprint("+"*8)BLINKER_LOG('Blinker readString: ', data)counter += 1number1.print(counter)#print(Blinker.aliParse())
button1.attach(button1_callback)
Blinker.attachData(data_callback)#BlinkerAliGenie.attachColor(aligenieColor)
#BlinkerAliGenie.attachPowerState(aligeniePowerState)
#BlinkerAliGenie.attachQuery(aligenieQuery)if __name__ == '__main__':#msg = MQTTClients()#msg = Blinker.aliParse()while True:Blinker.run()
2)BlinkerAdapters下的BlinkerWiFi.py
就是在对应on_message函数里自定义处理解析,225行左右
from machine import Pin
p2 = Pin(2, Pin.OUT)def on_message(self, topic, msg):BLINKER_LOG_ALL('payload: ', msg)data = ujson.loads(msg)fromDevice = data['fromDevice']data = data['data']try:if data["set"]["pState"]=="on":p2.value(1)print("mmmmmmm")elif data["set"]["pState"]=="off":p2.value(0)print("jjjjjjkkkkk",data["set"]["pState"])except:passprint("jjjjjjhhhhh",type(data),data)data = ujson.dumps(data)BLINKER_LOG_ALL('fromDevice:', fromDevice, ', data: ', data)
这篇关于天猫精灵、Blinker 控制esp32自带灯熄灭---Micropython版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!