1

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.

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

  • Thanks. I have tried that, but I get "-bash: /sys/class/backlight/rpi_backlight/bl_power: Permission denied" even when using sudo. I dont quite understand why sudo doesnt override the security – Lee Melbourne Jun 7 '18 at 10:23
  • how bizarre, what permissions does it report ls -l /sys/class/backlight/rpi_backlight/ ? – rob Jun 7 '18 at 12:20
  • -r--r--r-- 1 root root 4096 Jun 10 21:11 actual_brightness -rw-r--r-- 1 root root 4096 Jun 10 21:11 bl_power -rw-r--r-- 1 root root 4096 Jun 10 21:11 brightness lrwxrwxrwx 1 root root 0 Jun 10 21:11 device -> ../../../rpi_backlight -r--r--r-- 1 root root 4096 Jun 10 21:11 max_brightness drwxr-xr-x 2 root root 0 Jun 10 21:11 power lrwxrwxrwx 1 root root 0 Jun 10 21:11 subsystem -> ../../../../../class/backlight -r--r--r-- 1 root root 4096 Jun 10 21:11 type -rw-r--r-- 1 root root 4096 Jun 10 21:11 uevent – Lee Melbourne Jun 10 '18 at 11:14
  • 1
    @LeeMelbourne Ensure you've disabled console blanking: github.com/notro/fbtft/wiki/Boot-console#console-blanking – Aaron F Jul 10 '18 at 12:22
  • 1
    @LeeMelbourne Most likely you're using sudo incorrectly. sudo works on commands, not on redirections, so sudo command > file runs the command elevated, but accesses the files with your initial permissions. – Dmitry Grigoryev May 28 '19 at 11:19
1

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.