Raspberry Pi WiFi Radio Script Issues

674 Views Asked by At

I am having a problem and since I don't really know much about Python, I would appreciate the help of someone else to help me understand what my problem is.

I am building a Portable Wireless Radio. The Raspberry Pi utilizes Pianobar to go out to the Pandora servers, log in to my account, get my stations, and then begin playing the first one.

I am following the official Adafruit guide: https://learn.adafruit.com/pi-wifi-radio/overview

I have followed the guide all the way up until Pianobar was working. I am able to run "pianobar" from the command line. It connects and begins playing music in less than 10 seconds.

However, when I start the script that allows the 16x2 LCD Keypad to interface with pianobar, it doesn't work.

More specifically, it gets through the first half of the script. The LCD displays the IP address and says "Retrieving Station List...". After 10 seconds, the script exits with all of this.

pi@pandora ~/Python-WiFi-Radio $ sudo python PiPhi.py

Spawning pianobar...
Receiving station list...
Traceback (most recent call last):
  File "PiPhi.py", line 288, in <module>
    stationList, stationIDs = getStations()
  File "PiPhi.py", line 190, in getStations
    pianobar.expect('Select station: ', timeout=10)
  File "/usr/local/lib/python2.7/dist-packages/pexpect.py", line 1311, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "/usr/local/lib/python2.7/dist-packages/pexpect.py", line 1325, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "/usr/local/lib/python2.7/dist-packages/pexpect.py", line 1409, in expect_loop
    raise TIMEOUT (str(e) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
<pexpect.spawn object at 0xb6b305b0>
version: 2.3 ($Revision: 399 $)
command: /usr/bin/sudo
args: ['/usr/bin/sudo', '-u', 'pi', 'pianobar']
searcher: searcher_re:
    0: re.compile("Select station: ")
TIME: -03:35/03:43
TIME: -03:35/03:43
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 2315
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

pi@pandora ~/Python-WiFi-Radio $ 

http://pastebin.com/6Lm3dTwx - THIS IS THE SCRIPT THAT I AM TRYING TO RUN

From my basic knowledge, it looks like it it taking longer than whatever timeout is to retrieve the station list. Please help me as I am completely lost. Thanks!

4

There are 4 best solutions below

0
On

I had the same problem, for a low tech fix I just pinged google 10 times in the startup script. This gave the system long enough to get the network connection stabalized.

0
On

I found that userid "pi" is hard coded in PiPhi.py! Changing line 33 (PICKLEFILE), 286 (pepect.spawn('sudo -u pi... solved my problem ..

hope this helps ..

0
On

There are potentially two issues here. I was having difficulty spawning the process. This shows up as an EOF error at the pianobar.excect for 'Get stations...Ok.\r\n'. To see what is happening process the EOF exception and print pianobar.before:

# Launch pianobar as pi user (to use same config data, etc.) in background:
print('Spawning pianobar...')
pianobar = pexpect.spawn('sudo -u pi /home/pi/pianobar/pianobar', timeout=60)
print('Receiving station list...')
expectIdx = pianobar.expect(['Get stations... Ok.\r\n', pexpect.EOF, pexpect.TIMEOUT])
if expectIdx == 0:
    stationList, stationIDs = getStations()
    try:    # Use station name from last session
        stationNum = stationList.index(defaultStation)
    except: # Use first station in list
        stationNum = 0
    print 'Selecting station ' + stationIDs[stationNum]
    pianobar.sendline(stationIDs[stationNum])
elif expectIdx == 1: # EOF
    print 'pianobar.expect EOF error'
    print pianobar.before # shows response from pianobar spawn
    pianobar.kill(0)
elif expectIdx == 2: # TIMEOUT
    print 'pianobar.expect TIMEOUT error'
    pianobar.kill(0)

I fixed my problem by specifying the full path for pianobar (as shown above).

The second issue could be because you have a valid default station in your pianobar configuration. If this is the case the select station list is not displayed at startup any you will need to request it. This error shows up at the pianobar.expect in getStations(). I fixed this by requesting the station list if the initial request timed out:

    expectIdx = pianobar.expect(['Select station: ', pexpect.EOF, pexpect.TIMEOUT], timeout=10)
    if expectIdx == 1: # EOF
        print 'pianobar.expect EOF error at getStations'
        print pianobar.before # shows response from pianobar spawn
        pianobar.kill(0)
    elif expectIdx == 2: # TIMEOUT
        # try requesting the station list
        pianobar.send('s')
        pianobar.expect('Select station', timeout=10)

0
On

I know this is old but I've just encountered the problem myself. I found that issuing the following command:

pianobar.send('s')

before

pianobar.expect('Select station: ', timeout=20)

forced pianobar to update the station list