I am using Raspberry Pi’s since the first generation and I use them a lot. Over the time I collected a few snippets that I find really useful and want to same them with you.
Turn off display programmatically
I’ve got a Magic Mirror and by default its screen would be always on. Thats not necessary. It should be on when I am looking at it. So I added a PIR Sensor for motion detection and let it trigger the following python lines:
def turn_on():
bashCommand1 = ["tvservice -p"]
bashCommand2 = ["export DISPLAY=:0.0"]
bashCommand3 = ["xset dpms force on"]
bashCommand4 = ["fbset -depth 8; fbset -depth 16; xrefresh -d :0.0"]
process1 = subprocess.Popen(bashCommand1, stdout=subprocess.PIPE, shell=True)
time.sleep(1)
process2 = subprocess.Popen(bashCommand2, stdout=subprocess.PIPE, shell=True)
time.sleep(1)
process3 = subprocess.Popen(bashCommand3, stdout=subprocess.PIPE, shell=True)
process4 = subprocess.Popen(bashCommand4, stdout=subprocess.PIPE, shell=True)
def turn_off():
bashCommand = ["tvservice -o"]
process = subprocess.Popen(bashCommand, stdout=subprocess.PIPE, shell=True)
Put external HDD into deepsleep
If you have an external HDD attached to your Raspberry Pi it will run all the time. Even if you don’t use it. Even if you sleep. Thats a waste of energy and shortens the life of the HDD. I used this command to let my HDD sleep and it automatically wakes up when I use it. Awesome.
sudo hd-idle -i 0 -a /dev/disk/by-uuid/0038-8EE1
Programmatically unplug and replug USB devices
Sometime you have to restart your USB device because they ‚hang‘. Good example that I expericanced a lot is Bluetooth dongles. Since I don’t wanted to go physically to the device and replug it, I came up with this:
lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 0a5c:21e8 Broadcom Corp.
cd /sys/bus/usb/devices/
grep -i -s -r 0a5c *
…
1-1.2/idVendor:0a5c
…
cd 1-1.2
echo 0 > authorized
echo 1 > authorized