2

I have a RPi3B with the RPi 7inch screen attached. I am running Jessie, but will re-image it to stretch soon. I would like to turn the screen on or off via Python. For example, when I ask what is the time I want the screen to wake up as I display a certain webpage on the screen that has time and date. Then after a certain amount of time I want it to go blank again.

How can I control the screen display Python or via command line?

I have found posts that explain how to stop the screen timeout, but none that explain how to toggle back and forth.

Lee Melbourne
  • 219
  • 1
  • 3
  • 15

3 Answers3

3

Command line:

OFF:

sudo -E sh -c 'echo 1 > /sys/class/backlight/rpi_backlight/bl_power'

ON:

sudo -E sh -c 'echo 0 > /sys/class/backlight/rpi_backlight/bl_power'

TIP: I define the above as backlight_off and backlight_on in ~/.bash_aliases. Be sure to save that file elsewhere, since you'll lose it if you re-image.

Python:

  • First save the above as backlight_off.sh and backlight_on.sh (with proper #!/bin/sh shebang header), maybe saved to /usr/local/bin/ if you want it available in your $PATH
  • ... then you can use Python subprocess() to invoke these new scripts. Use the "command syntax" to run the script files, and the script's own shell should take care of things.
  • If you are doing this in a web application (and therefore rightly concerned about security), then avoid use of variables around these calls (hardcode the Python subprocess calls), and get the code peer-reviewed.

If you read Subprocess documentation, it may contain suggestions to not use it to run shell scripts. That's fine in most cases, but not in this one.

FYI - "-c" is a life-saver

The "-c" option is often used to "wrap" commands into a user context, like sudo. You'll see this in some cron files, or used heavily by the Docker community.

More reading (and doing this in "pure" Python):

There are downsides to this, and you may need to run your Python script as sudo (not a good idea, generally) or make configuration changes to your Pi. You'll learn a bit going down this rabbit hole..

FYI this also worked:

pi@pi3:~ $ echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power
1

That works because the redirect is executed as sudo, but there is the unintended side-effect of 1 output text.

Scott Prive
  • 261
  • 2
  • 5
2

ok, so the permissions error reported in the comments is not pi specific, it's unix/linux specific.

sudo echo 1 > /sys/class/backlight/rpi_backlight/bl_power

will give you -bash: /sys/class/backlight/rpi_backlight/bl_power: Permission denied because the sudo is only applying to the echo statement, not the redirection of output to the /sys/ file.

One option is to sudo bash followed by echo 1 > /sys/class/backlight/rpi_backlight/bl_power.

Alternatively, sudo sh -c "echo 1 > /sys/class/backlight/rpi_backlight/bl_power". That takes everything in double quotes and runs it in a shell that has had it's privileges elevated.

mrda
  • 21
  • 2
1

Although not Python these is commands I use to turn the screen off and on.

turn screen OFF

echo 1 > /sys/class/backlight/rpi_backlight/bl_power

turn screen ON

echo 0 > /sys/class/backlight/rpi_backlight/bl_power

Note: I did have to run these via sudo/root

rob
  • 2,813
  • 4
  • 22
  • 31