I'm trying to learn how to safely and reliably start and stop the pigipod DEOMON process from within a python script.
If pigpiod happens to be running already, then this script is always successful. It stops pigpiod and then starts it again.
But if pigpiod is NOT running when I run this, then MOST of the time it fails, and the standard message block that starts with "Can't connect to pigpio at localhost(8888)" appears when I try to instantiate pi = pigoio.pi()
This happens even though os.system('sudo pigpiod')
returns 0
, there are no exceptions, and a .sleep(1)
was executed "just to make sure".
- The FIRST time I run it after boot, it is always successful.
- if pigpiod was already running, then it is always successful.
- If I run a few seconds after a failure, it seems to always fail again.
- It seems if I wait a LONG TIME after a failure, like several minutes, then run it again, it is more likely to be successful.
Question: What could cause os.system('sudo pigpiod')
to fail to start pigpiod successfully some of the time, but still always return 0
and throw no exceptions?
edit: RPi 3, Python 2.7, Raspian 8.0 jessie
Failure looks like this:
START: trying os.system('sudo pigpiod')
and the returned status is: 0
sleeping for 1 second...
pigpiod started successfully...
...because status is Falsy
...and so I conclude that all is well.
START: I'm DONE
BOX: making a pi
MAKE_A_PI: I am going to try to instantiate a pi
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)
Did you start the pigpio daemon? E.g. sudo pigpiod
Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888
Did you specify the correct Pi host/port in the
pigpio.pi() function? E.g. pigpio.pi('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MAKE_A_PI: I'm DONE
satus of pi.connected: False
BOX: setting a GPIO pin to True:
Traceback (most recent call last):
File "/home/pi/PiProblemo/PiProblem.py", line 25, in <module>
BOX.pi.write(GPIO, 1)
File "/usr/lib/python2.7/dist-packages/pigpio.py", line 1376, in write
return _u2i(_pigpio_command(self.sl, _PI_CMD_WRITE, gpio, level))
File "/usr/lib/python2.7/dist-packages/pigpio.py", line 977, in _pigpio_command
sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
AttributeError: 'NoneType' object has no attribute 'send'
My main script:
#!/usr/bin/env python
if __name__ == "__main__":
import time, pigpio
from PiBox import PiBox
# Instantiate the Box:
BOX = PiBox.Box('my new Box')
print "BOX instantiated with name: ", BOX.name
print "BOX: starting pigpiod"
BOX.start_pigpiod()
print "BOX: making a pi"
BOX.make_a_pi()
print "satus of pi.connected: ", BOX.pi.connected
GPIO = 27
print "BOX: setting a GPIO pin to True:"
BOX.pi.write(GPIO, 1)
print "BOX: reading it gives: ", BOX.pi.read(GPIO)
print "BOX: setting a GPIO pin to False:"
BOX.pi.write(GPIO, 0)
print "BOX: reading it back gives: ", BOX.pi.read(GPIO)
My PiBox module, I've left the verbose print statements in to help explain what I believe each step is doing. It's certainly safe to ignore or delete them:
class Box(object):
def __init__(self, name):
self.name = name
def start_pigpiod(self):
import os, time
print "START: going to call STOP first."
self.stop_pigpiod()
print "START: STOP has been called ."
print "START: try to START again."
try:
print "START: trying os.system('sudo pigpiod')"
status = os.system('sudo pigpiod')
print " and the returned status is: ", status
print " sleeping for 1 second..."
time.sleep(1)
if not status:
print " pigpiod started successfully..."
print " ...because status is Falsy"
print " ...and so I conclude that all is well."
else:
print " pigpiod did not start successfully..."
print " ...because status is Truthy"
print " ...and so I conclude that there's been a problem"
except Exception, e:
print "START: There's been an exception"
print " and it is: ", str(e)
print "START: I'm DONE"
def stop_pigpiod(self):
import os, time
print "STOP: try to STOP."
try:
print "STOP: trying os.system('sudo killall pigpiod')"
status = os.system('sudo killall pigpiod') # stop it in case it's running
print " and the returned status is: ", status
print " sleeping for 1 second..."
time.sleep(1)
if not status:
print " pigpiod stopped successfully..."
print " ...because status is Falsy"
print " ...and so I conclude that all is well."
else:
print " pigpiod not stopped (probably wasn't started!)..."
print " ...because not status is Truthy"
print " ...but I still think that all is well."
except:
print " OH NO! there was some exception while stopping pigpiod!"
print "I AM STOP, and I'm DONE!"
def make_a_pi(self):
import time, pigpio
print "MAKE_A_PI: I am going to try to instantiate a pi"
try:
self.pi = pigpio.pi() # local GPIO only
print " I'm going to sleep for 1 sec..."
time.sleep(1)
except Exception, e:
print "MAKE_A_PI: There's been an exception..."
print " message is: ", str(e)
print "MAKE_A_PI: I'm DONE"