OldPCGuy
Posts: 73
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

Crontab Issue with Paho MQTT

Wed May 16, 2018 12:41 am

I have a script that loads at boot from crontab.

Code: Select all

# start water heater measure
@reboot /usr/bin/python /home/pi/Multi_DS18B20.py
imports and basic setups are like so;

Code: Select all

#!/usr/bin/python
from DS18B20_Class import DS18B20	# from file X import func Y Modified class from https://www.raspberrypi.org/forums/viewtopic.php?p=861420
from datetime import datetime, date
import time
import csv
import os

# shut down button
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)
gpio.setup(21, gpio.OUT)

# test temperature sensors
x = DS18B20()
count=x.device_count()



When I add code to publish to a Mosquitto broker running on the same Pi3 the script fails to load at boot. However it will run from the IDE.

Code: Select all

#!/usr/bin/python
from DS18B20_Class import DS18B20	# from file X import func Y Modified class from https://www.raspberrypi.org/forums/viewtopic.php?p=861420
from datetime import datetime, date
import time
import csv
import os
import paho.mqtt.client as mqtt

# shut down button
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)
gpio.setup(21, gpio.OUT)

# test temperature sensors
x = DS18B20()
count=x.device_count()

# MQTT publisher setup
client = mqtt.Client()

def mqttPublish(lst):
	client.connect("localhost", 1883, 60) # if broker on another machine replace localhost with I/P address
	client.publish("topic/WtrHtr", str(lst));
	client.disconnect();
	return
I thought maybe a fat finger so I comment out all the MQTT specific code it runs at boot again. What is it about the include/import of the Paho-MQTT that could cause this. Better yet how do I correct for it?

User avatar
jjsanderson
Posts: 14
Joined: Wed Oct 17, 2012 11:45 am

Re: Crontab Issue with Paho MQTT

Wed May 16, 2018 9:22 am

Hmm, I've done a lot with Paho and found it completely reliable… but I've not launched scripts at boot, so I don't really have much to offer here.

Is it possible you're hitting one of the issues discussed in this other thread: viewtopic.php?f=32&t=145382 ?

In particular, I'd look for whether your broker (Mosquitto?) is fully up and listening for connections. It's possible some error handling on `.connect()` or even just a big `sleep()` at the start of the script could fix the issue. Otherwise - as what user does the crontab try to run the script, and is paho-mqtt accessible by that user?

...but I've a hunch you'll already have explored all these. Good luck!

fredoll
Posts: 209
Joined: Thu Jan 10, 2013 10:30 am

Re: Crontab Issue with Paho MQTT

Wed May 16, 2018 9:36 am

Usually, the problem is that the network is not completely up and running when you script executes.
The easiest way around I've found is to add a sleep for a few seconds before connecting to the MQTT server (I know it's not "nice" nut it works)

pcmanbob
Posts: 3677
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Crontab Issue with Paho MQTT

Wed May 16, 2018 2:18 pm

A simple test would be to change your cron line to this

Code: Select all

@reboot sleep 60 && python /home/pi/Multi_DS18B20.py
it will add a 60 second delay before starting your program, you may find it will work ok with a shorter delay, but you will have to experiment to find out how short.

and I have never found the need to put the full path in to python on cron
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

User avatar
DougieLawson
Posts: 32355
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Crontab Issue with Paho MQTT

Wed May 16, 2018 6:10 pm

Change

Code: Select all

def mqttPublish(lst):
	client.connect("localhost", 1883, 60) # if broker on another machine replace localhost with I/P address
	client.publish("topic/WtrHtr", str(lst));
	client.disconnect();
	return
to

Code: Select all

def mqttPublish(payload):

    broker="localhost"
    port = 1883

    rc = client.connect(broker, port, 60)
    tries = 5
    while rc !=0:
        try:
            rc = client.reconnect()
            if rc != 0:
                rc = client.connect(broker, port, 60)
            if rc != 0:
                raise Exception('connectionError')
        except:
            tries -= 1
            client.disconnect()
            if tries == 0:
                raise Exception('connectionException')

    client.publish("topic/WtrHtr", str(payload))
as my version includes some retry logic.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

OldPCGuy
Posts: 73
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

Re: Crontab Issue with Paho MQTT

Thu May 17, 2018 11:24 pm

Doug,

I'm new to try/except blocks.

If I understand correctly the try: block only handles exception for a failed to connect. The publish statement will always execute after the while block is done, payload just won't go anywhere?

Code: Select all

    client.publish("topic/WtrHtr", str(payload))
Also are 10 attempts made at a connection? the reconnect followed by the connect looped 5 times?

OldPCGuy
Posts: 73
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

Re: Crontab Issue with Paho MQTT

Fri May 18, 2018 12:35 am

So the code runs by does not publish. I assume the try block is doing its thing error handling. Or is it.... I'm not sure the file I think is running is actually running :cry:

I confirmed the broker is running via cmd line pub/sub .

Digging deeper I notice Geany IDE saves the script *.py.py and *.pyc. Where as Python2 & 3 save *.py

When I try and run the script with Python2 or 3 IDE I first get a write permission error on the CSV file. If I delete the file so it is created fresh I get a the following error;

Code: Select all

Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "copyright", "credits" or "license()" for more information.
>>> 
===================== RESTART: /home/pi/Multi_DS18B20.py =====================
Traceback (most recent call last):
  File "/home/pi/Multi_DS18B20.py", line 10, in <module>
    import paho.mqtt.client as mqtt
ImportError: No module named 'paho'
Why can Geany find and link the Paho module but not Python? I put the explicate path on my crontab.

Code: Select all

@reboot sleep 60 && python /home/pi/Multi_DS18B20.py
I did not anticipate the *.py.py file name/extension. So I'm not sure crontab is launching the same script I've been editing with the Geany IDE! What am I missing?

PhatFil
Posts: 182
Joined: Thu Apr 13, 2017 3:55 pm

Re: Crontab Issue with Paho MQTT

Fri May 18, 2018 1:37 am

python beginner here too, so i could be shooting wide? but how did you install paho? and other resources i think you may need to use pip3 to install for python3 and pip for python2. if you installed it with another tool for the Geany ide that could be the problem?

you may need to install pip/pip3 with npm too?

User avatar
DougieLawson
Posts: 32355
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Crontab Issue with Paho MQTT

Fri May 18, 2018 6:08 am

OldPCGuy wrote:
Thu May 17, 2018 11:24 pm
Doug,

I'm new to try/except blocks.

If I understand correctly the try: block only handles exception for a failed to connect. The publish statement will always execute after the while block is done, payload just won't go anywhere?

Code: Select all

    client.publish("topic/WtrHtr", str(payload))
Also are 10 attempts made at a connection? the reconnect followed by the connect looped 5 times?
The publish doesn't run if any exception happens. That will stop the program dead in its tracks.

Code: Select all

                raise Exception('connectionError')
kills the program dead.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

OldPCGuy
Posts: 73
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

Re: Crontab Issue with Paho MQTT

Sat May 19, 2018 10:45 pm

PhatFil wrote:
Fri May 18, 2018 1:37 am
python beginner here too, so i could be shooting wide? but how did you install paho? and other resources i think you may need to use pip3 to install for python3 and pip for python2. if you installed it with another tool for the Geany ide that could be the problem?

you may need to install pip/pip3 with npm too?
Phil thanks, you got me pointed in the right direction.

I went to the Paho-MQTT web site and reviewed the install instruction. You should be able to install with PIP and have support for Py 2.7 and 3.X

https://www.eclipse.org/paho/clients/python/

That's where I noticed a note that under Linux or similar OS you may need to install under sudo. I don't think I did that originally.

Not completely familiar with PIP I decided to downloaded their clone from GitHub and manually install under sudo. That did the trick! ;)

Thanks to All!

Return to “Python”

Who is online

Users browsing this forum: No registered users and 11 guests