Page 1 of 1

Imports causing python script to hang at startup

Posted: Sun May 05, 2019 11:10 am
by mattg31
I have a python script running on a raspberry pi that I want to run automatically at startup. I had this working until I added a temperature sensor, and now the program wont run automatically at startup, but will run fine with no errors if I run it manually.

Is there something I don't know about certain imports at startup?

What works:

I modify `rc.local` by adding `python /home/pi/myscript.py &` near the end of the file.

where myscript.py =

Code: Select all

    #!/usr/bin/python3
  
    import RPi.GPIO as GPIO #for GPIO naming convention
    import os #for power circuit
    import serial #for reading serial port on USB
    import time
    import threading 

    def power_button():

	    GPIO.setmode(GPIO.BCM)#set to GPIO Values
	    GPIO.setwarnings(False)
	    GPIO.setup(4,GPIO.OUT)#relay coil
	    GPIO.setup(22,GPIO.OUT)#power LED
	    GPIO.setup(27,GPIO.IN)#power button sense
	    GPIO.output(4,GPIO.HIGH)#turn on coil
	    GPIO.output(22,GPIO.HIGH)#turn on power LED
	    powerButton = GPIO.input(27)

	    while powerButton ==1: #check for change, then shutdown
		    powerButton = GPIO.input(27)
		
	    #change occured, as program left the while loop, proceed below
	    GPIO.output(22,GPIO.LOW)#turn off power LED
	    os.system('sudo shutdown -h now')

	
    #power button thread
    thread1 = threading.Thread(target=power_button) 
    thread1.start() 
Like mentioned above, this code works and runs properly at startup automatically.

What doesn't work:
When I add my temperature sensor imports, it will no longer start automatically, but will run fine if I run manually after the pi boots up.

myscripy.py with more imports added:

Code: Select all

    #!/usr/bin/python3

    import RPi.GPIO as GPIO #for GPIO naming convention
    import os #for power circuit
    import serial #for reading serial port on USB
    import time
    import threading 
    import adafruit_sht31d #sht temphumid sensor
    import board #sht temphumid sensor
    import busio #sht temphumid sensor
    import adafruit_sht31d #sht temphumid sensor
    import requests #for posting



    # Create library object using our Bus I2C port for SHT31 temp humid sensor
    i2c = busio.I2C(board.SCL, board.SDA)
    sensor = adafruit_sht31d.SHT31D(i2c)


    def power_button():

	    GPIO.setmode(GPIO.BCM)#set to GPIO Values
	    GPIO.setwarnings(False)
	    GPIO.setup(4,GPIO.OUT)#relay coil
	    GPIO.setup(22,GPIO.OUT)#power LED
	    GPIO.setup(27,GPIO.IN)#power button sense
	    GPIO.output(4,GPIO.HIGH)#turn on coil
	    GPIO.output(22,GPIO.HIGH)#turn on power LED
	    powerButton = GPIO.input(27)

	    while powerButton ==1: #check for change, then shutdown
		    powerButton = GPIO.input(27)
		
	    #change occured, as program left the while loop, proceed below
	    GPIO.output(22,GPIO.LOW)#turn off power LED
	    os.system('sudo shutdown -h now')

	
	
    #power button thread
    thread1 = threading.Thread(target=power_button) 
    thread1.start() 

Any ideas?

Re: Imports causing python script to hang at startup

Posted: Sun May 05, 2019 1:09 pm
by bensimmo
You probably need to log the errors how are you starting it?
It could just be a permissions problem (e.g. needs to be run as pi user)
Or it may need a delay so everything is up and ready.

(As a note, it looks like you are importing the same thing twice, sht31t ?)

Re: Imports causing python script to hang at startup

Posted: Sun May 05, 2019 1:11 pm
by bensimmo
I modify `rc.local` by adding `python /home/pi/myscript.py &` near the end of the file.
I must read better
Try changing to python3

It's often better to do (I've found anyway)

Code: Select all

cd /home/pi
su pi -c "python3 my script.py > thelogfile.log 2>&1"
That way you can see what is going on (any print statements and any error or crashes in the order they appear, normally)

Re: Imports causing python script to hang at startup

Posted: Sun May 05, 2019 1:16 pm
by scruss
One of the libraries you're importing is expecting a network connection? The network might not be up by the time rc.local is run.

Re: Imports causing python script to hang at startup

Posted: Mon May 06, 2019 9:57 am
by mattg31
Thanks for the feedback guys. I tried quite a few things to make this work, ultimately it was making it executable, then running it as pi user from rc.local (/bin/su -c "/home/pi/myscript.py" - pi &), then finally adding in a 10 second delay to the beginning of the program to let everything startup before all the imports occur.
Thanks again!