rob_pi
Posts: 8
Joined: Mon May 21, 2012 9:01 pm

Programmatically turn screen off

Tue Jun 05, 2012 5:50 pm

Does anyone know how I might be able to programmatically switch the monitor off? It's connected by HDMI. I'd be happy if I could just disable output from the HDMI port, and then the monitor would switch itself off, but I can't manage even this!

I've tried

Code: Select all

$ xset dpms force off
but this just blanks the screen - the LCD monitor remains on. I think this is because the monitor is connected over HDMI, and so the EnergyStar stuff doesn't work.

I've also tried code that's supposed to disable the driver in the kernel:

Code: Select all

# echo "bcm2708_fb" > /sys/bus/platform/drivers/bcm2708_fb/unbind
This seemed to work (/dev/fb0 disappeared), but the display (with X running) carried on regardless.

Anybody got any other ideas? I think HDMI-CEC would be the gold-plated solution, and I know people are working on libcec, but I've no idea when this will be released.

The project is an always-on device, but to save power I want to power down the monitor when it's not needed.

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6940
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Programmatically turn screen off

Tue Jun 05, 2012 6:10 pm

try
/opt/vc/bin/tvservice -o

rob_pi
Posts: 8
Joined: Mon May 21, 2012 9:01 pm

Re: Programmatically turn screen off

Tue Jun 05, 2012 6:23 pm

That looks like just the ticket! but..

Code: Select all

root@raspberrypi:~# /opt/vc/bin/tvservice -o
Powering off HDMI
root@raspberrypi:~# /opt/vc/bin/tvservice -s
state 0x12000a, 1920x1080 @ 60Hz, progressive
root@raspberrypi:~# /opt/vc/bin/tvservice -M
Starting to monitor for HDMI events
[I] HDMI cable is unplugged
[I] HDMI in standby mode
^CShutting down...
Which looks like it's all working great, except the monitor rather stubbornly remains on, and is still showing updates from X..?

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6940
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Programmatically turn screen off

Tue Jun 05, 2012 6:28 pm

Can you try it again. I've a slight recollection it needs to be told twice...

Just tested, and it's not working for me. I'll look into it.

rob_pi
Posts: 8
Joined: Mon May 21, 2012 9:01 pm

Re: Programmatically turn screen off

Tue Jun 05, 2012 6:52 pm

It worked for me once - now it seems to be a bit intermittent.

rob_pi
Posts: 8
Joined: Mon May 21, 2012 9:01 pm

Re: Programmatically turn screen off

Wed Jun 06, 2012 12:44 am

I can get it to reliably turn hdmi off if I run /opt/vc/bin/tvservice -p before running /opt/vc/bin/tvservice -o.

The hdmi port can then be turned on again by running /opt/vc/bin/tvservice -p.

My challenge now is to get X to "reconnect" to the re-available monitor; I can do it by killing X, unbind'ing and bind'ing the driver and then restarting X but that seems a bit overkill!

unsightlygod
Posts: 4
Joined: Fri Jun 15, 2012 10:30 pm

Re: Programmatically turn screen off

Fri Jun 15, 2012 10:39 pm

Hi Guys,

Found this thread while searching for a way to put my HDMI->DVI connected monitor to sleep.

I discovered that switching virtual consoles once the screen has been turned back on gets the system going again.

I've been tinkering with a few custom graphics apps, launched via a script passed as an argument to startx.

Code: Select all

startx /usr/bin/graphical_launcher `fgconsole`
In doing so I pass the foreground console session number, then as soon as the app starts I retrieve the new foreground session number that X is using.

The first thing the script does when executed by startx is retrive the consoles.

Code: Select all

VT_START=$1
VT_XSERVER=`fgconsole`
Then upon turning the screen back on I simply run:

Code: Select all

chvt $VT_START
chvt $VT_XSERVER
It's a bit of a hack but seems to be working nicely,

Hope it helps,

Phil

rob_pi
Posts: 8
Joined: Mon May 21, 2012 9:01 pm

Re: Programmatically turn screen off

Tue Jun 19, 2012 10:56 pm

Hi Phil, Thanks for your reply!

I've been struggling with how to get the screen going for an age now and your method above works really well, except when the monitor comes back the image is moved over by about 200 pixels; and wraps around from the other side. It's a bit odd really and I can't find a way of fixing it!

unsightlygod
Posts: 4
Joined: Fri Jun 15, 2012 10:30 pm

Re: Programmatically turn screen off

Tue Jun 19, 2012 11:54 pm

@rob_pi What's the resolution of your screen? Would be interested to see if I can reproduce the wraparound you've been experiencing. You using a PC monitor or TV?

svetlev
Posts: 6
Joined: Sun Jul 01, 2012 10:46 pm

Re: Programmatically turn screen off

Mon Sep 17, 2012 11:36 pm

unsightlygod wrote:Hi Guys,

Found this thread while searching for a way to put my HDMI->DVI connected monitor to sleep.

I discovered that switching virtual consoles once the screen has been turned back on gets the system going again.

I've been tinkering with a few custom graphics apps, launched via a script passed as an argument to startx.

Code: Select all

startx /usr/bin/graphical_launcher `fgconsole`
In doing so I pass the foreground console session number, then as soon as the app starts I retrieve the new foreground session number that X is using.

The first thing the script does when executed by startx is retrive the consoles.

Code: Select all

VT_START=$1
VT_XSERVER=`fgconsole`
Then upon turning the screen back on I simply run:

Code: Select all

chvt $VT_START
chvt $VT_XSERVER
It's a bit of a hack but seems to be working nicely,

Hope it helps,

Phil
Hi, can you elaborate just a bit. Is this

Code: Select all

VT_START=$1
VT_XSERVER=`fgconsole`
what goes in your /usr/bin/graphical_launcher shell script?

Thanks so much and sorry for replying to an old post, but this is the closest I've found to a solution after much searching.

rob_pi
Posts: 8
Joined: Mon May 21, 2012 9:01 pm

Re: Programmatically turn screen off

Wed Mar 27, 2013 10:05 pm

Apologies for bumping an old post; but have finally revisited this and figured out a way to achive this:

download/install cec-client from http://sourceforge.net/projects/selfpro ... b/download

Code: Select all

sudo dpkg -i libcec_2.1.0-1_armhf.deb
Verify it's working ok:

Code: Select all

pi@raspberrypi:~$ cec-client -l
Found devices: 1

device:              1
com port:            RPI
vendor id:           2708
product id:          1001
firmware version:    1
type:                Raspberry Pi
Turn TV off(standby):

Code: Select all

pi@raspberrypi:~$ echo "standby 0" | cec-client -s -d 1
opening a connection to the CEC adapter...
Turn TV on:

Code: Select all

pi@raspberrypi:~$ echo "on 0" | cec-client -s -d 1
opening a connection to the CEC adapter...

averell23
Posts: 1
Joined: Tue Apr 09, 2013 10:12 am

Re: Programmatically turn screen off

Tue Apr 09, 2013 10:16 am

libcec didn't work for me. I didn't have time to debug further, but here's the output in case anyone wants to give it another spin:

Code: Select all

echo "standby 0" | cec-client -s -d 0
== skipped log-level parameter: invalid level '0' ==
opening a connection to the CEC adapter...
DEBUG:   [             142]	unregistering all CEC clients
DEBUG:   [             145]	Broadcast (F): osd name set to 'Broadcast'
DEBUG:   [             148]	InitHostCEC - vchiq_initialise succeeded
DEBUG:   [             151]	InitHostCEC - vchi_initialise succeeded
DEBUG:   [             153]	InitHostCEC - vchi_connect succeeded
DEBUG:   [             156]	logical address changed to Broadcast (f)
DEBUG:   [             158]	RegisterLogicalAddress - registering address e
DEBUG:   [             340]	logical address changed to Recorder 1 (1)
DEBUG:   [             340]	logical address changed to Free use (e)
DEBUG:   [             340]	Open - vc_cec initialised
NOTICE:  [             340]	connection opened
DEBUG:   [             342]	<< Broadcast (F) -> TV (0): POLL
DEBUG:   [             345]	initiator 'Broadcast' is not supported by the CEC adapter. using 'Free use' instead
TRAFFIC: [             346]	<< e0
DEBUG:   [             347]	processor thread started
DEBUG:   [             439]	command 'POLL' was not acked by the controller
DEBUG:   [             439]	initiator 'Broadcast' is not supported by the CEC adapter. using 'Free use' instead
TRAFFIC: [             439]	<< e0
DEBUG:   [             530]	command 'POLL' was not acked by the controller
DEBUG:   [             530]	>> POLL not sent
DEBUG:   [             530]	TV (0): device status changed into 'not present'
NOTICE:  [             530]	registering new CEC client - v2.1.1
DEBUG:   [             530]	detecting logical address for type 'recording device'
DEBUG:   [             531]	trying logical address 'Recorder 1'
DEBUG:   [             531]	<< Recorder 1 (1) -> Recorder 1 (1): POLL
TRAFFIC: [             531]	<< 11
DEBUG:   [             531]	UnregisterLogicalAddress - releasing previous logical address
DEBUG:   [             532]	logical address changed to Broadcast (f)
DEBUG:   [             535]	RegisterLogicalAddress - registering address 1
DEBUG:   [             717]	logical address changed to Free use (e)
DEBUG:   [             718]	logical address changed to Recorder 1 (1)
TRAFFIC: [             718]	<< 11
DEBUG:   [             718]	>> POLL not sent
DEBUG:   [             718]	using logical address 'Recorder 1'
DEBUG:   [             718]	Recorder 1 (1): device status changed into 'handled by libCEC'
DEBUG:   [             718]	Recorder 1 (1): power status changed from 'unknown' to 'on'
DEBUG:   [             719]	Recorder 1 (1): CEC version 1.4
DEBUG:   [             719]	AllocateLogicalAddresses - device '0', type 'recording device', LA '1'
DEBUG:   [             719]	Recorder 1 (1): osd name set to 'CECTester'
DEBUG:   [             719]	Recorder 1 (1): menu language set to 'eng'
DEBUG:   [             720]	GetPhysicalAddress - physical address = ffff
DEBUG:   [             723]	SetDevicePhysicalAddress - not setting invalid physical address ffff
NOTICE:  [             731]	setting HDMI port to 1 on device TV (0)
DEBUG:   [             733]	<< Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [             734]	<< 10
DEBUG:   [             825]	command 'POLL' was not acked by the controller
TRAFFIC: [             826]	<< 10
DEBUG:   [             916]	command 'POLL' was not acked by the controller
DEBUG:   [             916]	>> POLL not sent
DEBUG:   [             917]	Recorder 1 (1): physical address changed from ffff to 1000
DEBUG:   [             917]	<< Recorder 1 (1) -> broadcast (F): physical adddress 1000
TRAFFIC: [             917]	<< 1f:84:10:00:01
NOTICE:  [            1068]	CEC client registered: libCEC version = 2.1.1, client version = 2.1.1, firmware version = 1, logical address(es) = Recorder 1 (1) , base device: TV (0), HDMI port number: 1, physical address: 1.0.0.0, host: armv6l-unknown-linux-gnueabihf, features: 'P8 USB' 'P8 USB detect' 'RPi', git revision: 3ff78be, compiled on: Mon Mar 25 06:49:17 UTC 2013 by pi@raspberrypi on Linux 3.6.11+ (armv6l)
DEBUG:   [            1068]	<< Recorder 1 (1) -> TV (0): OSD name 'CECTester'
DEBUG:   [            1069]	<< Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [            1069]	<< 10
DEBUG:   [            1159]	command 'POLL' was not acked by the controller
TRAFFIC: [            1160]	<< 10
DEBUG:   [            1250]	command 'POLL' was not acked by the controller
DEBUG:   [            1250]	>> POLL not sent
DEBUG:   [            1251]	not sending command 'set osd name': destination device 'TV' marked as not present
DEBUG:   [            1251]	<< requesting power status of 'TV' (0)
DEBUG:   [            1251]	<< Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [            1251]	<< 10
DEBUG:   [            1342]	command 'POLL' was not acked by the controller
TRAFFIC: [            1342]	<< 10
DEBUG:   [            1433]	command 'POLL' was not acked by the controller
DEBUG:   [            1433]	>> POLL not sent
DEBUG:   [            1433]	not sending command 'give device power status': destination device 'TV' marked as not present
DEBUG:   [            1434]	<< Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [            1434]	<< 10
DEBUG:   [            1525]	command 'POLL' was not acked by the controller
TRAFFIC: [            1525]	<< 10
DEBUG:   [            1616]	command 'POLL' was not acked by the controller
DEBUG:   [            1616]	>> POLL not sent
NOTICE:  [            1616]	<< putting 'TV' (0) in standby mode
DEBUG:   [            1617]	<< Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [            1617]	<< 10
DEBUG:   [            1707]	command 'POLL' was not acked by the controller
TRAFFIC: [            1708]	<< 10
DEBUG:   [            1798]	command 'POLL' was not acked by the controller
DEBUG:   [            1799]	>> POLL not sent
DEBUG:   [            1799]	not sending command 'standby': destination device 'TV' marked as not present
DEBUG:   [            1799]	unregistering all CEC clients
NOTICE:  [            1799]	unregistering client: libCEC version = 2.1.1, client version = 2.1.1, firmware version = 1, logical address(es) = Recorder 1 (1) , base device: TV (0), HDMI port number: 1, physical address: 1.0.0.0, host: armv6l-unknown-linux-gnueabihf, features: 'P8 USB' 'P8 USB detect' 'RPi', git revision: 3ff78be, compiled on: Mon Mar 25 06:49:17 UTC 2013 by pi@raspberrypi on Linux 3.6.11+ (armv6l)
DEBUG:   [            1799]	Recorder 1 (1): power status changed from 'on' to 'unknown'
DEBUG:   [            1799]	Recorder 1 (1): CEC version unknown
DEBUG:   [            1800]	Recorder 1 (1): osd name set to 'Recorder 1'
DEBUG:   [            1800]	Recorder 1 (1): device status changed into 'unknown'
DEBUG:   [            1800]	unregistering all CEC clients
DEBUG:   [            1801]	UnregisterLogicalAddress - releasing previous logical address
DEBUG:   [            1805]	logical address changed to Broadcast (f)

cleverca22
Posts: 8194
Joined: Sat Aug 18, 2012 2:33 pm

Re: Programmatically turn screen off

Sun Apr 14, 2013 10:58 am

are you using a desktop monitor, or a tv?, only tv's have cec

User avatar
jojopi
Posts: 3862
Joined: Tue Oct 11, 2011 8:38 pm

Re: Programmatically turn screen off

Sun Apr 14, 2013 11:39 am

Is tvservice really not working? Power off monitor:

Code: Select all

tvservice -o
Power on:

Code: Select all

tvservice -p; fbset -depth 8; fbset -depth 16

hans.mussmann
Posts: 1
Joined: Wed Jun 12, 2013 10:58 pm

Re: Programmatically turn screen off

Wed Jun 12, 2013 11:06 pm

Thanks to code found here I thought I should post what worked for me.

This will go in a cron job to switch screen off during the night and on during the day.

Placed this in:
/usr/bin/screen.sh

and call it with:
/usr/bin/screen.sh off
or
/usr/bin/screen.sh on

Code: Select all

#!/bin/bash

if [ "$1" == 'on' ]; then
  tvservice -p;
  fbset -depth 8;
  fbset -depth 16;
  chvt 6;
  chvt 7;
  echo 'Switched Screen ON!'
fi

if [ "$1" == 'off' ]; then
  tvservice -o
  echo 'Switched Screen OFF!'
fi

peterjaap
Posts: 1
Joined: Mon Nov 04, 2013 4:16 pm

Re: Programmatically turn screen off

Mon Nov 04, 2013 4:18 pm

The above code did not work for me, because of my Bash not agreeing with the if structure. Here's my code;

Code: Select all

#!/bin/bash

if [ $1 = 'on' ]; then
  tvservice -p;
  fbset -depth 8;
  fbset -depth 16;
  chvt 6;
  chvt 7;
  echo 'Switched Screen ON!'
fi

if [ $1 = 'off' ]; then
  tvservice -o
  echo 'Switched Screen OFF!'
fi

dmichel76
Posts: 5
Joined: Wed Jan 08, 2014 4:56 pm

Re: Programmatically turn screen off

Fri Jan 10, 2014 10:02 am

I'm working on an application where I also have to programmatically turn the hdmi output on and off.

Turning it off with

Code: Select all

tvservice -o
and back on with

Code: Select all

tvservice -p
followed by

Code: Select all

chvt 6
chvt 7
seems to work fine...

But calling 'chvt 7' without calling 'chvt 6' before doesn't... Is there no way to go back to the tty7 directly?

In my case, it is critical to be able to go back to my full-screen app, without any other text, consoles prompts appearing...


David

User avatar
jojopi
Posts: 3862
Joined: Tue Oct 11, 2011 8:38 pm

Re: Programmatically turn screen off

Fri Jan 10, 2014 5:31 pm

dmichel76 wrote:But calling 'chvt 7' without calling 'chvt 6' before doesn't... Is there no way to go back to the tty7 directly?
You are already on tty7. I assume that switching away and back is being done for the side-effect of making Xorg redraw all windows. You can get that effect more cleanly with "xrefresh" (provided your script has inherited the DISPLAY and XAUTHORITY variables from inside the X session).

Instead of tty6, you can also switch to one that is blank, such as 9.

dmtroyer
Posts: 2
Joined: Mon Oct 06, 2014 6:47 pm

Re: Programmatically turn screen off

Mon Oct 06, 2014 6:51 pm

Has anyone finished a solution for this? I'm still new to Pi. I see solutions in development here, but I'm not sure they are complete.
/troyer

broo0ose
Posts: 318
Joined: Wed Dec 14, 2011 3:59 pm
Location: Wirral, UK

Re: Programmatically turn screen off

Tue Oct 07, 2014 6:05 pm

I got this working a while ago like this ...
To turn the screen off

Code: Select all

tvservice -o
Then I have script to turn it back on..

Code: Select all

#!/bin/sh
tvservice --preferred > /dev/null
fbset -depth 8; fbset -depth 16; xrefresh
pkill -HUP midori
I can't take much credit for this as I copied it from somewhere, can't remember where.

note the pkill -HUP command reloads the application with the name midori, which causes the screen to refresh properly. Otherwise the screen can get corrupted with large black areas. You must change the name if you are using a different browser.

dmtroyer
Posts: 2
Joined: Mon Oct 06, 2014 6:47 pm

Re: Programmatically turn screen off

Tue Oct 07, 2014 7:34 pm

Thanks!

m1cha3ll0w3
Posts: 9
Joined: Tue Sep 09, 2014 10:00 pm

Re: Programmatically turn screen off

Sun Mar 13, 2016 10:17 pm

Hello All
This is a very interesting thread and would be super useful if my problem concerns a tv.
But it doesnt!? I got sick of my other family members not knowing about how rpi works and they seem
currently unwilling to learn! I wasnt about to stand for this due to them always switching it off at the wall
without proper poweroff and shutdown. So part of my solution was to move it to another room and run it as a plexmedia server for the entire household, which seems to be working fairly sweetly. trouble is on initial set up I needed a screen to see what I was doing as at the time I hadnt learnt how to ssh into it.
Anyway to cut a long story short I now have ssh putty on desktop with which I ssh into rpi and admin the system remotely (still within my home network, obviously) my question to you is now that I can ssh
is it really necessary to have the screen on (I bought a 5 inch lcd) if I leave the pi on permanently which was my original intention since I very first bought it, the screen stays on too and never sleeps, which to me seems very irrisponsible and wasteful of energy! So what I was thinking was either option 1:
Leave the lcd attached but figure out how to turn it off until further notice without it affecting the performance or the normal operation of the server and the system running on the rpi, and if ever needs be which I think probably never will happen, be able to send a command via ssh to turn it on again?!

Option 2: (And I dont know if this is actually possible, as Ive noticed sometimes this happens when pulling out other peripherals the rpi reboots!) Just remove the screen altogether, as I assume this is what one does in order to run it as a headless server? there must be a way I am sure I saw something on the web while browsing once about running headless server on rpi?? are there some prescribed measures that must be taken before and after removing lcd screen so as to do it with operational safety and health and safety (avoid electric shock) concerned about my kids! they have already noticed it sitting there looking like an innocent 5 inch (mobile screen)
I just want to do this properly?!
Any help would be hugely appreciated and apologies for replying to this old thread, cheers.

PS. I thought it would be helpful to mention that the external hdd connected to the pi and from which all the media files are served from is not self powered. I bought one of those 7 port usb hubs from the pi-hut to supply the required juice as they are mains powered, suffice to say it never got recognised by the pi or any other computer in our household for that matter, I should have returned it but never got round to it(real life gets in the way sometimes!) anyway I found a temp solution (Not ideal at all and probably the cause of the screen never sleeping!!???!!)
I have been using a tiny 4 port usb hub which I lost the psu adapter for, never mind - turns out my archos gmini 205 mains adapter supplies almost the right voltage to power the usb hub, and therefore power the connected external hdd, however I think it is too much voltage? And it was so long ago that I did all the configuration for plex and all that mount point nonsense and fuser bla bla that I just dont want to mess with replacing this botch job but at the same time the screen never sleeping is really really annoying even if I shutdown the pi and switch it off with the switch (yes I have one of those nice on/off switches for my pi) the screen remains on because of the usb hub continuing to supply power to the ext hdd and subsequently the rpi itself? so I had to resort to pulling the power to the usb hub everytime which when you are trying to run a server kind of defeats the point, you really dont even want to know its there, it should just be quietly serving from some corner of the room, doing what its supposed to do without making such a fuss over it!!!!!!! anyone know of any solutins to my dilemma (a recommended usb hub that is easy to use with rpi and plex would be the obvious solution I would have thought?? I dont want to use a different drive as this is a 1TB drive which has so many of my other NB files on it as well which I now ftp to and access from my desktop, so in actual fact it is a multipurpose server.
Also I have the Rpi rev 2 board with 1GB RAM not the original rpi) Thanks in advance for any help, cheers! sorry for being so long winded just thought I would provide as much detail as possible so we dont end up playing email or forum tennis! ;-)

turtlegood
Posts: 7
Joined: Tue Feb 24, 2015 11:45 pm

Re: Programmatically turn screen off

Sun May 22, 2016 4:09 am

peterjaap wrote:The above code did not work for me, because of my Bash not agreeing with the if structure. Here's my code;
...
Partly worked for me. As for my solution, it is simpler:

Code: Select all

sudo tvservice -p
sudo chvt 6
sudo chvt 7
It is important that all commands should be executed by superuser. And the 6 before 7 is also significant.

azarubkin
Posts: 4
Joined: Tue Aug 23, 2016 8:16 am

Re: Programmatically turn screen off

Tue Aug 23, 2016 8:32 am

Another variant which works for me is:

Code: Select all

tvservice -p
xset -display :0.0 dmps force on

ftrueck
Posts: 1
Joined: Sun Mar 05, 2017 5:33 pm

Re: Programmatically turn screen off

Sun Mar 05, 2017 5:56 pm

For me this one works well.

Turn screen off:

Code: Select all

echo 0 > /sys/class/backlight/rpi_backlight/brightness
Turn screen on:

Code: Select all

echo 255 > /sys/class/backlight/rpi_backlight/brightness
CAUTION: you should set up a bootup script which sets brightness back to 255 as the setting seems to be persistent upon reboot.

Return to “Other projects”