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:
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
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.
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.