从 cron 执行的 Python 脚本仅显示 1-Wire 上发送到 mqtt 代理的温度读取结果的 10 个结果,而从命令行执行的脚本连续运行没有任何问题。有人能告诉我这是为什么吗?
import paho.mqtt.client as mqtt
server = "xxxxxxxxx.com"
port = 1883
username = "xx"
password = "xxxxxxxxxxxxxxxxxxxx"
topic = "temp/f/xxxx"
client = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol="MQTTv31")
client.username_pw_set(username, password)
client.connect(server, port, keepalive=120, bind_address="")
def read_temperature(device_id):
"Read float temperature value from 1wire device DS18B20."
with open('/sys/bus/w1/devices/%s/w1_slave' % device_id) as f:
text = f.read().strip()
fragments = text.split()
return float(fragments[-1][2:]) / 1000.
time.sleep(10)
while True:
client.publish(topic, read_temperature('28-00000531cfff'), qos=1, retain=False)
print round(read_temperature('28-00000531cfff'),1)
time.sleep(10)
我尝试过稍微放慢一点进程,但没有任何帮助。我会感激任何帮助。