[LINUX] HOWTO auto power on off your LG (maybe others) screen
#1
Star 
Hi i made this little howto for people searching for a way to turn on and off his LG LCD SCREEN (May work with any other brand that support commands with RS232), because i have no found any isntructions in the forums to tell how to do it:

1.- rs232 cable make / buy
i made it by myself, if you don't know how to solder maybe you can find a friend that do it for you..

2.- finding serial port number:
i issued this command on xbmc shell:

Code:
dmesg | grep ttyS
[    0.212718] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[    0.213132] 00:05: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
3.- test cable with linux minicom:

"insert a paperclip between pins 2 and 3 of the db9 to make a loop"
Code:
sudo apt-get install minicom
sudo minicom

CTRL-A Z
O Configure minicom
Serial port setup
A - Serial Device ttyS0
E - Bps/Par/Bits 9600
F - Hardware Flow Control No

Save setup as dfl
exit

ctrl-a X

sudo minicom

type any character it may see on the screen if cable is working and you hit the correct serial port

4.- Test tv commands with linux minicom:

Turn off:
sent this:
ka 0 0
receive this:
a 00 OK00x

Turn on:
sent this:
ka 0 1
receive this:
a 00 OK01x


5.- Install python serial (because seems to be the preferred programming language for xbmc):
Code:
sudo apt-get install python-serial
made 2 little scripts:

tv_on.py:
----------------------------------------------------------
import serial
ser = serial.Serial(0) # open first serial port
#print ser.portstr # check which port was really used
ser.write("ka 0 1\n") # string to turn off tv
#print ser.read(10) # read a '\n' terminated line
ser.close() # close port
-----------------------------------------------------------

tv_off.py:
-----------------------------------------------------
import serial
ser = serial.Serial(0) # open first serial port
#print ser.portstr # check which port was really used
ser.write("ka 0 0\n") # string to turn off tv
#print ser.read(10) # read a '\n' terminated line
ser.close() # close port
-------------------------------------------------------

Adding permision for using serial ports to xbmc user:
Code:
sudo usermod -a -G dialout xbmc
6.- Make init.d script (/etc/init.d/tvpower):
------------------------------------------------------------
#! /bin/sh
#
### BEGIN INIT INFO
# Provides: tvpower
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: tvpower
# Description: turns on and off lg lc screen
### END INIT INFO

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
NAME=tvpower
DESC=tvpower

set -e

case "$1" in
start)
python /home/xbmc/tv_on.py
;;
stop)
python /home/xbmc/tv_off.py
;;
restart|force-reload)
# nothing
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;

esac

exit 0
-------------------------------------------------------------------

7.- Add the script to startup:
Code:
sudo update-rc.d -f tvpower defaults 89 89
8.- For some reason that i cannot find yet, it doesn turn on the tv so i put it the following line on /etc/rc.local and that makes the trick for now:

/etc/rc.local:
---------------------------------------------------------------------
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
python /home/xbmc/tv_on.py

exit 0
----------------------------------------------------------------------

Hope this helps someone.
Regards, Jose
Reply
#2
i do nearly the same with my optoma beamer, but i don't use this on startup / shutdown, my htpc is running 24/7 and use other serial codes:
http://marketing.optomausa.com/PDFs/Prot..._RS232.pdf
Reply
#3
Wink 
overflowed Wrote:i do nearly the same with my optoma beamer, but i don't use this on startup / shutdown, my htpc is running 24/7 and use other serial codes:
http://marketing.optomausa.com/PDFs/Prot..._RS232.pdf

Please share your full experience, what are you exactly doing, and how, so you can help someone. :-)
Reply
#4
Nice guide Jose. I'll share my experience/improvements.

I just got my Epson Powerlite Home Cinema 3020 to turn on when I start XBMC and turn off when I exit.
OS is Ubuntu 12.04.

It also uses RS-232. I found the ASCII commands here: https://files.support.epson.com/Epson_Ha...21_e_P.pdf
Most important are the commands "PWR ON" and "PWR OFF". Also it states the baud rate is 9600.

I built a USB to RS-232 cable using these (could have bought it fully assembled I guess, but that's no fun.)
http://www.digikey.com/product-detail/en...ND/2451222
http://www.digikey.com/product-detail/en...ND/1836393

After I plug it in to my PC, I type
Code:
dmesg
and I see that it is connected to /dev/ttyUSB0

Instead of minicom, I used screen.
Code:
screen /dev/ttyUSB0 9600
When I type PWR ON and then enter key, the projector turned on! Sweet! PWR OFF turned it off.
To exit screen, type Control+a and then the \ key. (don't leave screen running because it hogs the serial port and then the rest of this won't work)

Then I tried your scripts, python-serial was already installed. (I modified the strings to turn on/off). Also, I didn't see the need to duplicate code in 2 scripts, so I added an argument of 'on' or 'off'.
/home/paul/tv_on.py
Code:
#!/usr/bin/python

import sys, serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)

if str(sys.argv[1]) == 'on':
    ser.write("PWR ON\r\n") # string to turn on tv
    print 'turning on'
elif str(sys.argv[1]) == 'off':
    ser.write("PWR OFF\r\n") # string to turn off tv
    print 'turning off'
print ser.read(10) # read a '\n' terminated line
ser.close() # close port

After make it executable, test it.
Code:
python tv_on.py on
and
Code:
python tv_on.py off

Then I wanted to make it run when XBMC is started to turn on the projector, and turn it off when I exit XBMC.
So I created a plugin for XBMC that will start when it launches.
/home/paul/.xbmc/addons/script.service.tv_on/addon.xml
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.tv.turnon"
       name="turn TV on"
       version="0.1"
       provider-name="phatpaul">
  <requires>
    <import addon="xbmc.python" version="2.0"/>
  </requires>
  <extension point="xbmc.service"
             library="xbmc_tv_on.py" start="[login|startup]">
  </extension>
  <extension point="xbmc.addon.metadata">
    <platform>all</platform>
    <summary lang="en">blah</summary>
  </extension>
</addon>

/home/paul/.xbmc/addons/script.service.tv_on/xbmc_tv_on.py (make sure it is executable!)
Code:
import xbmc, xbmcaddon, sys, serial

#Customize your serial port and baud here
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)

print 'turning on tv'
#Customize the string to turn on the tv
ser.write("PWR ON\r\n")
print ser.read(10) # read a '\n' terminated line

# wait here until closing xbmc
while (not xbmc.abortRequested):
    pass # Busy-wait

print 'turning off tv'
#Customize the string to turn off the tv
ser.write("PWR OFF\r\n")
print ser.read(10) # read a '\n' terminated line
ser.close() # close port

Cool.
- Paul
Reply

Logout Mark Read Team Forum Stats Members Help
[LINUX] HOWTO auto power on off your LG (maybe others) screen0