[英]Raspberry PI zero w PIR send to Telethon
如果检测到运动,我会尝试将图像从 usb 网络摄像头发送到电报组。 该动议有效,但我无法将其发送给我的小组。 所以我正在努力改为发送文本。 (通过图片我可以理解,但首先我需要让文字正常工作)
代码:
import RPi.GPIO as GPIO
import time
from telethon import TelegramClient
SENSOR_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN)
#Telegram settings
api_id='xxxxx'
api_hash='xxxxx'
phone='xxxx'
#Kamera Gruppe
chatid = -xxxxx
client = TelegramClient(phone, api_id, api_hash)
async def mein_callback(channel):
print('motion detected!')
await client.send_message(chatid, 'motion detected')
try:
GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING, callback=mein_callback)
while True:
time.sleep(100)
except KeyboardInterrupt:
print ("Beende...")
GPIO.cleanup()
错误:
sys:1: RuntimeWarning: coroutine 'mein_callback' was never awaited RuntimeWarning: Enable tracemalloc to get the object 分配追溯
我不知道 RPi 库是如何工作的,但我知道 Telethon 是一个async
库,从外观上看, RPi
不是。
你不能使用asyncio
time.sleep()
因为那会阻塞整个异步事件循环。 你需要像await asyncio.sleep()
这样的东西。 似乎您从未运行过client.start()
,因此客户端不会连接,因此client.send_message
会失败。 我建议您通读asyncio
文档以更好地理解这一点。 那就是说......以下应该有效,假设RPi
没有线程化:
import RPi.GPIO as GPIO
import asyncio
from telethon import TelegramClient
...
client = TelegramClient(phone, api_id, api_hash)
async def mein_callback(channel):
print('motion detected!')
await client.send_message(chatid, 'motion detected')
def schedule_mein_callback(channel):
client.loop.create_task(mein_callback(channel)) # <- create a new task that will run in the future
async def main():
async with client: # <- start the client so `send_message` works
GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING, callback=schedule_mein_callback) # <- not an async callback, GPIO can run it fine
while True:
await asyncio.sleep(100) # <- no longer blocks, telethon can run fine
try:
client.loop.run_until_complete(main()) # <- enter async-land
except KeyboardInterrupt: # <- should generally be used before entering `async`
print ("Beende...")
GPIO.cleanup()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.