Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8041

General discussion • MQTT Publishing Cron Job Stopping Randomly

$
0
0
Hello, All.

I recently completed a small MQTT project that uses my Rapberry Pi 4 as a Client and Broker to Publish data to a topic that is subscribed to by my ESP32 Client; and it works perfectly well.

The initial caveat is that I had to manually start the python script on the Pi that publishes the data every 10 seconds. To avoid this, so that the script would always be running as a background process, I added the script to my chrontab:

Code:

@reboot python3 /home/pi/mqttPub3.py
This starts the script at reboot and works fairly well, HOWEVER, it seems to stop at some point in the night. Rebooting the Pi restarts the script and everything works again. Any idea why that happens? The script should run indefinitely as the primary code block is in a while True block:

Code:

 while True:                 mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)                 MQTT_MSG = pull_data()                 print("Value Pulled: ${}".format(MQTT_MSG))                 # Publish message to MQTT Topic                 mqttc.publish(MQTT_TOPIC, int(MQTT_MSG))                 mqttc.disconnect()                 time.sleep(10)
Is this a limitation of MQTT? Or possibly I'm not using the crontab properly? Or perhaps a cron job is not the correct convention for this? I don't think there's a memory leak causing a crash, as I'm still able to connect to the Pi, even after the script seemingly stops running.

As always, I appreciate any and all advice.

Best,
Joe

Statistics: Posted by joebro391 — Wed May 07, 2025 7:47 pm



Viewing all articles
Browse latest Browse all 8041

Trending Articles