Raspberry Pi Tricks
Safe Shutdown
This is material presented in this video:
If you pull the plug on a Raspberry Pi during operation, it is well possible that it will never boot up again, because its SD card is corrupted. This is what we will change today. And I will show you a few other useful tricks you might not find right away.
Only connecting the switch is not enough. We have change the config.txt file using this command:
sudo nano /boot/config.txt
and add the following line to the file:
dtoverlay=gpio-shutdown,gpio_pin=3,active_low=1,gpio_pull=up
ctrl-X and Yes and, after a reboot, it should work.
Increase Swap File
The next trick concerns installations with high memory consumptions, mainly on the Pi Zero. I learned it when I used Node-Red, influxDB, and Grafana on a Pi Zero. After a while, the device blocked and had to be rebooted. Using the reboot switch from before, BTW.
The “top” command revealed the problem: No more Swap space available. This means that Linux does not get enough resources and blocks. Swap space is used by the operating system if the main memory is not big enough. Then, it stores less needed content to the “slow” SD card. Of course, this happens earlier the smaller your RAM is.
The Pi zero only has 512 MB RAM, and the operating system assigns 102MB swap space. The Pi 3 has 1 GB RAM, and the operating system assigns the same amount. For the Pi 3 this is not as critical because it needs much less of this space. What to do?
We enter the command:
sudo nano /etc/dphys-swapfile
and then uncomment the line:
CONF_SWAPFILE=/var/swap and CONF_SWAPFACTOR=2
This creates a swapfile which has 2 times the size of RAM, which seams to be best practice.
And we have to comment the line CONF_SWAPSIZE=100
Ctrl-Z , Y, and enter.
Then we have to invoke these two commands:
sudo dphys-swapfile setup
and
sudo dphys-swapfile swapon
After reboot, we see that the swap file is much bigger and, also after a time, Raspbian will still have free swap space. The Pi zero does not become a racing car with this change. But at least it does not block.
Wi-Fi Setup without Keyboard and display (headless)
We want to connect a “fresh” Raspberry to a Wi-Fi network first time without display and keyboard.
If you create a new SD image and boot your Pi, it cannot connect to your Wi-Fi network because it does not know the credentials and therefore you cannot connect to it to change the SSID or the password. The old “Henn” and “egg” problem.
Here is the trick: after writing the image to the SD card, you can remove and insert it gagain into the reader. You will get an error message. Just ignore it. Now you see this picture . We need to add two files. One is called:
” wpa_supplicant.conf” and the other ssh without an extension.
“wpa_supplicant.conf” has to contain the following lines:
The first is your country, and here you have to enter the Wi-Fi credentials. Save it to the SD card.
country=CH
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid=”…..”
psk=”…..“
}
The file with the name ssh can be empty. It just has to be there. Usually I create a blank text file and remove the extension.
Now we can boot a Raspberry with this SD card, and it will connect to Wi-Fi and enable SSH. As needed. My tip: I store a wpa_supplicant.conf file on my PC which includes my credentials. Then I just have to copy it to the new SD card.
Vincent Lynos
20. May 2019 @ 7:32
That’s a wonderful Youtube channel you got there, it would be so great if you could feature/review some of our stuff in your upcoming vids, is that possible?
Daniel ROBERT
17. June 2019 @ 15:06
Hello, Thank you for theses tricks. Unformtunately, the switch is not working.I was happy to be able to simplify my power ON and OFF procedure. I use the version with scripts and your method is much easier to implement. This is the method I use that works.
———————————————————-
add commande in /etc/rc.local
sudo nano /etc/rc.local
Add:
sudo python /home/pi/PiSupply/softshut.py
avant la ligne exit 0
Create folder ‘PiSupply’ into pi
create file softshut.py
put this into
———
# Import the modules to send commands to the system and access GPIO pins
from subprocess import call
import RPi.GPIO as gpio
# Define a function to keep script running
def loop():
raw_input()
# Define a function to run when an interrupt is called
def shutdown(pin):
call(‘halt’, shell=False)
gpio.setmode(gpio.BOARD) # Set pin numbering to board numbering
gpio.setup(7, gpio.IN) # Set up pin 7 as an input
gpio.add_event_detect(7, gpio.RISING, callback=shutdown, bouncetime=200) # Set up an interrupt to look for button presses
loop() # Run the loop function to keep script running
———–
set right to ‘execute’ to softshut.py
sudo chmod +x softshut.py
——————————————————————————-
If I can implement this method, I should be able to implement yours.
Thank you for your help.
Luciano Paitch
10. July 2019 @ 13:26
Keep going Sir! The best Youtube channel for eletronics, ESPs, Pis and all this stuff.. I´ve learned a lot so far with you..
Kudos and Best Regards!
Daniel ROBERT
11. July 2019 @ 6:09
Hello, I just try with the last Raspbian and a Rpi 3B+ and that work. Thank-you for thé help.
Ian N
16. June 2020 @ 14:57
Enjoy your work and that you share, you have a typo in the Pi 3 RAM
The Pi 3 has 1 MB RAM
Should be 1GB and of course the Pi4 has more, are the GPIO pins the same?
admin
16. June 2020 @ 15:27
Thanks. Corrected.
Pace Bonner
18. June 2021 @ 19:28
I love your channel. Great content.
I built the switch for the Raspberry Pi. I did make one modification. I put some hot glue between the switch and the sockets to give the switch some support.
Max Good
10. August 2021 @ 13:02
When you say it will NOT boot anymore, is that just for gpio21 or do you mean that it only boots with power (disconnected first) connected as usual. or will it start with gpio3 ?
admin
11. August 2021 @ 8:11
It only boots with power on if you use GPIO3. Otherwise you have to disconnect and reconnect power
Max Good
11. August 2021 @ 20:02
Yes so it will work like a normal front panel switch as long as I stay with GPIO3 🙂
Great and thank you.
Mariano
15. April 2023 @ 15:06
HI AndreasSpiess,
I saw your shop window at the link:
https://www.amazon.com/shop/andreasspiess
I live in Italy
you don’t sell in Italy?
could you sell in Italy in the future?
https://www.amazon.it/shop/andreasspiess
I saw your site:
https://www.sensorsiot.org/
https://www.sensorsiot.org/raspberry-pi-tricks/
I wanted to ask you,
in the raspberri pi tab,
especially the Zero W or the Zero 2 W,
which is the best place to disable the wifi-bt,
which processor pin?
the link may be useful:
https://www.jeffgeerling.com/blog/2021/look-inside-raspberry-pi-zero-2-w-and-rp3a0-au
could you make a video on your youtube.com channel
how to disable-enable wifi-bt, with a switch…
hello thanks and Let me know
mariano