天天看点

python控制蓝牙音响_[ESP32+MicroPython]智能音响控制

blinker支持多种智能音响控制,如天猫精灵、百度小度、小米小爱、京东叮咚等。

这里以天猫精灵控制为例,blinker DIY支持将设备模拟成三种类型的智能家居:插座、灯、传感器。

Blinker支持多种语音助手控制,如天猫精灵、百度小度,本节以天猫精灵控制为例。

示例程序及blinker模块

天猫精灵基本接入方法

通常语音助手都是对特定的设备类型进行支持,确定设备类型后,才能响应对应的语音指令。使用blinker方案开发设备,也需要先设定设备类型,支持的设备类型如下:

设备类型

支持功能

blinker宏

传感器

可以查询传感器获取到的数据

BLINKER_ALIGENIE_SENSOR

开关灯、设置颜色、亮度、色温、模式

BLINKER_ALIGENIE_LIGHT

插座

开关设备

BLINKER_ALIGENIE_OUTLET

如,将设备设定为插座类型:

Blinker.aliType('BLINKER_ALIGENIE_OUTLET')

定义类型后,即可使用对象BlinkerAliGenie,进行相关操作。

例如传感器类型的设备,支持状态查询操作,和对UI组件的操作类似,也需要编写并注册回调函数。可使用

BlinkerAliGenie.attachPowerState(aligeniePowerState)

BlinkerAliGenie.attachQuery(aligenieQuery)

语句注册一个数据查询回调函数。完成注册操作后,每次天猫精灵收到对应语音指令,即会触发aligenieQuery回调函数。

状态查询回调函数如下:

def aligeniePowerState(state):

''' '''

BLINKER_LOG('need set power state: ', state)

BlinkerAliGenie.powerState(state)

BlinkerAliGenie.print()

def aligenieQuery(queryCode):

''' '''

BLINKER_LOG('AliGenie Query codes: ', 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()

完整示例如下:

#!/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 *

# 此处填入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_OUTLET')

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):

''' '''

BLINKER_LOG('need set power state: ', state)

BlinkerAliGenie.powerState(state)

BlinkerAliGenie.print()

def aligenieQuery(queryCode):

''' '''

BLINKER_LOG('AliGenie Query codes: ', 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):

''' '''

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

BLINKER_LOG('Blinker readString: ', data)

counter += 1

number1.print(counter)

button1.attach(button1_callback)

Blinker.attachData(data_callback)

BlinkerAliGenie.attachPowerState(aligeniePowerState)

BlinkerAliGenie.attachQuery(aligenieQuery)

if __name__ == '__main__':

while True:

Blinker.run()

天猫精灵端配置

设备上线后,还需要在天猫精灵App中绑定blinker账号,方法如下:

1.打开天猫精灵App,在下方导航栏,点 我的 > 添加智能设备

2.搜索blinker,并点击要添加的设备类型

3.点击绑定账号

4.使用blinker账号密码登录,并完成绑定

现在,可以对天猫精灵说,“天猫精灵打开插座”或者“天猫精灵,查询插座状态”等,都可以让天猫精灵播报当前传感器数据。

你还是通过阅读开发文档和示例,可以将设备设为灯或传感器