UP | HOME
Ravi Sagar | Home | Blog

Setup Raspberry Pi

Table of Contents

1 Read the instructions

2 Download the image

Find the compatible image from this page https://www.raspberrypi.com/software/operating-systems/

3 Extract the image

unxz 2022-09-06-raspios-bullseye-arm64-lite.img.xz

Insert your sd card and find its name. Usually it is /dev/mmcblk0.

sudo dd status=progress if=2022-09-06-raspios-bullseye-arm64-lite.img of=/dev/mmcblk0 bs=4M conv=fsync

Mount the boot drive.

sudo mount -o umask=0022,gid=1000,uid=1000 /dev/mmcblk0p1 /srv/public/sdcard1_16gb/

Configure the username. Create a file in /boot directory.

touch /srv/public/sdcard1_16gb/userconf

This file should contain a single line of text, consisting of username:encrypted-password.

To generate password use openssl.

echo 'password' | openssl passwd -6 -stdin

To do it all in one command use this.

HASHEDPASSWORD=$(echo 'password' | openssl passwd -6 -stdin); echo "username":$HASHEDPASSWORD >> /srv/public/sdcard1_16gb/userconf

Create a blank ssh file in the mounted /boot/ directory.

touch /srv/public/sdcard1_16gb/ssh

This file will be deleted after first boot. Refer to this page: https://www.raspberrypi.com/documentation/computers/remote-access.html#enabling-the-server

Setup WiFi in the mounted /boot directory.

touch /srv/public/sdcard1_16gb/wpa_supplicant.conf

Copy paste the following with your WiFi SSID and Password.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
        ssid="wifi_name"
        psk="wifi_password"
}

This file will be moved to /etc/wpa_supplicant.conf after first boot.

4 wlan0 not found

If after fresh install you cannot find wlan0 then there is a problem with the wifi but you can use a wifi dongle. Check your wlan0 using this command. If it is missing then use a dongle.

ip -br link

5 Further configurations

You can use this utility to make further changes.

sudo raspi-config

6 Upgrade your os

sudo apt-get update
sudo apt upgrade

7 nginx with ffmpeg setup

7.1 Install nginx with rtmp module

sudo apt-get install libnginx-mod-rtmp

nginx will be started after the installation automatically.

7.2 Confire nginx

Modify the content of this file.

sudo nano /etc/nginx/nginx.conf

Add the following.

rtmp {
        server {
                listen 1935;
                chunk_size 4096;
                allow publish 127.0.0.1;
                deny publish all;

                application live {
                        live on;
                        record off;
                }
        }
}

Restart nginx.

sudo systemctl restart nginx

You can check with nginx is listening on port.

netstat -an | grep 1935

8 ffmpeg

Stream to rtmp server which you just setup above.

 ffmpeg -f v4l2 -input_format yuyv422 -video_size 640x360 -framerate 24 -i /dev/video0 -vcodec libx264 -preset ultrafast -r 24 -g 48 -b:v 1500k
-bufsize 3000k -maxrate 1500k -f flv rtmp://127.0.0.1/live/stream

The above script works great.

You can play this using ffplay.

ffplay rtmp://raspberrypi2/live/stream

You can also use the ip address if hostname is setup.

9 Check devices and formats

Check connected and available cameras.

v4l2-ctl --list-devices

Check available formats and resolutions of a specific camera.

ffmpeg -f v4l2 -list_formats all -i /dev/video0

10 Streaming to HLS format instead

ffmpeg -f v4l2 -input_format yuyv422 -video_size 640x360 -framerate 24 -i /dev/video2 -vcodec libx264 -preset ultrafast -r 24 -g 48 -b:v 1500k -bufsize 3000k -maxrate 1500k -f hls -hls_flags delete_segments /var/www/html/stream.m3u8

I realised that ffplay can stream to rtmp but you can't play rtmp directly from a browser but HLS format can be played from a browser using a JavaScript based player.

So instead of rtmp the above command will write to stream.m3u8 file which is nothing but an index of various stream1.ts, stream2.ts, etc segment files. The -hls_flags delete_segments will keep only recent segment files and delete the old ones.

The location of the stream.m3u8 could be anywhere where your webserver, nginx in this case can read.

So eventually you should be able to open http://raspberrypi2/stream.m3u8 from anywhere on your network.

11 How to play the HLS stream from a browser?

Create an HTML file with the following content.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Your title</title>


  <link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet">
  <script src="https://unpkg.com/video.js/dist/video.js"></script>
  <script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script>

</head>
<body>
  <video id="my_video_1" class="video-js vjs-fluid vjs-default-skin" controls preload="auto"
  data-setup='{}'>
    <source src="http://raspberrypi2/stream.m3u8" type="application/x-mpegURL">
  </video>

<script>
var player = videojs('my_video_1');
player.play();
</script>

</body>
</html>

Source of this file: https://youtu.be/rgWVm_j3llo

You can also play http://raspberrypi2/stream.m3u8 directly using ffplay.

12 Install Syncthing command line

First make sure you perform update.

I followed this tutorial: https://pimylifeup.com/raspberry-pi-syncthing/

First install Syncthing.

sudo apt install apt-transport-https

curl -s https://syncthing.net/release-key.txt | gpg --dearmor | sudo tee /usr/share/keyrings/syncthing-archive-keyring.gpg >/dev/null

echo "deb [signed-by=/usr/share/keyrings/syncthing-archive-keyring.gpg] https://apt.syncthing.net/ syncthing stable" | sudo tee /etc/apt/sources.list.d/syncthing.list

sudo apt update
sudo apt install syncthing

Run Syncthing to create the configuration files.

syncthing

Press Ctrl + c to terminate the command.

Now open the configuration file.

nano ~/.config/syncthing/config.xml

Find this line: <address>127.0.0.1:8384</address> Replace by: <address>0.0.0.0:8384</address>

Now start syncthing again by running syncthing again and you should now be able to access the Syncthing's web interface using http://raspberrypi:8384

Now create Syncthing service.

sudo nano /lib/systemd/system/syncthing.service

Use the following content for syncthing.service file.

[Unit]
Description=Syncthing - Open Source Continuous File Synchronization
Documentation=man:syncthing(1)
After=network.target

[Service]
User=pi
ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0
Restart=on-failure
RestartSec=5
SuccessExitStatus=3 4
RestartForceExitStatus=3 4

# Hardening
ProtectSystem=full
PrivateTmp=true
SystemCallArchitectures=native
MemoryDenyWriteExecute=true
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

Enable the service and the start or check the status.

sudo systemctl enable syncthing@USERNAME
sudo systemctl enable start@USERNAME
sudo systemctl enable status@USERNAME

Make sure you specify your username otherwise you might get the following error.

"syncthing.service: Failed to determine user credentials: No such process"