Showing posts with label IoT. Show all posts
Showing posts with label IoT. Show all posts

Tuesday, July 8, 2025

How to Stream RTSP IP Cameras and USB Webcams to the Browser with JSMpeg and WebSockets

Commercial NVRs and cloud-based streaming services often impose high latency, heavy resource overhead, or recurring subscriptions. If you have an IP camera (such as a Hikvision IPC-B120 with an RTSP stream) or a USB webcam (such as a Logitech BRIO), you can stream live video directly to any modern browser with low latency and zero plugins.

Architecture Overview

This lightweight pipeline converts video on-demand using three core components:

  • FFmpeg: Pulls the RTSP feed or V4L2 webcam stream, encodes it as an MPEG-TS container with MPEG-1 video (mpeg1video) and MP2 audio, and pipes stdout to websocat.
  • websocat: Listens as an on-demand WebSocket server. When a browser client connects, websocat launches FFmpeg and streams the video chunks over WebSocket. When the browser tab closes, websocat terminates FFmpeg automatically—consuming 0% CPU when idle!
  • JSMpeg: A tiny JavaScript decoder that renders MPEG-1 video frames directly onto an HTML5 <canvas> element in the browser.

1. RTSP Camera Service (/etc/systemd/system/rtsp-ws.service)

Create a systemd service for your Hikvision or ONVIF RTSP camera:

[Unit]
Description=On-demand WebSocket->FFmpeg bridge for JSMpeg RTSP
After=network.target

[Service]
User=pi
Group=pi
ExecStart=/usr/local/bin/websocat \
  --binary \
  --exit-on-eof \
  -s 127.0.0.1:10000 \
  sh-c:'/usr/bin/ffmpeg -hide_banner -loglevel error -rtsp_transport tcp -i "rtsp://user:pass@192.168.1.100:554/Streaming/Channels/101" -f mpegts -codec:v mpeg1video -bf 0 -r 25 pipe:1'
Restart=on-failure

[Install]
WantedBy=multi-user.target

2. USB Webcam Service (/etc/systemd/system/webcam-ws.service)

Create a systemd service for a local V4L2 USB camera (e.g. Logitech BRIO) with ALSA microphone audio:

[Unit]
Description=On-demand WebSocket->FFmpeg bridge for Logitech Brio
After=network.target

[Service]
User=pi
ExecStart=/usr/local/bin/websocat \
  --binary \
  --exit-on-eof \
  -s 127.0.0.1:10001 \
  sh-c:'WAYLAND_DISPLAY=wayland-0 XDG_RUNTIME_DIR=/run/user/1000 /usr/bin/ffmpeg -hide_banner -loglevel error \
    -f v4l2 -framerate 30 -video_size 1920x1080 -i /dev/video0 \
    -f alsa -ac 2 -ar 44100 -i default \
    -f mpegts -codec:v mpeg1video -bf 0 -r 30 -codec:a mp2 pipe:1'
Restart=on-failure

[Install]
WantedBy=multi-user.target

Enable and start the services:

sudo systemctl daemon-reload
sudo systemctl enable --now rtsp-ws webcam-ws

3. Frontend HTML5 Player (player.html)

Serve this minimalist HTML5 page with JSMpeg to view the stream from any desktop or mobile browser:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Live Camera Stream (JSMpeg)</title>
  <style>
    html, body {
      margin: 0; padding: 0;
      width: 100vw; height: 100vh;
      background: #000;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .player-container {
      width: 90vw;
      max-width: 1280px;
      max-height: 90vh;
    }
    .player-container canvas {
      width: 100%;
      height: auto;
      display: block;
    }
  </style>
</head>
<body>
  <div class="player-container">
    <canvas id="videoCanvas"></canvas>
  </div>

  <script src="/jsmpeg.min.js"></script>
  <script>
    const protocol = location.protocol === 'https:' ? 'wss://' : 'ws://';
    const url = protocol + location.host + '/my-ws-prefix';

    new JSMpeg.Player(url, {
      canvas: document.getElementById('videoCanvas'),
      autoplay: true,
      audio: false
    });
  </script>
</body>
</html>

Monday, February 7, 2022

How to Set Up a Samba Network Storage Drive on Raspberry Pi for Hikvision IP Cameras

Hikvision value IP cameras (such as the IPC-B120, IPC-B121H, and IPC-B120-D-W) feature onboard motion detection, infrared night vision, and H.265+ compression, but lack internal hard drives. Rather than purchasing an expensive commercial NVR, you can attach an external USB hard disk to a Raspberry Pi configured as an SMB/CIFS Network Attached Storage (NAS) target for continuous or motion-triggered video archiving.

Stack: Raspberry Pi 4 • External USB HDD (ext4) • Samba Server (SMBv2/v3) • Hikvision Storage Management

1. Installing and Configuring Samba on the Raspberry Pi

sudo apt-get update
sudo apt-get install samba samba-common-bin -y

# Create recording directory on mounted external drive
sudo mkdir -p /media/cctv_storage/hikvision
sudo chown -R nobody:nogroup /media/cctv_storage/hikvision
sudo chmod -R 0777 /media/cctv_storage/hikvision

2. Samba Share Definition (/etc/samba/smb.conf)

Hikvision firmware requires specific Samba dialect and locking settings to format network drives successfully:

[hikvision]
   comment = Hikvision CCTV Storage
   path = /media/cctv_storage/hikvision
   browseable = yes
   writeable = yes
   guest ok = yes
   read only = no
   create mask = 0777
   directory mask = 0777
   force user = nobody

Restart Samba:

sudo systemctl restart smbd

3. Mounting the Drive in Hikvision Web GUI

  1. Log into your camera's web interface → Configuration → Storage → Storage Management → Net HDD.
  2. Set Type to NAS / SMB/CIFS, enter the Raspberry Pi's IP address, set File Path to /hikvision, and save.
  3. Under HDD Management, select the newly added network disk and click Format / Init. Once status changes to Normal, motion recordings will save directly to your Pi.

Sunday, December 13, 2020

Connecting USB GSM / 3G / 4G Dongles to Raspberry Pi 4 (Huawei E303, usb-modeswitch, and Gammu)

USB cellular modems (such as the ubiquitous Huawei E303, E173, and E3372) enable Raspberry Pi 4 projects to send SMS notifications, handle emergency alerts, and provide cellular backup connectivity. However, modern USB modems initially boot into a virtual CD-ROM storage mode (ZeroCD) to deliver Windows drivers, requiring usb-modeswitch to toggle them into serial modem mode.

Hardware: Raspberry Pi 4 • Huawei E303 USB Dongle • usb-modeswitch • Gammu SMS Gateway

1. Installing usb-modeswitch and Gammu

sudo apt-get update
sudo apt-get install usb-modeswitch usb-modeswitch-data gammu minicom -y

2. Switching from Mass Storage to Modem Mode

Check lsusb to identify your device vendor and product ID:

# Example: ID 12d1:1f01 (Mass Storage Mode)
lsusb | grep Huawei

# Mode-switch into serial modem mode (12d1:1001 or 12d1:14db):
sudo usb_modeswitch -v 12d1 -p 1f01 -M '55534243123456780000000000000011062000000100000000000000000000'

Confirm the modem serial nodes are created under /dev/ttyUSB0, /dev/ttyUSB1, and /dev/ttyUSB2.

3. Sending a Test SMS via Gammu

Configure ~/.gammurc:

[gammu]
port = /dev/ttyUSB0
connection = at115200

Send an automated test message:

echo "Alert: Raspberry Pi online!" | gammu sendsms TEXT +1234567890

How Google Antigravity Solved the Mysterious NVIDIA Sleep Reboot on My Dell Inspiron 7567 (Ubuntu Linux)

If you run modern Ubuntu or Linux on a Dell Inspiron 15 Gaming (7567) or a similar 7th-gen Intel laptop paired with an NVIDIA GeForce GTX ...