У меня Raspberry Pi подключен к датчику температуры / влажности, который отправляет отчет на домашний сервер через Paho MQTT. Он случайно выпадает с ошибкой
«Ошибка подключения к серверу mqtt: индекс кортежа выходит за пределы диапазона»
Кажется, я не могу понять, как надежно воссоздать проблему - иногда этонемедленно останавливается, иногда это занимает несколько часов, иногда больше суток ...
Я попытался немного почистить код, но не совсем уверен, что не так.
#!/usr/bin/python
# DHT Sensor Data-logging to MQTT Temperature channel
# Requies a Mosquitto Server Install On the destination.
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# MQTT Encahncements: David Cole (2016)
import json
import requests
import sys
import time
import datetime
import paho.mqtt.client as mqtt
import Adafruit_DHT
DHT_TYPE = Adafruit_DHT.DHT22
DHT_PIN = 4
MOSQUITTO_HOST = '192.168.0.150'
MOSQUITTO_PORT = 1883
MOSQUITTO_TEMP_MSG = 'shed/temp'
MOSQUITTO_HUMI_MSG = 'shed/humid'
MOSQUITTO_CLIENTID = 'shedpi'
MOSQUITTO_USER = 'xxx'
MOSQUITTO_PASS = 'xxx'
print('Mosquitto Temp MSG {0}'.format(MOSQUITTO_TEMP_MSG))
print('Mosquitto Humidity MSG {0}'.format(MOSQUITTO_HUMI_MSG))
# How long to wait (in seconds) between measurements.
FREQUENCY_SECONDS = 300
print('Logging sensor measurements to {0} every {1} seconds.'.format('MQTT', FREQUENCY_SECONDS))
print('Press Ctrl-C to quit.')
print('Connecting to MQTT on {0}'.format(MOSQUITTO_HOST))
mqttc = mqtt.Client(client_id=MOSQUITTO_CLIENTID)
mqttc.username_pw_set(MOSQUITTO_USER, password=MOSQUITTO_PASS)
mqttc.connect(MOSQUITTO_HOST, port=MOSQUITTO_PORT, keepalive=60)
mqttc.loop_start()
try:
while True:
# Attempt to get sensor reading.
humidity, temp = Adafruit_DHT.read(DHT_TYPE, DHT_PIN)
if not -20 < temp < 50:
f = open('/home/pi/temphumid/errlog.log', 'a+')
f.write('{0},{1}, Temperature out of bounds, logged: {2:0.1f}*C\r\n'.format(time.strftime('%m/%d/%y'), time.strftime('%H:%M:%S'), temp))
f.close()
time.sleep(10)
humidity, temp = Adafruit_DHT.read(DHT_TYPE, DHT_PIN)
continue
if not -10 < humidity < 110:
f = open('/home/pi/temphumid/errlog.log', 'a+')
f.write('{0},{1}, Humidity out of bounds, logged{3:0.1f}%\r\n'.format(time.strftime('%m/%d/%y'), time.strftime('%H:%M:%S'), humidity))
f.close()
time.sleep(10)
humidity, temp = Adafruit_DHT.read(DHT_TYPE, DHT_PIN)
continue
# Publish to the MQTT channel
try:
#print 'Updating {0}'.format(MOSQUITTO_TEMP_MSG)
mqttc.publish("shed/temp",temp,qos=0,retain=True)
#print 'Updating {0}'.format(MOSQUITTO_HUMI_MSG)
time.sleep(1)
mqttc.publish("shed/humid",humidity,qos=0,retain=True)
except Exception,e:
# Error appending data, most likely because credentials are stale.
mqttc.disconnect()
print('Append error, logging in again: ' + str(e))
continue
# Wait 30 seconds before continuing
#print('Wrote a message to MQTT')
time.sleep(FREQUENCY_SECONDS)
except Exception as e:
print('Error connecting to the mqtt server: {0}'.format(e))
Произвольно завершается с ошибкой:
Ошибка подключения к серверу mqtt: индекс кортежа вне диапазона