Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么python脚本会在Pi为零的情况下随机消亡?_Python_Mqtt - Fatal编程技术网

为什么python脚本会在Pi为零的情况下随机消亡?

为什么python脚本会在Pi为零的情况下随机消亡?,python,mqtt,Python,Mqtt,编辑: 为清楚起见: 问题不在MQTT中,而是Pi Zero上的python脚本刚刚死亡。经过一段不同的时间后,脚本停止存在并 sudo ps -ax | grep python 不再列出运行脚本的进程 原始问题: 我编写了一个简单的脚本,从pi零点读取DHT22传感器,并将温度和湿度每分钟发布一次到单独的MQTT主题上。在某个时刻,脚本只是停止,因为它不再读取或发布任何读数,即使它在while True循环中运行。在我的代理中,pi随后显示为“脱机”,因为这是最后一条will消息。日志文件

编辑: 为清楚起见: 问题不在MQTT中,而是Pi Zero上的python脚本刚刚死亡。经过一段不同的时间后,脚本停止存在并

sudo ps -ax | grep python 
不再列出运行脚本的进程

原始问题:

我编写了一个简单的脚本,从pi零点读取DHT22传感器,并将温度和湿度每分钟发布一次到单独的MQTT主题上。在某个时刻,脚本只是停止,因为它不再读取或发布任何读数,即使它在while True循环中运行。在我的代理中,pi随后显示为“脱机”,因为这是最后一条will消息。日志文件不包含任何有用的信息,从某种意义上说,只要编写日志文件,脚本就会按预期工作,然后突然出现一个空白

def on_connect(client, userdata, flags, rc):
    message = "connected with rc: " + str(rc)
    print(message)
    log(message, mqtt_log_file)
    mqttPub.publish(topicStatusBedroom, "Online", 1, True)
def on_publish(client, obj, mid):
    message = "published with mid: " + str(mid)
    print(message)
    log(message, mqtt_log_file)
def on_disconnect(client, userdata, rc):
    print("client disconnected ok")
    log("client disconnected\n", mqtt_log_file)
    while True:
        try:
            mqttPub.connect(url_str, url_port)
            break # break the while loop, if reconnect works
        except:
            time.sleep(2) # otherwise sleep and retry
def log(message, file):
    file = open(file, "a")
    file.write(message)
    file.close()  
mqttPub = mqtt.Client()
mqttPub.on_connect = on_connect
mqttPub.on_publish = on_publish
mqttPub.on_disconnect = on_disconnect
mqttPub.username_pw_set(username, givenPassword)
mqttPub.will_set(topicStatusBedroom, 'Offline', 1, True)
mqttPub.connect(url_str, url_port)
mqttPub.loop_start() # loop_start handles reconnects automatically

if __name__ == "__main__":
    pin = 22
    sensor = Adafruit_DHT.DHT22
    log_file = 'dht_22_logging.txt'

    while True:
        humidity, temperature = readDHT(sensor, pin)
        if humidity is not None and temperature is not None:
            print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
            message = time.strftime("%Y-%m-%dT%H:%M:%S") + "," + str(round(temperature, 1)) + "," + str(int(humidity)) + "\n"
            mqttPub.publish(topicStatusTemp, str(round(temperature, 1)), 1, True)
            mqttPub.publish(topicStatusHumidity, str(round(humidity, 0)), 1, True)
            log(message, log_file)    
        else:
            message = 'Failed to get reading. Try again!'
            print(message)
            log(message, log_file)
        time.sleep(60)
回答: 脚本与SSH连接一起终止。 有效的方法是创建脚本服务并使用systemd启动它。我确实按照本教程进行了操作:


脚本已经运行了一周,没有任何问题

你的断开连接的
实现是什么?我已经用断开连接功能更新了帖子。但是,on_disconnect从不被调用,因为来自print的消息(“client disconnected ok”从不显示。所有打印消息也会保存到日志文件中,并且打印消息也不会出现在日志文件中,尽管所有其他消息都保存在日志文件中。如果在
pdb
(python调试器)中启动它会发生什么?我通过SSH在Putty上使用了“sudo python3-m pdb myscript.py”,它没有显示任何错误。在某个时刻,Putty关闭连接,不再更新pdb消息,脚本也不通信。这是否意味着,通过SSH启动的任何脚本都包含在该特定SSH会话中?