我想用我的树莓派做气象站。因此,我买了一个“德博森雨”和“德博森BME680”。因为我以前从未使用过Python,所以我从互联网上复制了两个脚本。他们都做得很好。昨天,我创建了一个电报机器人,当我发送"/data“或"/rain”时,它应该发送给我当前测量的数据。因此,我在一个新的python脚本中复制了原始脚本,并嵌入了bot。不幸的是,它不能工作,但我无法定位错误。
这是我的密码:
#!/usr/bin/python3
#coding: utf8
# telegram import
import time, datetime
import telepot
from telepot.loop import MessageLoop
# import os
# bme680 import
import board
from busio import I2C
import adafruit_bme680
i2c = I2C(board.SCL, board.SDA)
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c, debug = False)
bme680.sea_level_pressure = 971.91
temperature_offset = 0
temperatur = "Temperatur: %0.1f C" % (bme680.temperature + temperature_offset)
gas = "Gas: %d ohm" % bme680.gas
luftfeuchtigkeit = "Luftfeuchtigkeit: %0.1f %%" % bme680.relative_humidity
luftdruck = "Luftdruck: %0.3f hPa" % bme680.pressure
messdaten = temperatur + "\n" + gas + "\n" + luftfeuchtigkeit + "\n" + luftdruck
#rain import
from time import sleep
from gpiozero import InputDevice
rain = InputDevice(18)
while True:
if rain.is_active:
regen = "Es regnet. Fenster zu!"
else:
regen = "Es regnet nicht"
now = datetime.datetime.now()
def action(msg):
chat_id=msg['chat']['id']
command = msg['text']
print ('Received: %s' % command)
if command == '/hi':
telegram_bot.sendMessage (chat_id, str("Kuckuck"))
elif command == '/data':
telegram_bot.sendMessage (chat_id, str(messdaten))
elif command == '/rain':
telegram_bot.sendMessage (chat_id, str(regen))
telegram_bot = telepot.Bot('XXX:XXXXXXX')
print (telegram_bot.getMe())
MessageLoop(telegram_bot, action).run_as_thread()
print ('Up and Running...')
# Keep the program running.
while 1:
time.sleep(10)(我试图使用Python编辑器“Mu”运行脚本。)
非常感谢你的帮助。
发布于 2021-11-30 14:20:04
让我们仔细看看您的代码的这一部分:
while True:
if rain.is_active:
regen = "Es regnet. Fenster zu!"
else:
regen = "Es regnet nicht"while True 将永远不会退出,下面的电报代码将永远无法到达。
您需要重做环,在相同的循环中更新regen变量,就像发送电报消息一样
https://stackoverflow.com/questions/70170788
复制相似问题