• 天猫精灵、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()
            
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    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()
            
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    AI大模型日报#0420:开源模型击败GPT-4、西湖大学蛋白质通用大模型、GPT的七条经验
    积分专题笔记-曲线面积分三大公式
    mindspore课程打卡第二天
    将音频格式从flac转到wav的两种方法
    redis的c++ 客户端 redis-plus-plus
    pytest 失败截图
    Java对象分配及垃圾回收机制
    eclipse配置maven,安装lombok,导入和创建springboot项目
    DBCO-PEG9-NH2_2353409-99-9 点击化学 PEG 试剂
    kafka-go操作kafka
  • 原文地址:https://blog.csdn.net/weixin_42357472/article/details/126311113