天猫精灵、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 Pin

from Blinker.Blinker import Blinker, BlinkerButton, BlinkerNumber, BlinkerAliGenie
from Blinker.BlinkerConfig import *
from Blinker.BlinkerDebug import *

#from BlinkerAdapters.BlinkerWiFi import MQTTClients
     

print(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 oState
    print("#"*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 oState
    BLINKER_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 pinValue

    pinValue = 1 - pinValue
    p2.value(pinValue)

def data_callback(data):
    global counter
    print("+"*8)
    BLINKER_LOG('Blinker readString: ', data)
    counter += 1
    number1.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 Pin

from Blinker.Blinker import Blinker, BlinkerButton, BlinkerNumber, BlinkerAliGenie
from Blinker.BlinkerConfig import *
from Blinker.BlinkerDebug import *

#from BlinkerAdapters.BlinkerWiFi import MQTTClients
     

print(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 oState
    print("#"*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 oState
    BLINKER_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 pinValue

    pinValue = 1 - pinValue
    p2.value(pinValue)

def data_callback(data):
    global counter
    print("+"*8)
    BLINKER_LOG('Blinker readString: ', data)
    counter += 1
    number1.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:
            pass
        print("jjjjjjhhhhh",type(data),data)
        data = ujson.dumps(data)
        BLINKER_LOG_ALL('fromDevice:', fromDevice, ', data: ', data)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

loong_XL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值