Home » Eclipse Projects » Paho » RPi Python MQTT - occasional errors(MQTT fails with an error that is inside the library - and stops!)
RPi Python MQTT - occasional errors [message #1850165] |
Fri, 18 February 2022 18:50 |
Bruce Orr Messages: 1 Registered: February 2022 |
Junior Member |
|
|
I am running several RPis using the Python PahoMQTT library.
Occasionally, one of them fails and although the Python program continues running it stops publishing messages.
They commonly run for a few hours and then fail.
They all usually run headless, but in this case I happened to have a console open and that is how I captured the error report. In the error report you may spot that the program is still publishing messages to the console, but it isn't publishing to MQTT !!!
This example is running on a RPi Zero (although I have similar examples on RPi 3s).
This is a typical error report ....
8.875 C on ip No 190 17:59:42
9.0 C on ip No 190 18:00:44
9.0 C on ip No 190 18:01:46
9.0 C on ip No 190 18:02:48
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3452, in _thread_main
self.loop_forever(retry_first_connection=True)
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1779, in loop_forever
rc = self.loop(timeout, max_packets)
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1201, in loop
return self.loop_misc()
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1628, in loop_misc
self._check_keepalive()
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 2430, in _check_keepalive
self._do_on_disconnect(rc)
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3360, in _do_on_disconnect
self.on_disconnect(self, self._userdata, rc)
TypeError: on_disconnect() missing 1 required positional argument: 'rc'
8.937 C on ip No 190 18:03:50
8.937 C on ip No 190 18:04:51
9.0 C on ip No 190 18:05:53
My impression is that the error is inside the library itself - and I don't have the skill to debug the library.
My code is this ....
Basically it reads a temperature from an 18B20 sensor and publishes the result to an MQTT queue. The queue is generated from a fixed "prefix" with the addition of the last 3 digits of the ip adress to make it specific to that machine.
# -- Setup -------------------------------------------------------------
import datetime, time, glob, os, subprocess
import paho.mqtt.client as mqtt
filename = " 18b20-testnolibmq2X.py"
print (filename + " restarted at " + time.strftime("%H:%M:%S"))
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
ip = subprocess.getoutput("hostname -I") # get the ip address
print("ip is "+ip)
ipl3 = ip[10:13]
queue = ("highmount/temp/"+ipl3) # use ip as queue name
# Functions ------------------------------------------------------------
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
# ----------------------------------------------------------------------
def read_temp():
try: # trap errors if it doesn't read
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES': # error check
time.sleep(2) # if error wait for N sec
print ("Pause to re-read")
writetolog(" re-read ")
lines = read_temp_raw() # try again - re-read temp
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
except:
print("Oops - error!")
writetolog( " Error ")
# ----------------------------------------------------------------------
def writetolog(logmessage): # Write log messages to log file - called by each sensor check
fo = open("/home/pi/test/18b20/logfile.txt", "r+") # open the file smlog.txt or /var/www/html/sysmanlog.txt, r+ is read and write
existing=fo.read() # import existing content
fo.seek(0,0) # move pointer to start of file
fo.write(time.strftime("%A: %d %H:%M:%S ")+logmessage+"\n"+ existing) # append new text
fo.close() # close the file
# ----------------------------------------------------------------------
def on_connect(mqttc, userdata, flags, rc):
mqttc.subscribe(queue, 0) # Connect queue
writetolog ("Connected with result code " +str(rc)+" "+ time.strftime("%A %d %H:%M:%S "))
mqttc.publish("highmount/status", payload=filename+" Online",qos=1, retain=True)
#-----------------------------------------------------------------------
def on_disconnect(mqttc, userdata, flags, rc):
writetolog ("HELP DISConnected with result code " +str(rc)+" "+ time.strftime("%A %d %H:%M:%S "))
#-----------------------------------------------------------------------
#---- Main programme ---------------------------------------------------
mqttc = mqtt.Client() # Alias for MQTT Client
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.will_set("highmount/status", payload=filename+" Offline", qos=1, retain=True)
mqttc.connect("broker.hivemq.com", port=1883, keepalive=10) # Connect to server - this calls on_connect
# --- Start of loop ---------------------------------------------------
try:
while True:
print(str(read_temp())+" C on ip No "+ipl3+" "+ time.strftime("%H:%M:%S"))
mqttc.publish(queue, read_temp(),1)
mqttc.loop_start()
time.sleep(60)
except KeyboardInterrupt: # ... and to clean exit
print(" Control C breakout ...")
Any suggestions would be gratefully received.
I was thinking that maybe I could check if the MQTT link was still up - can I do something to generate a PINGREQ and if I don't get a response then restart the MQTT client? That is probably stretching my MQTT skills, but maybe I can learn .........
Thanks in advance
Bruce
|
|
|
Goto Forum:
Current Time: Fri May 03 06:34:59 GMT 2024
Powered by FUDForum. Page generated in 0.02750 seconds
|