hostname or any system command output to 2x16 LCD help.


8 posts
by sim_tcr » Sun Dec 16, 2012 5:19 pm
Hello,

I have a 2x16 LCD and am able to display text on it using GPIO pins of raspberry. I am using below python code for it.

Code: Select all
#import
import RPi.GPIO as GPIO
import time
import commands

# Define GPIO to LCD mapping
LCD_RS = 7
LCD_E  = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18
LED_ON = 15

# 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.00003
E_DELAY = 0.00003

def main():
  # Main program block

  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  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(LED_ON, GPIO.OUT) # Backlight enable

  # Initialise display
  lcd_init()

  # Toggle backlight on-off-on
  GPIO.output(LED_ON, True)
  time.sleep(1)
  GPIO.output(LED_ON, False)
  time.sleep(1)
  GPIO.output(LED_ON, True)
  time.sleep(1)

  # Send some centred test
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string(Raspberry, 2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("IV Beginners",2)

  time.sleep(3) # 3 second delay

def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD) 
  lcd_byte(0x06,LCD_CMD)
  lcd_byte(0x01,LCD_CMD) 

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):
    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()


Now I want to display a system command output to LCD using the above python script, for eg.
a = hostname

How do display 'a' using this part of code?
Code: Select all
  # Send some centred test
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string(Raspberry, 2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("IV Beginners",2)
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by alexeames » Sun Dec 16, 2012 5:35 pm
These three bits should do you...

Code: Select all
import subprocess

near the top of the file

Code: Select all
a = subprocess.Popen(['hostname'], stdout=subprocess.PIPE).communicate()[0]

or something like it, should stuff the hostname in variable a (I use this code snippet in another program).

Code: Select all
lcd_string('Hostname:%s' % a)

Will write "Hostname:" and then your hostname in whichever line of the lcd you chose. :)
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2068
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by sim_tcr » Mon Dec 17, 2012 8:33 am
alexeames wrote:These three bits should do you...

Code: Select all
import subprocess

near the top of the file

Code: Select all
a = subprocess.Popen(['hostname'], stdout=subprocess.PIPE).communicate()[0]

or something like it, should stuff the hostname in variable a (I use this code snippet in another program).

Code: Select all
lcd_string('Hostname:%s' % a)

Will write "Hostname:" and then your hostname in whichever line of the lcd you chose. :)


Thank you. That worked.
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by alexeames » Mon Dec 17, 2012 11:24 am
sim_tcr wrote:Thank you. That worked.


Excellent :D
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2068
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by sim_tcr » Tue Dec 18, 2012 7:19 am
alexeames wrote:
sim_tcr wrote:Thank you. That worked.


Excellent :D


I found one issue, instead of hostname command, if i put a little complex command its erroring. For eg,
Code: Select all
a = subprocess.Popen(['who | tr -d " "'], stdout=subprocess.PIPE) .communicate() [0]


error is,

pi@raspiabita ~/lcdpy $ sudo python LCD_text.py
Traceback (most recent call last):
File "LCD_text.py", line 42, in <module>
a = subprocess.Popen(['who | tr -d " "'], stdout=subprocess.PIPE) .communicate() [0]
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory


How to resolve it?
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by alexeames » Tue Dec 18, 2012 12:44 pm
sim_tcr wrote:How to resolve it?


I hope someone else can sort you out as I don't know what the problem is there.
The error message is quite alarming isn't it?

"Execute child"

What did the poor kid do? :lol:
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2068
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by terrycarlin » Tue Dec 18, 2012 9:14 pm
UNIX is quite the violent operating system.
If it ain't broke, take it apart and see how it works.
User avatar
Posts: 70
Joined: Thu Jun 14, 2012 10:42 pm
by sim_tcr » Wed Dec 19, 2012 8:05 am
I found another python script to do this nicely.
Below link has all details including wiring and python scripts.
http://learn.adafruit.com/drive-a-16x2- ... pberry-pi/
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore