Automation/RaspberryPi: Difference between revisions

From Medien Wiki
 
(56 intermediate revisions by the same user not shown)
Line 1: Line 1:
The Raspberry Pi is an inexpensive Linux Computer which you can for example use to play back videos (up to Fill-HD). This page shows you how to set it up:
The Raspberry Pi is an inexpensive Linux computer which you can for example use to play back videos (up to Full-HD). This page shows you how to set it up:


== Making the RapberryPi run a video at startup ==
=== Prepare the system ===
Get a [http://elinux.org/RPi_SD_cards compatible and proven] SD Card (make sure it is not a knock-off). Install The Raspbian image (NOOBS is okay for the beginning, but it wastes a bit space on the SDcard for the recovery image which we don't need. [http://elinux.org/RPi_Easy_SD_Card_Setup Copy the raspbian on the card]. Read about [[#SD Card corruption issues]] to avoid those.
Once that is done, connect to the Internet (Ethernet + DHCP) and bring the OS up to date:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install raspberrypi-net-mods


== Making the RapberryPi run a video at startup ==
Then while we are at it we might want to check if the firmwarware is up to date too. "Firmware" is a bit confusing in this context as the raspberry apparently has no bios sitting on some chip, but all that "Firmware" is part of the stuff on the SD card. ([http://raspberrypiguide.de/howtos/raspberry-pi-firmware-update/ Source])
 
uname -a


Install The Raspbian image (NOOBS is okay for the beginning, but it wastes a bit space on the SDcard for the recovery image which we don't need. here is how to copy the raspbian on the card: [http://elinux.org/RPi_Easy_SD_Card_Setup source]
Will let us see the current version. Then there is a script we can run to update it.


Connect to the internet (Ethernet + DHCP) and bring the OS up to date:
sudo apt-get install git
wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update
sudo rpi-update


  sudo apt-get update
Optional: If you need every little bit of space of the memory card you may want to remove some software and example content to free some memory.
  sudo apt-get upgrade
  sudo apt-get purge --auto-remove sonic-pi scratch squeak-vm idle idle3 timidity wolfram-engine python-pygame penguinspuzzle dillo pistore


After all this we should re-boot
sudo reboot


=== Setup video playback ===
Test video playback: [http://www.raspberrypi.org/documentation/usage/video/README.md source]
Test video playback: [http://www.raspberrypi.org/documentation/usage/video/README.md source]


Line 20: Line 36:
  omxplayer -r /opt/vc/src/hello_pi/hello_video/test.h264
  omxplayer -r /opt/vc/src/hello_pi/hello_video/test.h264


startup_script.sh
make a file startup_script.sh (shell script)


  clear
  touch startup_script.sh
echo "Startup script running"
  nano startup_script.sh
echo "some useful information here"
sleep 2
  omxplayer -r movie.mp4 --loop


with the following content:


<syntaxhighlight lang="bash">
clear
echo "Automatic start up script running"
date '+Date %Y-%m-%d Time %H:%m'
echo -e "This is \c" && hostname
ifconfig | grep "inet addr"
sleep 2
clear
sudo shutdown -h +540 & omxplayer -r movie.mp4 --loop
</syntaxhighlight>


Make it log in automatically:
sudo shutdown -h +540 will shut the Raspberry down in 9 hours.
http://opentechguides.com/how-to/article/raspberry-pi/5/raspberry-pi-auto-start.html


Step 1: Open a terminal session and edit inittab file.
You will have to make the file executable with
 
sudo chmod 775 startup_script.sh
 
and you can test if it works by running it
 
./startup_script.sh
 
=== Make it log in automatically ===
You don't want a password prompt when the raspberry boots [http://opentechguides.com/how-to/article/raspberry-pi/5/raspberry-pi-auto-start.html source]
 
Edit the inittab file.
  sudo nano /etc/inittab
  sudo nano /etc/inittab
Step 2: Disable the getty program.
 
Navigate to the following line in inittab
Disable the getty program.
1:2345:respawn:/sbin/getty 115200 tty1
Find this line and comment it out by adding a # at the beginning of the line
And add a # at the beginning of the line to comment it out
<syntaxhighlight lang="bash">
#1:2345:respawn:/sbin/getty 115200 tty1
#1:2345:respawn:/sbin/getty 115200 tty1
Step 3: Add login program to inittab.
</syntaxhighlight>
 
Add login program to inittab.
Add the following line just below the commented line
Add the following line just below the commented line
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1
<syntaxhighlight lang="bash">
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1
</syntaxhighlight>
This will run the login program with pi user and without any authentication
This will run the login program with pi user and without any authentication
Step 4: Save and Exit.
 
Save and Exit.


Make it start the script automatically:
Make it start the script automatically:
  sudo nano .bashrc
  sudo nano .bashrc
and right at the end put:
and right at the end put:
<syntaxhighlight lang="bash">
  if [ $(tty) == /dev/tty1 ]; then
  if [ $(tty) == /dev/tty1 ]; then
   ./startup_script.sh
   ./startup_script.sh
  fi
  fi
</syntaxhighlight>
(that last 'if' makes sure the script is not starting in an x session)


==Synchronizing multiple RaspberryPis==
==HDMI Output==
By default the RaspberryPi will check if HDMI is attached and switch video and audio output accordingly. This means you need to have the HDMI screen/projector switched on and connected before booting the Pi. Since this can be inconvenient (you may want to switch both on through the same multi-plug for example), you can also change this behaviour in the file /boot/config.txt


maybe you want to have the different pis different hostnames, so you don't get confused
If you always want HDMI output, you would like to uncomment the lines
<syntaxhighlight lang="bash">
hdmi_force_hotplug=1
hdmi_drive=2
</syntaxhighlight>


all of the switches are explained on [http://elinux.org/RPiconfig elinux]
==Synchronizing multiple RaspberryPi==
maybe you want to give the different Pi different host names, so you don't get confused. Do it in the config utility:
  sudo raspi-config
  sudo raspi-config




install pexpect:
install pexpect:
  sudo apt-get install python-pexpect
  sudo apt-get install python-pexpect


 
install [https://github.com/LEW21/pydbus python bindings for DBUS]
install python bindings for DBUS https://github.com/LEW21/pydbus
 
  sudo apt-get install python3-setuptools
  sudo apt-get install python3-setuptools
  sudo wget https://github.com/LEW21/pydbus/archive/master.zip
  sudo wget https://github.com/LEW21/pydbus/archive/master.zip
Line 89: Line 139:
  sudo nano /etc/network/interfaces
  sudo nano /etc/network/interfaces


# start on master
start on master:
  omxplayer-sync -muv synctest.mp4
  omxplayer-sync -mubv synctest.mp4
 
start on slave:
omxplayer-sync -lubv synctest.mp4
 
-b flag to make screen background black. (Remove -v for verbose mode later once everything works)
 
Note: You can use the startup_script above to do this, just replace the omxplayer command with the omxplayer-sync command from here.
 
==Timing==
===Set the time===
dpkg-reconfigure tzdata
 
Note that the Raspberry doesn't have a real-time clock on board. Instead it saves the current time in a file when it shuts down and resumes at the same point when it boots. It is searching online for the current time when booting. If there is no Internet connection the time will almost certainly not be accurate! (Alternatively you could add a real-time clock shield or a GPS receiver which would allow you to get the current time without Internet connection)


# start on slave
===Shut down at specific time===
  omxplayer-sync -luv synctest.mp4
The shutdown command has a timer built in, so when it is called like this
  sudo shutdown -h 15:00
the system will shut down at three in the afternoon.
sudo shutdown -h +30
will shut the system down in 30 minutes from now.


==Notes==
You can also set a cron job for shutting down on specific days at specific times.
sudo crontab -e
add this line:
45 18 * * * /sbin/shutdown -h now
 
to shut down 45mins past 6PM
[https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=59520 source]
 
==SD Card corruption issues==
It's a common problem when working with the Raspberry that the SD Card is getting corrupted and a boot fails. This is caused by not properly shutting down but cutting the power instead. When the raspberry is writing files to the scdard while the power is cut, the risk of SD Card corruption is high.
 
===How to minimize the risk of SD Card corruption===
* Also read: [https://learn.adafruit.com/read-only-raspberry-pi read-only-raspberry-pi]
* Make sure it is a proven, compatible and non-fake SD Card
* A faster card may be more stable (shorter time to write - less times when it is dangerous when a power loss happens
* Never plug the cable, leave Raspberry always on
* Always shut down properly
* Equip Raspberry with a battery pack
* Use a read only file system
* Consider AlpineLinux instead of Raspbian


other ressources:
==Backup and copy==
Use multiple RasberryPis to make a video wall (needs one extra server-Pi, plus one Pi per client) [http://www.piwall.co.uk Piwall]
let's say /dev/mmcblk0 is your card reader


make a backup of SDCard:
make a backup of SDCard:
Line 107: Line 194:


(both these commands take a long time and the terminal seems like frozen, there is no progress feedback)
(both these commands take a long time and the terminal seems like frozen, there is no progress feedback)
==Power==


reboot:
reboot:
Line 113: Line 202:
shutdown:
shutdown:
  sudo shutdown -h now
  sudo shutdown -h now
==Notes==
===other resources===
Use multiple RasberryPis to make a video wall (needs one extra server-Pi, plus one Pi per client) [http://www.piwall.co.uk Piwall]
[[Category:Max Neupert]]
[[Category:Automation]]
[[Category:RaspberryPi]]

Latest revision as of 15:51, 9 July 2019

The Raspberry Pi is an inexpensive Linux computer which you can for example use to play back videos (up to Full-HD). This page shows you how to set it up:

Making the RapberryPi run a video at startup

Prepare the system

Get a compatible and proven SD Card (make sure it is not a knock-off). Install The Raspbian image (NOOBS is okay for the beginning, but it wastes a bit space on the SDcard for the recovery image which we don't need. Copy the raspbian on the card. Read about #SD Card corruption issues to avoid those.

Once that is done, connect to the Internet (Ethernet + DHCP) and bring the OS up to date:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install raspberrypi-net-mods

Then while we are at it we might want to check if the firmwarware is up to date too. "Firmware" is a bit confusing in this context as the raspberry apparently has no bios sitting on some chip, but all that "Firmware" is part of the stuff on the SD card. (Source)

uname -a

Will let us see the current version. Then there is a script we can run to update it.

sudo apt-get install git
wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update
sudo rpi-update

Optional: If you need every little bit of space of the memory card you may want to remove some software and example content to free some memory.

sudo apt-get purge --auto-remove sonic-pi scratch squeak-vm idle idle3 timidity wolfram-engine  python-pygame penguinspuzzle dillo pistore

After all this we should re-boot

sudo reboot

Setup video playback

Test video playback: source

omxplayer /opt/vc/src/hello_pi/hello_video/test.h264

add option -r to change display frame rate and resolution to the one that the movie file has:

omxplayer -r /opt/vc/src/hello_pi/hello_video/test.h264

make a file startup_script.sh (shell script)

touch startup_script.sh
nano startup_script.sh

with the following content:

clear
echo "Automatic start up script running"
date '+Date %Y-%m-%d Time %H:%m'
echo -e "This is \c" && hostname
ifconfig | grep "inet addr"
sleep 2
clear
sudo shutdown -h +540 & omxplayer -r movie.mp4 --loop

sudo shutdown -h +540 will shut the Raspberry down in 9 hours.

You will have to make the file executable with

sudo chmod 775 startup_script.sh

and you can test if it works by running it

./startup_script.sh

Make it log in automatically

You don't want a password prompt when the raspberry boots source

Edit the inittab file.

sudo nano /etc/inittab

Disable the getty program. Find this line and comment it out by adding a # at the beginning of the line

#1:2345:respawn:/sbin/getty 115200 tty1

Add login program to inittab. Add the following line just below the commented line

1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

This will run the login program with pi user and without any authentication

Save and Exit.

Make it start the script automatically:

sudo nano .bashrc

and right at the end put:

 if [ $(tty) == /dev/tty1 ]; then
   ./startup_script.sh
 fi

(that last 'if' makes sure the script is not starting in an x session)

HDMI Output

By default the RaspberryPi will check if HDMI is attached and switch video and audio output accordingly. This means you need to have the HDMI screen/projector switched on and connected before booting the Pi. Since this can be inconvenient (you may want to switch both on through the same multi-plug for example), you can also change this behaviour in the file /boot/config.txt

If you always want HDMI output, you would like to uncomment the lines

 hdmi_force_hotplug=1
 hdmi_drive=2

all of the switches are explained on elinux

Synchronizing multiple RaspberryPi

maybe you want to give the different Pi different host names, so you don't get confused. Do it in the config utility:

sudo raspi-config


install pexpect:

sudo apt-get install python-pexpect

install python bindings for DBUS

sudo apt-get install python3-setuptools
sudo wget https://github.com/LEW21/pydbus/archive/master.zip
sudo unzip master.zip
rm master.zip
cd pydbus-master/
sudo chmod 777 setup.py
sudo ./setup.py build
sudo ./setup.py install


sudo apt-get remove omxplayer
sudo rm -rf /usr/bin/omxplayer /usr/bin/omxplayer.bin /usr/lib/omxplayer
sudo wget -O- http://yokto.net/0/omxplayer/omxplayer-3176db4.tar.bz2 | sudo tar -C / -xjvf -
sudo ln -sf /lib/arm-linux-gnueabihf/libpcre.so.3 /lib/arm-linux-gnueabihf/libpcre.so.1
sudo wget -O /usr/bin/omxplayer-sync https://github.com/turingmachine/omxplayer-sync/raw/master/omxplayer-sync
sudo chmod 0755 /usr/bin/omxplayer-sync
sudo wget https://github.com/turingmachine/omxplayer-sync/raw/master/synctest.mp4


Make a local network, connect them with Ethernet cable (+Hub if more then two) and configure static IPs source

sudo nano /etc/network/interfaces

start on master:

omxplayer-sync -mubv synctest.mp4

start on slave:

omxplayer-sync -lubv synctest.mp4

-b flag to make screen background black. (Remove -v for verbose mode later once everything works)

Note: You can use the startup_script above to do this, just replace the omxplayer command with the omxplayer-sync command from here.

Timing

Set the time

dpkg-reconfigure tzdata

Note that the Raspberry doesn't have a real-time clock on board. Instead it saves the current time in a file when it shuts down and resumes at the same point when it boots. It is searching online for the current time when booting. If there is no Internet connection the time will almost certainly not be accurate! (Alternatively you could add a real-time clock shield or a GPS receiver which would allow you to get the current time without Internet connection)

Shut down at specific time

The shutdown command has a timer built in, so when it is called like this

sudo shutdown -h 15:00

the system will shut down at three in the afternoon.

sudo shutdown -h +30

will shut the system down in 30 minutes from now.

You can also set a cron job for shutting down on specific days at specific times.

sudo crontab -e

add this line:

45 18 * * * /sbin/shutdown -h now

to shut down 45mins past 6PM

source

SD Card corruption issues

It's a common problem when working with the Raspberry that the SD Card is getting corrupted and a boot fails. This is caused by not properly shutting down but cutting the power instead. When the raspberry is writing files to the scdard while the power is cut, the risk of SD Card corruption is high.

How to minimize the risk of SD Card corruption

  • Also read: read-only-raspberry-pi
  • Make sure it is a proven, compatible and non-fake SD Card
  • A faster card may be more stable (shorter time to write - less times when it is dangerous when a power loss happens
  • Never plug the cable, leave Raspberry always on
  • Always shut down properly
  • Equip Raspberry with a battery pack
  • Use a read only file system
  • Consider AlpineLinux instead of Raspbian

Backup and copy

let's say /dev/mmcblk0 is your card reader

make a backup of SDCard:

sudo dd if="/dev/mmcblk0" of="Pi.bin"

put backup on card:

sudo dd bs=4M if=Pi.bin of=/dev/mmcblk0

(both these commands take a long time and the terminal seems like frozen, there is no progress feedback)

Power

reboot:

sudo reboot -h

shutdown:

sudo shutdown -h now

Notes

other resources

Use multiple RasberryPis to make a video wall (needs one extra server-Pi, plus one Pi per client) Piwall