0

I'm trying to check if remote machine (RaspberryPi) runs pigpiod- a daemon needed down the road to control its GPIO, using Python.

if remote/ machine does not run pigpiod an error is displayed :

> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at 192.168.2.112(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))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Traceback (most recent call last):
  File "readfile_ssh.py", line 72, in <module>
    factory = PiGPIOFactory(host=ad)
  File "/usr/local/lib/python3.5/dist-packages/gpiozero/pins/pigpio.py", line 90, in __init__
    raise IOError('failed to connect to %s:%s' % (host, port))
OSError: failed to connect to 192.168.2.112:8888


------------------
(program exited with code: 1)
Press return to continue

With try and except I tried to catch this error, with no success:

ad='192.168.2.112'
factory = PiGPIOFactory(host=ad)
try:
    switches= OutputDevice(4, pin_factory=factory,initial_value=False)
except OSError:
    print("BOOOO")

I tried to change except: OSError to except: IOError (also shown as part of error ) - still no luck.

1) Why can't it except this error ?

2) Is there a better way ( tried to look in pigpio's library ) to check if remote machine is running pigpiod

  • Not a python expert, but it looks to me like the exception is caught before reaching your catch statement and there is an explicit call to sys.exit(1). If that's the case, you cannot catch the exception because it was handled deeper in the stack than your code lies. – Brick Aug 31 '17 at 16:42
  • 1
    You can catch the error, but as joan's mentioned in the answer below you can't suppress the warning message (which is printed by the library prior to raising the exception; well, you can suppress it but only by closing or redirecting the output stream!). BTW, I'd love an option to suppress the error message - or just get rid of it entirely :) – Dave Jones Aug 31 '17 at 22:56
  • @DaveJones - redirecting it out is some way out, I would love that this error message will be optional. – Guy . D Sep 1 '17 at 17:07
  • @Brick can you give an example how to use sys.exit(1)? – Guy . D Sep 1 '17 at 17:07
  • The other comments suggest this is irrelevant, but to clarify what I said: You are calling a library that's throwing an exception. In theory, that library could also catch that exception and handle it. One way to "handle" it would be to call sys.exit(1), which would end the program instantly. If this happened, you could not catch the exception because control would never return to your code. Again, others who seem to know more about this specific library are saying this isn't the case, so maybe don't worry about this too much now. – Brick Sep 1 '17 at 17:33
2

I'm not sure why you can't trap the error message.

I would try tentatively connecting to port 8888 on the remote system before using pigpio.pi(). If the connect is successful it is highly likely that the pigpio daemon is present and running.

I will probably add an optional parameter to pigpio.pi() to disable the error message. The default would be to display the error message.

There is a connected instance variable which is true if the connection was okay.

pi = pigpio.pi()
if not pi.connected:
   print("Couldn't connect to daemon")
   exit()
  • joan: First- thank for your help. that is exaclty what I needed ( edited your answer with my exact need ). second - do you know how to catch the error ( as explained in question above ). – Guy . D Sep 1 '17 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.