Pali24
Posts: 5
Joined: Fri Jan 16, 2015 10:30 am

Python script problem in terminal

Fri Jan 16, 2015 1:36 pm

Hi everybody,
I really have an interesting problem. My Python script runs very well, without mistakes if I use Idle(3) in grafical mode, but if I start the same program in terminal, it has some mistakes. In this case the main problem, that it does not use the interrupts :( (GPIO.add_event_detect and so on). Have You ever met this funny things?
This is a net radio program with a 2x16 LCD display and 4 push buttons for changing the stations and the volume. The push buttons work in interrupts. I attache the code, You can try it, I like it. ;) First a short initial text, after a scrolling text shows the radio station, and the details of the song.
When I start the program in terminal, the music and the scrolling text starts according to the expectations, but I can not use the push buttons, so I suppose, that the interrupt system does not work. In the terminal there are not warnings, but it shows only the mpc datas.

Code: Select all

[code]#!/usr/bin/python
 
# The wiring for the LCD is as follows:
# 1 : GND
# 2 : 5V
# 3 : Contrast (0-5V)*
# 4 : RS (Register Select)
# 5 : R/W (Read Write)       - GROUND THIS PIN! We do not want the LCD to send anything to the Pi @ 5v
# 6 : Enable or Strobe
# 7 : Data Bit 0             - NOT USED
# 8 : Data Bit 1             - NOT USED
# 9 : Data Bit 2             - NOT USED
# 10: Data Bit 3             - NOT USED
# 11: Data Bit 4
# 12: Data Bit 5
# 13: Data Bit 6
# 14: Data Bit 7
# 15: LCD Backlight +5V
# 16: LCD Backlight GND (Red)
 
#import
import RPi.GPIO as GPIO
import time
import os
 
# Define GPIO to LCD mapping
LCD_RS = 25
LCD_E  = 8
LCD_D4 = 7
LCD_D5 = 10
LCD_D6 = 9
LCD_D7 = 11
LCD_LI = 4
 
# Define GPIO for Radio Controls
NEXT = 22
PREV = 27
VOLUP = 23
VOLDO = 24
 
# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
 
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

# Timing constants
E_PULSE = 0.0001  #0.00005
E_DELAY = 0.0001  #0.00005

GPIO.setwarnings(False)
 
def main():

  # Main program block
  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  
  GPIO.setup(NEXT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  GPIO.setup(PREV, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  GPIO.setup(VOLUP, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  GPIO.setup(VOLDO, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
  GPIO.add_event_detect(NEXT, GPIO.FALLING, bouncetime=800)
  GPIO.add_event_callback(NEXT,nextsong)
  GPIO.add_event_detect(PREV, GPIO.FALLING, bouncetime=800)
  GPIO.add_event_callback(PREV,prevsong)
  GPIO.add_event_detect(VOLUP,GPIO.FALLING, bouncetime=800)
  GPIO.add_event_callback(VOLUP,volumeup)
  GPIO.add_event_detect(VOLDO,GPIO.FALLING, bouncetime=800)
  GPIO.add_event_callback(VOLDO,volumedown)

  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  GPIO.setup(LCD_LI, GPIO.OUT) # Backlight

  os.system("mpc clear")
  os.system("mpc load dplist")
  os.system("mpc volume 30")
  
  while 1:
    a = 0
    b = 1

    GPIO.output(LCD_LI, False)      # Initialise display
    os.system("mpc play")    
    time.sleep(0.4)  
    lcd_init()
    text()
    
    while (a <= b):
      f=os.popen("mpc current")
      title = f.read()
      title = title[:-1]            #delete EOF signal from the end
      str_pad = " " * 15            #15 space at the front of the line
      title = str_pad + title
      b = len(title)
      lcd_byte(LCD_LINE_1, LCD_CMD)
      lcd_text = title[a:(a+16)]
      a += 1
      lcd_string(lcd_text,1)
      time.sleep(0.4)
      if GPIO.event_detected(NEXT): #if you press the NEXT button
        a = 0
        text()
      if GPIO.event_detected(PREV): #if you press the PREV button
        a = 0
        text()
      if (GPIO.input(VOLUP) == False) & (GPIO.input(VOLDO) == False):
        b = 0                       #if press both VOL button, program pause
        os.system("mpc stop")        
        GPIO.remove_event_detect(VOLUP)
        text2()
        GPIO.output(LCD_LI, True)
        GPIO.wait_for_edge(VOLUP, GPIO.FALLING)
        GPIO.remove_event_detect(VOLUP)
        GPIO.add_event_detect(VOLUP,GPIO.FALLING, bouncetime=300)
        GPIO.add_event_callback(VOLUP,volumeup)



def text():                  # Send some text
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Raspberry Pi",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("Internet Radio",2)
  time.sleep(2)


def text2():                  # Send some text
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Raspberry Pi",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("Bye bye!",2)
  time.sleep(2)


def nextsong(NEXT):
  f = os.popen("mpc -f %position% current")
  current_Pos = int(f.read())
  if current_Pos == 5:
    os.system("mpc play 1")
  elif current_Pos < 5:
    os.system("mpc next")
  time.sleep(0.3)


def prevsong(PREV):
  os.system("mpc prev")
  time.sleep(0.3)


def volumeup(VOLUP):
  while GPIO.input(VOLUP) == False:
    os.system("mpc volume +3")
    time.sleep(0.3)


def volumedown(VOLDO):
  while GPIO.input(VOLDO) == False:
    os.system("mpc volume -3")
    time.sleep(0.3)
  
 
def lcd_init():
  # Initialise display
  time.sleep(0.05)
  lcd_byte(0x33,LCD_CMD)
  time.sleep(0.01)
  lcd_byte(0x32,LCD_CMD)
  time.sleep(0.001)  
  lcd_byte(0x28,LCD_CMD)   # 4 bits, 2 lines, 5x8 dots
  lcd_byte(0x0C,LCD_CMD)   # Display on, Cursor off, Blinking off
  lcd_byte(0x06,LCD_CMD)   # Cursor increment
  lcd_byte(0x02,LCD_CMD)   # DD RAM Address = 0
  time.sleep(0.005)
  lcd_byte(0x01,LCD_CMD)   # Clear Display
  time.sleep(0.005) 
  

def lcd_string(message,style):
  # Send string to display
  # style=1 Left justified
  # style=2 Centred
  # style=3 Right justified

  if style==1:
    message = message.ljust(LCD_WIDTH," ")  
  elif style==2:
    message = message.center(LCD_WIDTH," ")
  elif style==3:
    message = message.rjust(LCD_WIDTH," ")

  for i in range(LCD_WIDTH):
    omessage = ord(message[i])
    if omessage==0x70:   # p
      omessage = 0xF0
    elif omessage==0x79: # y
      omessage = 0xF9
    elif omessage==0x71: # q
      omessage = 0xF1
    elif omessage==0x67: # g
      omessage = 0xE7
    elif omessage==0x6A: # j
      omessage = 0xEA      
    else:
      omessage = ord(message[i])
    lcd_byte(omessage,LCD_CHR)
  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)
    
 
def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command
 
  GPIO.output(LCD_RS, mode) # RS
 
  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)
 
  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)      
 
  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)
 
  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)   
 
if __name__ == '__main__':
  main()
[code]

Pali24
Posts: 5
Joined: Fri Jan 16, 2015 10:30 am

Re: Python script problem in terminal

Fri Jan 16, 2015 2:13 pm

Ahaaaa, there are warnings in terminal mode if I comment the
GPIO.setwarnings(False) line, as follows:

Code: Select all

[email protected] ~ $ sudo python3 /home/pi/Python/net_radio.py
/home/pi/Python/net_radio.py:74: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_E, GPIO.OUT)  # E
/home/pi/Python/net_radio.py:75: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
/home/pi/Python/net_radio.py:76: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
/home/pi/Python/net_radio.py:77: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
/home/pi/Python/net_radio.py:78: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
/home/pi/Python/net_radio.py:79: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
/home/pi/Python/net_radio.py:80: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_LI, GPIO.OUT) # Backlight
volume: 27%   repeat: off   random: off   single: off   consume: off
loading: dplist
volume: 30%   repeat: off   random: off   single: off   consume: off
http://mp2.somafm.com:2666

Pali24
Posts: 5
Joined: Fri Jan 16, 2015 10:30 am

Re: Python script problem in terminal

Mon Jan 26, 2015 8:01 am

Hi, the problem is solved, I could find, but I do not understand why :roll: I am a beginner for programming.
If the Media Player Client (mpc) starts first, the interrupt does not work. So at first I have to start the mpc:
os.system("mpc play")
and after I initialize the interrupt:
GPIO.add_event_detect..... and so on
In this case the python script works well if I start it in terminal :)

Return to “Python”