Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start() hangs #1

Closed
VipSaran opened this issue Jan 4, 2014 · 16 comments
Closed

start() hangs #1

VipSaran opened this issue Jan 4, 2014 · 16 comments

Comments

@VipSaran
Copy link

VipSaran commented Jan 4, 2014

Code:

    import pigpio
    pigpio.start()
    pigpio.stop()

Result (only comes after Ctrl+C):

$ sudo python test.py 
^CTraceback (most recent call last):
  File "test.py", line 4, in <module>
    pigpio.start()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1547, in start
    _notify = _callback_thread()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 346, in __init__
    self.handle = _pigpio_command(self.sock, _PI_CMD_NOIB, 0, 0)
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 316, in _pigpio_command
    x, y, z, res = struct.unpack('IIII', sock.recv(16))
KeyboardInterrupt
@joan2937
Copy link
Owner

joan2937 commented Jan 5, 2014

There are timing issues with starting and then stopping straight away. Of course I never do that in my own usage so I forget. As a work around a time.sleep(1) before the stop() works for me.

How are you running the code?

From a Linux machine I guess as Windows would give a different error message.

Are you running the code on a Pi or on a box on the same network as the Pi?

Are you typing the code into a Python interpreter or are you running a script?

@VipSaran
Copy link
Author

VipSaran commented Jan 5, 2014

Direct access (ssh) and running on the Pi from the script as: sudo python test.py.

@joan2937
Copy link
Owner

joan2937 commented Jan 5, 2014

You don't need the sudo (you only need to use sudo to start the pigpiod daemon).

I can't reproduce the error you get. I get

soft ~ $ cat test.py
import pigpio

pigpio.start()

pigpio.stop()


soft ~ $ python test.py
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    pigpio.stop()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1583, in stop
    _notify.stop()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 351, in stop
    if self.go:
AttributeError: '_callback_thread' object has no attribute 'go'

soft ~ $ grep VERSION /usr/local/include/pigpio.h
#define PIGPIO_VERSION 9
soft ~ $ 

@VipSaran
Copy link
Author

VipSaran commented Jan 5, 2014

I've added sleep(1), but, as I feared, it changed nothing.
As I said in the original comment, the error comes only AFTER the interrupt.
Just as then, I have now let program "run" for few minutes, but it obviously hangs as it exits (with this error) only when I press Ctrl+C:

$ python test.py 
^CTraceback (most recent call last):
  File "test.py", line 5, in <module>
    pigpio.start()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1547, in start
    _notify = _callback_thread()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 346, in __init__
    self.handle = _pigpio_command(self.sock, _PI_CMD_NOIB, 0, 0)
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 316, in _pigpio_command
    x, y, z, res = struct.unpack('IIII', sock.recv(16))
KeyboardInterrupt

Again the same --> the error is in start() which never finishes (normally).

The script:

import sys
import pigpio
from time import sleep

pigpio.start()
sleep(1)
pigpio.stop()

sys.exit(0)

@joan2937
Copy link
Owner

joan2937 commented Jan 5, 2014

All I can think of is that you are using a version of pigpio prior to that which supports in band socket notifications.

Did you make/make install to install the latest pigpio and python module or did you just extract the tar and do a sudo python setup.py install yourself?

Do a grep for the pigpio version in /usr/local/include.

@VipSaran
Copy link
Author

VipSaran commented Jan 5, 2014

I followd this:

wget abyz.co.uk/rpi/pigpio/pigpio.tar
tar xvf pigpio.tar
cd PIGPIO
make
make install

all passed OK.

This is the (grep for) version:

usr/local/include $ grep version pigpio.h 
This version is for pigpio version 9
gpioHardwareRevision       Get hardware version.
   Returns the pigpio version number if OK, otherwise PI_INIT_FAILED.
/* Used to print a readable version of the current waveform to stdout.

@VipSaran
Copy link
Author

VipSaran commented Jan 5, 2014

I have now cloned the GitHub source.
This is the result of run make; make install:

$ make; make install
gcc -O3 -Wall   -c -o pigpio.o pigpio.c
gcc -O3 -Wall   -c -o command.o command.c
ar rcs libpigpio.a pigpio.o command.o
ranlib libpigpio.a
size   libpigpio.a
   text    data     bss     dec     hex filename
  65804     148  368328  434280   6a068 pigpio.o (ex libpigpio.a)
   3316     548       0    3864     f18 command.o (ex libpigpio.a)
gcc -O3 -Wall   -c -o checklib.o checklib.c
gcc -o checklib checklib.c -L. -lpigpio -lpthread -lrt
gcc -O3 -Wall   -c -o demolib.o demolib.c
gcc -o demolib demolib.c -L. -lpigpio -lpthread -lrt
gcc -O3 -Wall   -c -o pig2vcd.o pig2vcd.c
gcc -o pig2vcd pig2vcd.c
gcc -O3 -Wall   -c -o pigpiod.o pigpiod.c
gcc -o pigpiod pigpiod.c -L. -lpigpio -lpthread -lrt
gcc -o pigs pigs.c command.c
sudo install -m 0755 -d          /usr/local/include
sudo install -m 0644 pigpio.h    /usr/local/include
sudo install -m 0755 -d          /usr/local/lib
sudo install -m 0644 libpigpio.a /usr/local/lib
sudo install -m 0755 -d          /usr/local/bin
sudo install -m 0755 pig2vcd     /usr/local/bin
sudo install -m 0755 pigpiod     /usr/local/bin
sudo install -m 0755 pigs        /usr/local/bin
sudo python setup.py install
running install
running build
running build_py
creating build
creating build/lib.linux-armv6l-2.7
copying pigpio.py -> build/lib.linux-armv6l-2.7
running install_lib
copying build/lib.linux-armv6l-2.7/pigpio.py -> /usr/local/lib/python2.7/dist-packages
byte-compiling /usr/local/lib/python2.7/dist-packages/pigpio.py to pigpio.pyc
running install_egg_info
Removing /usr/local/lib/python2.7/dist-packages/pigpio-1.0.egg-info
Writing /usr/local/lib/python2.7/dist-packages/pigpio-1.0.egg-info

Version:

$ grep version pigpio.h 
This version is for pigpio version 9

Same problem (after kbd interrupt):

$ python test.py 
^CTraceback (most recent call last):
  File "test.py", line 5, in <module>
    pigpio.start()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1547, in start
    _notify = _callback_thread()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 346, in __init__
    self.handle = _pigpio_command(self.sock, _PI_CMD_NOIB, 0, 0)
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 316, in _pigpio_command
    x, y, z, res = struct.unpack('IIII', sock.recv(16))
KeyboardInterrupt

@joan2937
Copy link
Owner

joan2937 commented Jan 5, 2014

Did you sudo pigpiod after building?

@VipSaran
Copy link
Author

VipSaran commented Jan 5, 2014

yes

@joan2937
Copy link
Owner

joan2937 commented Jan 5, 2014

I'm stumped.

I think you need to give full details of your setup and versions.

I've used the module on Windows (Vista, Python 3.3), laptop (Linux 3.11-2-amd64, Python 3.3, Python 2.7.6), Pi 1 (Raspbian Linux 3.10.25, Python 2.7.3, Python 3.2.3) Pi 2 (Debian Linux 3.10.25, Python 2.7.3, Python 3.2.3).

#########################################################################
Troubleshooting.

Does pigs br1, pigs br2 return hex numbers (confirms pigpiod is running).

Do the pigpio pipes exist? There should be /dev/piopio, /dev/pigout, /dev/pigerr

Does cat /dev/pigerr & report any errors?

Leave cat /dev/pigerr & running in the background and switch on pigpio debugs by sending a signal to the daemon.

sudo killall -USR2 pigpiod
sudo killall -USR2 pigpiod
sudo killall -USR2 pigpiod
sudo killall -USR2 pigpiod

Each -USR2 increases the debug level, -USR1 decreases the debug level.

Then do python test.py

You should see.

soft ~ $  cat /dev/pigerr &
[1] 3983
soft ~ $ sudo killall -USR2 pigpiod
soft ~ $ sudo killall -USR2 pigpiod
soft ~ $ sudo killall -USR2 pigpiod
2014-01-05 15:09:38 sigHandler: Debug level 3

soft ~ $ sudo killall -USR2 pigpiod
2014-01-05 15:09:40 sigHandler: Debug level 4

soft ~ $ python test.py
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    pigpio.stop()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1583, in stop
    _notify.stop()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 351, in stop
    if self.go:
AttributeError: '_callback_thread' object has no attribute 'go'
soft ~ $
2014-01-05 15:09:53 gpioNotifyOpenInBand: 
2014-01-05 15:09:53 gpioNotifyClose: handle=0

@VipSaran
Copy link
Author

Same.

cat /dev/pigerr &
[1] 20228
pi@raspi ~ $ sudo killall -USR2 pigpiod
pi@raspi ~ $ sudo killall -USR2 pigpiod
pi@raspi ~ $ sudo killall -USR2 pigpiod
2014-01-14 10:15:33 sigHandler: Debug level 3

pi@raspi ~ $ sudo killall -USR2 pigpiod
2014-01-14 10:15:35 sigHandler: Debug level 4

pi@raspi ~ $ cd python
pi@raspi ~/python $ python test.py
^CTraceback (most recent call last):
  File "test.py", line 5, in <module>
    pigpio.start()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1547, in start
    _notify = _callback_thread()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 346, in __init__
    self.handle = _pigpio_command(self.sock, _PI_CMD_NOIB, 0, 0)
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 316, in _pigpio_command
    x, y, z, res = struct.unpack('IIII', sock.recv(16))
KeyboardInterrupt

When I run the python script, there is no pigerr output until I press Ctrl+C.

@joan2937
Copy link
Owner

The only thing I can think of is that port 8888 is being used by another process and you are talking to that rather than pigpiod. However in that case I would have expected sudo pigpiod to have thrown up an error about port in use.

I just used netstat --listen to find ports being listened to on my local machine. I chose one at random and specified that as PIGPIO_PORT and started Python.

I get similar but not identical results (straight away though).

hard ~ $ export PIGPIO_PORT=6010
hard ~ $ python
Python 2.7.3 (default, Jan 13 2013, 11:20:46) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pigpio
>>> pigpio.start()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1558, in start
    _notify = _callback_thread()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 347, in __init__
    self.handle = _pigpio_command(self.sock, _PI_CMD_NOIB, 0, 0)
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 316, in _pigpio_command
    x, y, z, res = struct.unpack('IIII', sock.recv(16))
struct.error: unpack requires a string argument of length 16
>>> 

I suggest the following.

netstat --listen | grep 8888
sudo killall pigpiod
sudo rm /var/run/pigpio.pid
netstat --listen | grep 8888
sudo pigpiod
netstat --listen | grep 8888

hard ~ $ netstat --listen|grep 8888
tcp        0      0 *:8888                  *:*                     LISTEN     
hard ~ $ sudo killall pigpiod
hard ~ $ sudo killall pigpiod
pigpiod: no process found
hard ~ $ sudo rm /var/run/pigpio.pid
rm: cannot remove `/var/run/pigpio.pid': No such file or directory
hard ~ $ netstat --listen|grep 8888
hard ~ $ sudo pigpiod
hard ~ $ cat /dev/pigerr &
[1] 3742
hard ~ $ netstat --listen|grep 8888
tcp        0      0 *:8888                  *:*                     LISTEN     
hard ~ $ pigs r 4
0

I'm afraid this is beyond my experience and am at a bit of a loss as to know how to proceed.

Have you got a funny firewall setting?

Could you try running the Python from a different networked machine?

@VipSaran
Copy link
Author

pi@raspi ~/python $ export PIGPIO_PORT=6010
pi@raspi ~/python $ sudo pigpiod
pi@raspi ~/python $ python test.py 
********************************************************
Can't connect to pigpio on localhost(6010)

Did you start the pigpio daemon?
(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.start() function
(e.g. pigpio.start('soft', 8888))
********************************************************
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    pigpio.start()
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1546, in start
    _control.connect((_host, _port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
pi@raspi ~/python $ 

The 8888 port was reserved by RPi-Monitor. Since I wasn't using it, I removed it and pigpio is now working (at least the test.py script doesn't hang and exits as expected, can't test more as I'm not at home now).

Strange how the export PIGPIO_PORT did not fix the problem...

@joan2937
Copy link
Owner

The export was deliberately to a bad port (a port used by a different service) to try to get the same problem as you were seeing.

What is RPi-Monitor? If a lot of people use it I might have to search for another port to use by default.

pigpio by default uses 8888 but may be started with another port. However anything that uses pigpio must also be started with that port.

If you are happy this issue is resolved I will close it.

@VipSaran
Copy link
Author

RPi-Monitor: http://bit.ly/1cipgEh

You can close the case. Thanks.

@SlySven
Copy link
Contributor

SlySven commented Feb 28, 2016

You might like to note the "Unofficial" use of port 8888 on, say, here at Wikipedia . It is a good central place that people might look at! 😀

Looking at the code of PiMonitor though - it does use the same default port of 8888 see ./rpimonitor/daemon.conf so - given that neither project has the explicit LANA "ownership" of it - it suggests a few extra words in the feedback to users that there may be something else (and perhaps quote "RPi-Monitor" as a specific case) that may also be trying to use the same Port might be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants