GithubHelp home page GithubHelp logo

blinker-iot / blinker-py Goto Github PK

View Code? Open in Web Editor NEW
2.7K 6.0 31.0 227 KB

Blinker python library for hardware. Works with Raspberry Pi, Banan Pi, Linux devices

License: MIT License

Python 100.00%
blinker wifi websocket ble mqtt respberry-pi linux windows embedded hardware

blinker-py's People

Contributors

i3water avatar werewolfst avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

blinker-py's Issues

关于blinker支持esp

请问blinker-py支持esp32和esp8266吗,我想让siri控制设备,但我对c++不熟悉,想使用python

小爱同学接入无效

小爱同学例程运行于树莓派环境测试,点灯app编辑按钮后能控制,但是同步到自己的小米账号,无法使用小爱同学进行语音控制,感觉是接入接口问题。请作者解决下!!谢谢

报错问题,求解答

官方例程Hello_WiFi.py,在仅修改auth情况下,报错
Exception in thread zeroconf-Engine-1968657504:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in bootstrap_inner
self.run()
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 1309, in run
reader.handle_read(socket
)
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 1391, in handle_read
self.zc.handle_query(msg, None, MDNS_PORT)
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 2678, in handle_query
service.server,
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 652, in init
DNSRecord.init(self, name, type
, class_, ttl)
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 457, in init
self._expiration_time = self.get_expiration_time(100)
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 484, in get_expiration_time
return self.created + (percent * self.ttl * 10)
TypeError: unsupported operand type(s) for +: 'float' and 'str'

Traceback (most recent call last):
File "demo1.py", line 11, in
Blinker.begin('1c6f493bb57f')
File "/usr/local/lib/python3.6/dist-packages/Blinker-0.2.0-py3.6.egg/Blinker/Blinker.py", line 151, in begin
bProto.conn2.start(bProto.conn1.bmqtt.deviceName)
File "/usr/local/lib/python3.6/dist-packages/Blinker-0.2.0-py3.6.egg/BlinkerAdapters/BlinkerLinuxWS.py", line 86, in start
mDNSinit(self.type , name)
File "/usr/local/lib/python3.6/dist-packages/Blinker-0.2.0-py3.6.egg/BlinkerAdapters/BlinkerLinuxWS.py", line 40, in mDNSinit
zeroconf.register_service(info)
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 2385, in register_service
self.broadcast_service(info)
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 2419, in broadcast_service
info.server,
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 652, in init
DNSRecord.init(self, name, type
, class
, ttl)
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 457, in init
self._expiration_time = self.get_expiration_time(100)
File "/usr/local/lib/python3.6/dist-packages/zeroconf-0.27.1-py3.6.egg/zeroconf/init.py", line 484, in get_expiration_time
return self.created + (percent * self.ttl * 10)
TypeError: unsupported operand type(s) for +: 'float' and 'str'

这代码真的是要气死人啊?!

到底为什么会把布尔值当作字符串的值啊?!
在示例代码里面,以下这段话根本无法进行电源状态变更,因为pState是一个字符串而返回值是"true"和”false“
首先,按照代码里面的on和off是根本就没判断。
其次,按照代码逻辑,返回值是真假应该是布尔变量直接判断也不行。
也就是说,非得把布尔值当作字符串用。
什么情况啊,示例代码不按照代码规范写还是个出错不能运行的。

set_state = message.data["pState"]
print("change power state to : {0}".format(set_state))

if set_state == "on":
    pass
elif set_state == "off":
    pass

也就是说这段代码正常运行应该是这样的:

set_state = message.data["pState"]
print("change power state to : {0}".format(set_state))

if set_state == "true":
    pass
elif set_state == "false":
    pass

https://github.com/blinker-iot/blinker-py/blob/dev_3.0/example/voice_assistant.py

同时运行两个脚本,地址冲突问题,求解决办法

Traceback (most recent call last):
File "wol.py", line 17, in
Blinker.mode("BLINKER_WIFI")
File "/usr/local/lib/python3.7/site-packages/Blinker-0.2.0-py3.7.egg/Blinker/Blinker.py", line 123, in mode
bProto.conn2 = bProto.proto2.WebSocketServer(BLINKER_DIY_MQTT)
File "/usr/local/lib/python3.7/site-packages/Blinker-0.2.0-py3.7.egg/BlinkerAdapters/BlinkerLinuxWS.py", line 81, in init
self.server = SimpleWebSocketServer(self.name, self.port, HandleServer)
File "/usr/local/lib/python3.7/site-packages/SimpleWebSocketServer/SimpleWebSocketServer.py", line 602, in init
self.serversocket.bind(hostInfo[0][4])
OSError: [Errno 98] Address already in use

树莓派Hello_WiFi.py运行时报host无效

请教一下,这个是否需要手动更改host等参数,这些参数应该怎么选择或者参考哪里?
报错信息如下:
Traceback (most recent call last):
File "Hello_WiFi.py", line 12, in
Blinker.begin(auth)
File "/mnt/d/Desktop/blinker-py-0.2.0/Blinker/Blinker.py", line 172, in begin
bProto.conn1.start(auth, bProto.aliType, bProto.duerType, bProto.miType)
File "/mnt/d/Desktop/blinker-py-0.2.0/BlinkerAdapters/BlinkerMQTT.py", line 329, in start
self.client.connect(self.bmqtt.host, self.bmqtt.port, 60)
File "/usr/local/lib/python3.6/dist-packages/paho_mqtt-1.5.1-py3.6.egg/paho/mqtt/client.py", line 940, in connect
bind_address, bind_port, clean_start, properties)
File "/usr/local/lib/python3.6/dist-packages/paho_mqtt-1.5.1-py3.6.egg/paho/mqtt/client.py", line 1005, in connect_async
raise ValueError('Invalid host.')
ValueError: Invalid host.

求小爱控制空调

希望下一版本能加入,python版本小爱控制空调的各种函数和example。目前好像没空调的。

dev 3.0 运行报错 SyntaxError: invalid syntax

输出如下

pi@raspberrypi:~/Desktop $ /bin/python3 /home/pi/Desktop/blinker_control.py
Traceback (most recent call last):
  File "/home/pi/Desktop/blinker_control.py", line 1, in <module>
    from blinker import Device , ButtonWidget
  File "/home/pi/.local/lib/python3.7/site-packages/blinker/__init__.py", line 6, in <module>
    from .device import *
  File "/home/pi/.local/lib/python3.7/site-packages/blinker/device.py", line 70
    def __init__(self, /, **kw):
                       ^
SyntaxError: invalid syntax
pi@raspberrypi:~/Desktop $ 

新装raspbian, python 3.7, auth 报错 invalid host

pi@raspberrypi:~/blinker-py $ python --version
Python 3.7.3
pi@raspberrypi:~/blinker-py $ python myblinker.py
Traceback (most recent call last):
  File "myblinker.py", line 8, in <module>
    Blinker.mode("BLINKER_WIFI")
  File "/home/pi/blinker-py/Blinker/Blinker.py", line 123, in mode
    bProto.conn2 = bProto.proto2.WebSocketServer(BLINKER_DIY_MQTT)
  File "/home/pi/blinker-py/BlinkerAdapters/BlinkerLinuxWS.py", line 81, in __init__
    self.server = SimpleWebSocketServer(self.name, self.port, HandleServer)
  File "/usr/local/lib/python3.7/dist-packages/SimpleWebSocketServer-0.1.1-py3.7.egg/SimpleWebSocketServer/SimpleWebSocketServer.py", line 602, in __init__
PermissionError: [Errno 13] Permission denied
pi@raspberrypi:~/blinker-py $ sudo python myblinker.py
[14:09:26 2021] url: https://iot.diandeng.tech/api/v1/user/device/diy/auth?authKey=b5d35a05b817
[14:09:29 2021] Device Auth Data: {'detail': {'broker': 'blinker', 'deviceName': 'FD236C7EXT8MOAX8WD447G08', 'host': 'mqtts://broker.diandeng.tech', 'iotId': '9RG7376YTMrVUmVMkuxkbGhv77', 'iotToken': 'KksdZm9ATj62EPhMg4Bzqf4pZRCRHuPv', 'port': '1884', 'productKey': 'blinker', 'uuid': 'fd236c7fac6e11eb89795254'}, 'message': 1000}
[14:09:29 2021] deviceName: FD236C7EXT8MOAX8WD447G08
[14:09:29 2021] iotId: 9RG7376YTMrVUmVMkuxkbGhv77
[14:09:29 2021] iotToken: KksdZm9ATj62EPhMg4Bzqf4pZRCRHuPv
[14:09:29 2021] productKey: blinker
[14:09:29 2021] uuid: fd236c7fac6e11eb89795254
[14:09:29 2021] broker: blinker
[14:09:29 2021] clientID:
[14:09:29 2021] userName:
[14:09:29 2021] password: 😎
[14:09:29 2021] subtopic:
[14:09:29 2021] pubtopic:
Traceback (most recent call last):
  File "myblinker.py", line 9, in <module>
    Blinker.begin(auth)
  File "/home/pi/blinker-py/Blinker/Blinker.py", line 171, in begin
    bProto.conn1.start(auth, bProto.aliType, bProto.duerType, bProto.miType)
  File "/home/pi/blinker-py/BlinkerAdapters/BlinkerMQTT.py", line 328, in start
    self.client.connect(self.bmqtt.host, self.bmqtt.port, 60)
  File "/usr/local/lib/python3.7/dist-packages/paho_mqtt-1.5.1-py3.7.egg/paho/mqtt/client.py", line 940, in connect
    bind_address, bind_port, clean_start, properties)
  File "/usr/local/lib/python3.7/dist-packages/paho_mqtt-1.5.1-py3.7.egg/paho/mqtt/client.py", line 1005, in connect_async
    raise ValueError('Invalid host.')
ValueError: Invalid host.
pi@raspberrypi:~/blinker-py $

python 版本3.7.3,myblinker.py 除了初始化什么也没写, 不用sudo说没权限,用sudo报错 mqtt 有个Invalid host

from Blinker import *

auth = '😎'

BLINKER_DEBUG.debugAll()

Blinker.mode("BLINKER_WIFI")
Blinker.begin(auth)

小爱同学 要操作的设备好像出问题了,等一会再试

小爱同学语音控制总是返回 设备出问题,有时能正常控制,有时不行

环境:

Linux OptiPlex 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Python 3.9.5
blinker-py                        0.3.0

代码:

from blinker import Device
from blinker.voice_assistant import VAType, VoiceAssistant, PowerMessage, DataMessage
STATUS = 'off'

async def power_change(message: PowerMessage):
    """ 电源状态改变(适用于灯和插座)
    """
    global STATUS
    set_state = message.data["pState"]
    print(f"[{datetime.datetime.now()}] change power state to : {set_state}")
    if set_state == 'true':
        send_magic_packet(mac)
        STATUS = 'on'
    else:
        STATUS = 'off'
    await (await message.power(set_state)).update()

async def state_query(message: DataMessage):
    print(f"[{datetime.datetime.now()}] query state: {message.data}")
    await (await message.power(STATUS)).update()

device = Device("9701030d0719", websocket=True, mi_type=VAType.OUTLET)
voice_assistant = VoiceAssistant(VAType.OUTLET)

voice_assistant.state_query_callable = state_query
voice_assistant.power_change_callable = power_change

device.addVoiceAssistant(voice_assistant)

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

日志

{'fromDevice': 'ServerSender', 'data': {'get': 'state', 'from': 'MIOT', 'messageId': '63778fa527969378'}}
[2022-11-18 21:59:01.287934] query state: state
2022-11-18 21:59:01.288 | INFO     | blinker.mqttclient:send_to_voiceassistant:115 - send mqtt message to voice assistant: {"fromDevice": "833128F89V2PBJ0TAIZXNYHL", "toDevice": "ServerReceiver", "data": {"pState": "false", "messageId": "63778fa527969378"}}
2022-11-18 21:59:02.377 | INFO     | blinker.mqttclient:_on_connect:46 - Connect to broker from mqtt
2022-11-18 21:59:02.378 | SUCCESS  | blinker.mqttclient:_on_connect:48 - Broker connected...
{'fromDevice': 'ServerSender', 'data': {'set': {'pState': 'true', 'pstate': 'on'}, 'from': 'MIOT', 'messageId': '63778fa62842e056'}}
[2022-11-18 21:59:02.436670] change power state to : true
2022-11-18 21:59:02.437 | INFO     | blinker.mqttclient:send_to_voiceassistant:115 - send mqtt message to voice assistant: {"fromDevice": "833128F89V2PBJ0TAIZXNYHL", "toDevice": "ServerReceiver", "data": {"pState": "true", "messageId": "63778fa62842e056"}}
2022-11-18 21:59:03.482 | INFO     | blinker.mqttclient:_on_connect:46 - Connect to broker from mqtt
2022-11-18 21:59:03.482 | SUCCESS  | blinker.mqttclient:_on_connect:48 - Broker connected...


{'fromDevice': 'ServerSender', 'data': {'get': 'state', 'from': 'MIOT', 'messageId': '63778fb12e9e096c'}}
[2022-11-18 21:59:13.081066] query state: state
2022-11-18 21:59:13.081 | INFO     | blinker.mqttclient:send_to_voiceassistant:115 - send mqtt message to voice assistant: {"fromDevice": "833128F89V2PBJ0TAIZXNYHL", "toDevice": "ServerReceiver", "data": {"pState": "true", "messageId": "63778fb12e9e096c"}}
2022-11-18 21:59:14.129 | INFO     | blinker.mqttclient:_on_connect:46 - Connect to broker from mqtt
2022-11-18 21:59:14.129 | SUCCESS  | blinker.mqttclient:_on_connect:48 - Broker connected...

python3运行几分钟就报错

File "me.py", line 52, in
Blinker.run()
File "/usr/local/lib/python3.7/dist-packages/Blinker-0.2.0-py3.7.egg/Blinker/Blinker.py", line 247, in run
BlinkerPY.checkAutoFormat(self)
File "/usr/local/lib/python3.7/dist-packages/Blinker-0.2.0-py3.7.egg/Blinker/Blinker.py", line 357, in checkAutoFormat
BLINKER_LOG_ALL("auto format: ", json.loads(bProto.sendBuf))
File "/usr/lib/python3.7/json/init.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

连接后报错

Hello_WIFI.py
�库都是最新的。

[10:54:05 2020] subtopic:
[10:54:05 2020] pubtopic:
Traceback (most recent call last):
File "Hello_WiFi.py", line 12, in
Blinker.begin(auth)
File "/usr/local/lib/python3.7/dist-packages/Blinker-0.2.0-py3.7.egg/Blinker/Blinker.py", line 171, in begin
bProto.conn1.start(auth, bProto.aliType, bProto.duerType, bProto.miType)
File "/usr/local/lib/python3.7/dist-packages/Blinker-0.2.0-py3.7.egg/BlinkerAdapters/BlinkerMQTT.py", line 328, in start
self.client.connect(self.bmqtt.host, self.bmqtt.port, 60)
File "/usr/local/lib/python3.7/dist-packages/paho_mqtt-1.5.1-py3.7.egg/paho/mqtt/client.py", line 940, in connect
bind_address, bind_port, clean_start, properties)
File "/usr/local/lib/python3.7/dist-packages/paho_mqtt-1.5.1-py3.7.egg/paho/mqtt/client.py", line 1005, in connect_async
raise ValueError('Invalid host.')
ValueError: Invalid host.
pi@raspberrypi:~/blinker-py/ex

dev_3.0报错接小爱同学报错 "要操作的设备好像出问题了,等一会再试"

(venv) ➜  [/home/pi/Soft/blinker-py] git:(dev_3.0) ✗ sudo python3 example/voice_assistant.py
Press Ctrl+C to exit
2022-04-11 21:13:27.355 | SUCCESS  | blinker.device:device_init:223 - Device auth successful...
2022-04-11 21:13:27.362 | INFO     | blinker.device:_cloud_heartbeat:246 - Send cloud heartbeat
2022-04-11 21:13:27.363 | INFO     | blinker.httpclient:cloud_heartbeat:128 - http cloud heartbeat
Exception in thread Thread-6:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.9/dist-packages/blinker/device.py", line 458, in init_local_service
    await zero_conf.async_register_service(info)
AttributeError: 'Zeroconf' object has no attribute 'async_register_service'
2022-04-11 21:13:27.430 | INFO     | blinker.mqttclient:_on_connect:46 - Connect to broker from mqtt
2022-04-11 21:13:27.431 | SUCCESS  | blinker.mqttclient:_on_connect:48 - Broker connected...
2022-04-11 21:13:27.432 | SUCCESS  | blinker.device:_receiver:382 - Receiver ready...
query state: state
2022-04-11 21:13:34.302 | INFO     | blinker.mqttclient:send_to_voiceassistant:115 - send mqtt message to voice assistant: {"fromDevice": "E9E2009E1MRJ4VM2VBVHB1XN", "toDevice": "ServerReceiver", "data": {"pState": "true", "mode": "day", "color": "red", "brightness": "66", "messageId": "6254297e30efdd70"}}
2022-04-11 21:13:35.375 | INFO     | blinker.mqttclient:_on_connect:46 - Connect to broker from mqtt
2022-04-11 21:13:35.376 | SUCCESS  | blinker.mqttclient:_on_connect:48 - Broker connected...

The Blinker.run() takes 100%CPU power

Since the thread_run() function will busy loop polling checkData, the CPU core will be occupied 100% by Blinker library. Suggest add a small sleep like time.sleep(0.1) after checkData() in thread_run() function.

Plus, it's good if we could change the demo program as follow:
...
if name == 'main':
while True:
Blinker.run()
time.sleep(0.1)
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.