Silent boot on Raspbian Stretch in Console Mode


Update (May 22, 2019):
Step 5 has been updated to adopt the latest version of Raspbian (2019-04-08).


The purpose of the steps here is to remove Rainbow image, Raspberry Pi Logo, login messages, etc. from Raspberry Pi boot up process when it’s in console autologin mode. For silent boot in desktop mode, please see this post. After following the steps, the first thing you see will be a shell prompt like below.

pi@raspberrypi:~ $

I tested it with Raspberry Pi 3 running Raspbian Stretch with Desktop (2019-04-08).

 

Assumptions

– Console Autologin
If it’s not yet in Console Autologin mode, launch raspi-config.

sudo raspi-config

Then, navigate to “Boot Options” > “Desktop / CLI” and select “Console Autologin“.

– Password change if SSH enabled
If SSH is enabled, change the login password to avoid the warning message below.

SSH is enabled and the default password for the 'pi' user has not been changed. 
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

 

Steps

1. Disable “Welcome to PIXEL” splash
Disable plymouth-start service.

sudo systemctl mask plymouth-start.service

 

2. Remove Rainbow Screen
2-1. Open “/boot/config.txt”.

sudo nano /boot/config.txt

2-2. Add below at the end of the file.

# Disable rainbow image at boot
disable_splash=1

 

3. Remove Raspberry Pi logo and blinking cursor
3-1. Open “/boot/cmdline.txt”.

sudo nano /boot/cmdline.txt

3-2. Add below at the end of the line.

logo.nologo vt.global_cursor_default=0

‘logo.nologo’ and ‘vt.global_cursor_default=0’ remove Raspberry Pi logo (image of four raspberries in the top left corner) and blinking cursor, respectively.

 

4. Remove login message
Create ‘.hushlogin’ file in home directory.

touch ~/.hushlogin

It removes login message below [1]:

Last login: Tue Nov 21 14:51:56 2017
Linux raspberrypi 4.9.41-v7+ #1023 SMP Tue Aug 8 16:00:15 BST 2017 armv7l
 
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
 
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

 

5. Remove autologin message by modify autologin service
5-1. Open “getty@tty1.service.d/autologin.conf”.

sudo nano /etc/systemd/system/getty@tty1.service.d/autologin.conf

5-2. Replace the line below:

ExecStart=-/sbin/agetty --autologin pi --noclear %I xterm-256color

with:

ExecStart=-/sbin/agetty --skip-login --noclear --noissue --login-options "-f pi" %I $TERM

It removes autologin message below:

Raspbian GNU/Linux 9 raspberrypi tty1
raspberrypi login: pi (automatic login)

 

6. Verify
Reboot the board and see if it works properly.

sudo reboot

 

Bonus 1: Removing Lightning Bolt Icon
In case a lightning bolt icon is shown in the right-top corner of the screen, see this post.

 

Bonus 2: Removing Bash Prompt
If you want to remove even the bash prompt (e.g. “pi@raspberrypi:~ $”), open .bashrc file.

nano ~/.bashrc

Then add the line below at the bottom of the file.

PS1=""

 

 

References
[1] Remove GNU licence and ‘no warranty’ thing when logging into ssh
[2] Silence and Appliance-ify your RetroPie Installation

 

Setting Up Raspberry Pi Web Kiosk


This post shows steps to setup a web kiosk using Raspberry Pi. The goal is to load a web page using a browser in full-screen kiosk mode directly after boot up.

I tested it with Raspberry Pi 3 B+ running Raspbian Stretch (November 2017 version) with Raspberry Pi official 7″ display. I used Chromium as browser. Also, Internet connection is required since the kiosk access a web site on the Internet.

 

Steps
1. install software
Install unclutter which hides mouse cursor.

sudo apt-get update
sudo apt-get install unclutter -y

 

2. Screen setting
2-1. Create .xinitrc in home directory.

nano ~/.xinitrc

2-2. Copy below lines, save and close the file.

#!/bin/sh

# disable blank screen
xset s off
xset -dpms
xset s noblank

# hide mouse cursor when it's not moving
unclutter -idle 0.1 &

# launch browser
exec chromium-browser --window-size=800,480 --incognito --kiosk --window-position=0,0 https://www.wunderground.com/fullscreenweather

Line 4-6 disables blank screen. [1]
Line 9 enables the mouse pointer only when it’s moving.
The browser loads full-screen weather [2] which is great for kiosk example. (Line 12)
‘incognito’ option disables session restore prompt on Chrome browser. [3] (Line 12)

 

3. Setting for launching browser when login
3-1. Open .bashrc.

nano ~/.bashrc

3-2. Add below lines at the end of the file.

if [ -z "${SSH_TTY}" ]; then
    startx > /dev/null 2>&1
fi

 

At this point, full-screen weather (specified at line 12 in step 2-2) will be shown after boot up. Check it by rebooting the board.
Below steps are to remove unnecessary stuff for kiosk, such as Rainbow image, Raspberry logo, Desktop GUI, login messages, etc

 

4. Disable Desktop Environment (PIXEL/LXDE)
Since desktop environment is not required for kiosk, let’s disable it.

4-1. Launch raspi-config.

sudo raspi-config

4-2. Navigate to “Boot Options” > “Desktop / CLI“, then select “Console Autologin“.

 

5. Disable “Welcome to PIXEL” splash
Disable plymouth-start service.

sudo systemctl mask plymouth-start.service

 

6. Remove Rainbow Screen
6-1. Open “/boot/config.txt”.

sudo nano /boot/config.txt

6-2. Add below at the end of the file.

# Disable rainbow image at boot
disable_splash=1

 

7. Remove Raspberry Pi logo and blinking cursor
7-1. Open “/boot/cmdline.txt”.

sudo nano /boot/cmdline.txt

7-2. Add below at the end of the line.

logo.nologo vt.global_cursor_default=0

‘logo.nologo’ and ‘vt.global_cursor_default=0’ remove Raspberry Pi logo and blinking cursor, respectively.

 

8. Remove login message
8-1. Create ‘.hushlogin’ file in home directory.

touch ~/.hushlogin

It removes login message below [4]:

Last login: Tue Nov 21 14:51:56 2017
Linux raspberrypi 4.9.41-v7+ #1023 SMP Tue Aug 8 16:00:15 BST 2017 armv7l
 
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
 
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

 

9. Remove autologin message by modify autologin service
9-1. Open the unit file for autologin service.

sudo nano /etc/systemd/system/autologin\@.service

9-2. Modify the line starts with “ExecStart”
From:

ExecStart=-/sbin/agetty --autologin pi --noclear %I $TERM

To:

ExecStart=-/sbin/agetty --skip-login --noclear --noissue --login-options "-f pi" %I $TERM

It removes autologin message below:

Raspbian GNU/Linux 9 raspberrypi tty1
raspberrypi login: pi (automatic login)

 

10. Verify
Now, everything should be ready. Reboot the board and see if it works properly.

sudo reboot

 

In case a lightning bolt icon is shown in the right-top corner of the screen, see this post.

 

 

References
[1] How do I prevent the screen from going blank?
[2] https://www.wunderground.com/fullscreenweather
[3] How to hide chrome warning after crash?
[4] Remove GNU licence and ‘no warranty’ thing when logging into ssh
[5] Setup a Raspberry Pi Kiosk with Chromium
[6] How to build a web kiosk with Raspberry Pi and make the SD read-only

 

 

Adding Power Switch on Raspberry Pi


Below shows how I added a physical power switch to Raspberry Pi. It enables boot up, shutdown, and reboot Raspberry Pi board.

Note: This switch does not actually power-up or power-down, and Raspberry Pi board consumes some mA of current even after shutdown (i.e. halt state). To control actual power, additional circuit such as this is required.

Tested with

 

Prerequisites
– Raspberry Pi board (e.g. Raspberry Pi Zero W, Raspberry Pi 4B) running Raspberry Pi OS
– Normally-Open Switch such as this
Jumper wires

 

Steps
1. Wiring
1-1. Solder jumper wires with the switch.

1-2. Connect the wires with GPIO pin 5 & 6. Please refer this site for Raspberry Pi’s GPIO pinout.
Momentarily shorting pin 5 and 6 makes Raspberry Pi booting up from halt state [1]. Also, pin 5 will be used for detecting shutdown and reboot signals from the switch.

 

2. Test wiring
2-1. First, supply power to Raspberry Pi and boot it up.

2-2. Then, shut it down. After the activity LED (green LED) goes off, Raspberry Pi is in halt state.

sudo shutdown -h now

2-3. Push the switch to test. Raspberry Pi should boot up. If not, check the continuity of the switch with multimeter.

 

3. Script for shutdown and reboot
3-1. Create a python script file of any name or location.

sudo nano /usr/local/bin/power-switch.py

3-2. Write a code to detect shutdown and reboot signal.

import threading, subprocess
import RPi.GPIO as GPIO

if __name__ == '__main__':
    try:
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(5, GPIO.IN)
        GPIO.wait_for_edge(5, GPIO.RISING)
        pin = GPIO.wait_for_edge(5, GPIO.FALLING, timeout=3000)
        if pin is None:
            subprocess.call('sudo shutdown -h now', shell=True)
        else:
            subprocess.call('sudo reboot', shell=True)
    finally:
        GPIO.cleanup()

Pushing and holding the switch more than 3 seconds makes Raspberry Pi shutdown, and pushing the switch less than 3 seconds makes it reboot.

 

4. Enable script at boot
4-1. Open /etc/rc.local.

sudo nano /etc/rc.local

4-2. Add a line above “exit 0” to execute the script at boot.

python /usr/local/bin/power-switch.py &

4-3. Reboot.

sudo reboot

 

5. Verify
5-1. Verify reboot by pushing the switch less than 3 seconds when Raspberry Pi is running.

5-2. Verify shutdown by pushing and hold the switch more than 3 seconds when Raspberry Pi is running.

5-3. Verify booting up by pushing the switch when Raspberry Pi is in halt state.

 

References
[1] Wake from Halt

[2] rpi-gpio Event detection

[3] How to add a power button to your Raspberry Pi

 

 

Controlling Display Backlight on Raspberry Pi


This is how to turn on and off backlight on Raspberry Pi for the official 7-inch touchscreen display and HDMI displays using command-line interface (CLI).

I checked it with Raspberry Pi 3 B+ running Raspbian Stretch (September 2017 version) + official 7″ display / GeChic 1002 10.1″ HDMI Display.

 

The official 7-inch touchscreen display [1]

OFF:

echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power

ON:

echo 0 | sudo tee /sys/class/backlight/rpi_backlight/bl_power

 

 

HDMI displays [2] [3] [4]

OFF:

vcgencmd display_power 0

ON:

vcgencmd display_power 1

 

 

Troubleshoot

You may get the error below if you are using older software.

-bash: /sys/class/backlight/rpi_backlight/brightness: No such file or directory

In that case, run rpi-update. [5]

sudo rpi-update

 

 

Summary [6]

Since I use both the official 7″ display and the HDMI display, I wrote a script which works for both displays.

Note: It’s working on my environments but I don’t think how it detects HDMI connection (i.e. line 10) is robust enough. You should test with “tvservice -s” command on your environment.

rpi-backlight:

#!/bin/bash

if [ $# -eq 0 ] || [[ $1 != "on"  &&  $1 != "off" ]]; then
  echo "Usage: rpi-backlight [on/off]"
  exit 1
fi

output=$(tvservice -s)

if [[ $output =~ "0x400000 [LCD]" ]]; then

  # Official 7" display
  if [ $1 = "on" ]; then
    echo 0 | sudo tee /sys/class/backlight/rpi_backlight/bl_power
  elif [ $1 = "off" ]; then
    echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power
  fi

else

  # HDMI display
  if [ $1 = "on" ]; then
    vcgencmd display_power 1
  elif [ $1 = "off" ]; then
    vcgencmd display_power 0
  fi

fi

 

 

References
[1] Official 7” Raspberry Pi Touch Screen FAQ

[2] Programmatically turn screen off

[3] Turning ‘tvservice’ on and off leaves screen blank

[4] RPI vcgencmd usage

[5] RPi Touchscreen – How to turn it off?

[6] Detect HDMI and Start GUI

 

 

Silent boot on Raspbian Stretch/Buster

The purpose of the steps here is to remove 1 – 4 below from showing up at Raspberry Pi boot up process. After following the steps, the first thing you see will be Raspbian desktop.

1) Rainbow image
2) Raspberry Pi Logo (image of four raspberries at the top left corner)
3) Blinking cursor (at the top left corner)
4) Splash image

I tested the steps with the following setup:

 

Steps
1. Remove Rainbow Screen
1-1. Open “/boot/config.txt”.

sudo nano /boot/config.txt

1-2. Then add below at the end of the file.

# Disable rainbow image at boot
disable_splash=1

 

2. Disable splash image
2-1. Disable (mask) plymouth services.

sudo systemctl mask plymouth-start.service

 

3. Remove Raspberry Pi logo and blinking cursor
3-1. Open “/boot/cmdline.txt”.

sudo nano /boot/cmdline.txt

3-2. Add below at the end of the line.

logo.nologo vt.global_cursor_default=0

‘logo.nologo’ and ‘vt.global_cursor_default=0’ remove Raspberry Pi logo and blinking cursor, respectively.

 

4. Verify
Reboot the board and see if it works properly.

sudo reboot

 

In case a lightning bolt icon is shown in the right-top corner of the screen, see this post.