# Raspberry PI

# Start a script as a service

```
sudo nano /etc/systemd/system/ptcam-app.service
```

Create an ini file

```
[Unit]
Description=PanTilt Camera Web App
After=network-online.target
Wants=network-online.target

[Service]
User=sophior
WorkingDirectory=/home/sophior/pt_rpi
ExecStart=/home/sophior/pt_rpi/venv/bin/python -u /home/sophior/pt_rpi/app.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
```

Start

```
sudo systemctl daemon-reload
sudo systemctl enable ptcam-app.service
sudo systemctl start ptcam-app.service
```

Check logs

```
journalctl -u ptcam-app.service -f
```

Stop/restart

```
sudo systemctl restart ptcam-app.service
sudo systemctl restart ptcam-headtracker.service
```

# Enable Filesharing

Install samba

```
sudo apt update
sudo apt install -y samba
```

Set password

```
sudo smbpasswd -a sophior
```

edit config

```
sudo nano /etc/samba/smb.conf

[pt_rpi]
path = /home/sophior/pt_rpi
browseable = yes
read only = no
guest ok = no
valid users = sophior
force user = sophior
create mask = 0664
directory mask = 0775

```

Restart Samba

```
sudo systemctl restart smbd
```

# Fix DNS (apt-get doesn't work)

When apt-get update times out or you can't reach servers that you know are online

```
sudo rm -f /etc/resolv.conf
echo "nameserver 1.1.1.1" | sudo tee /etc/resolv.conf
echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf
ping -c 2 deb.debian.org

```

# Enabled dual camera (Raspberry pi 5)

When you can only see one of both cameras:

rpicam-hello <span class="ͼn">--list-cameras</span>

[https://docs.arducam.com/Raspberry-Pi-Camera/Pivariety-Camera/Libcamera-User-Guide/#introduction <span class="badge badge-notification clicks" title="122 clicks">123</span>](https://docs.arducam.com/Raspberry-Pi-Camera/Pivariety-Camera/Libcamera-User-Guide/#introduction) to do:

<div class="codeblock-button-wrapper" id="bkmrk-"><button aria-label="copy code to clipboard" class="btn nohighlight copy-cmd"><svg class="fa d-icon d-icon-copy svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"></svg></button><button aria-label="show code in full screen" class="btn nohighlight fullscreen-cmd"><svg class="fa d-icon d-icon-discourse-expand svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"></svg></button></div>```bash
install_pivariety_pkgs.sh -p libcamera_dev
install_pivariety_pkgs.sh -p libcamera_apps
install_pivareity_pkgs.sh -p kernel_driver
```

# Access files over HTTP quickly

python3 <span class="ͼn">-m</span> http.server <span class="ͼj">8080</span>

<span class="ͼj">Current folder will now be accessible over http </span>

# Set HTTPS to bypass browser constraints

<span class="ͼl">cd</span> ~/pt\_rpi  
<span class="ͼl">openssl</span> req <span class="ͼn">-x509</span> <span class="ͼn">-newkey</span> rsa:2048 <span class="ͼn">-nodes</span> \\  
<span class="ͼn">-keyout</span> ssl.key \\  
<span class="ͼn">-out</span> ssl.crt \\  
<span class="ͼn">-days</span> <span class="ͼj">365</span> \\  
<span class="ͼn">-subj</span> <span class="ͼk">"/CN=192.168.178.123"</span>

<span class="ͼm">socketio</span><span class="ͼg">.</span>run(  
<span class="ͼm">app</span>,  
<span class="ͼm">host</span><span class="ͼg">=</span><span class="ͼk">'0.0.0.0'</span>,  
<span class="ͼm">port</span><span class="ͼg">=</span><span class="ͼj">5000</span>,  
<span class="ͼm">ssl\_context</span><span class="ͼg">=</span>(<span class="ͼk">'ssl.crt'</span>, <span class="ͼk">'ssl.key'</span>),  
<span class="ͼm">allow\_unsafe\_werkzeug</span><span class="ͼg">=</span><span class="ͼj">True</span>  
)

# stream camera feed

**Broadcast from Raspberry pi:**

rpicam-vid --camera 0 --inline --width 2028 --height 1080 --listen --timeout 0 --profile high --intra 1 -o [tcp://0.0.0.0:8888](tcp://0.0.0.0:8888)

**Receive on PC:**

ffplay -fflags nobuffer -flags low\_delay -probesize 32 tcp://192.168.178.123:8888

# Fixing blurry 8-50mm Zoom Lens for Raspberry Pi High Quality Camera

When you mounted the lense with C-mount, it might be that everything is extremely blurry.  
To adjust it, might feel like a daunting task.  
  
  
The issue is the focal point when you attach the lense too close to the sensor. For this, you have the lock-screw on the C-mount and should not screw in the lense all the way to the end.  
  
Ideally you'd open a video stream and watch it with the near/far focal ring in the middle and the tele/wide ring in the middle.While you adjust the C-mount screw depth.

[![image.png](https://wiki.sophior.com/uploads/images/gallery/2026-05/scaled-1680-/cjwimage.png)](https://wiki.sophior.com/uploads/images/gallery/2026-05/cjwimage.png)

You'd want to reach a point where either focal rings will have impact on sharpness with very tiny mainpulations (1/10th of a mm will be blurry or not blurry).

You could make such a program and then refresh the page after every manipulation to focus the lens.

```
@app.route('/hq_snapshot')
def hq_snapshot():
    path = "/tmp/hq_snapshot.jpg"

    subprocess.run([
        "rpicam-still",
        "--camera", "0",
        "--nopreview",
        "--timeout", "300",
        "--width", "1920",
        "--height", "1080",
        "-o", path
    ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

    return send_file(path, mimetype="image/jpeg")

```

After you're done, lock the lockscrew.  
Now, you should be able to focus with the focal rings, you'd set the tele/wide ring to approximately the object you want to have in focus and adjust the "near/far" until it becomes sharp.

Opeing the shutter (open/close) ring has some effect on sharpness or image bleed. It reduces or increases the amount of light that hits the sensor. In bright shiny days, fully open shutter will create fuzzy overexposed images.