Skip to main content

Setup USB Camera to work in Octoprint

This passage summerize the website "Orange Pi Zero and Webcam for Octoprint", https://www.iot-experiments.com/orange-pi-zero-and-webcam-for-octoprint/, aimed to make my life easier.

Check the USB webcam present,
octoprint@orangepizero:~$ lsusb


Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 002: ID 046d:0826 Logitech, Inc. HD Webcam C525
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 007 Device 002: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub


To discover webcam information,
(octoprint@orangepizero:~$sudo apt-get install hwinfo)
octoprint@orangepizero:~$ hwinfo --usb

...
09: USB 00.2: 0000 Unclassified device
  [Created at usb.122]
  Unique ID: O0wn.n7GFn22urBA
  Parent ID: 2XnU.CpYmifTkid6
  SysFS ID: /devices/platform/soc/1c1d000.usb/usb5/5-1/5-1:1.2
  SysFS BusID: 5-1:1.2
  Hardware Class: unknown
  Model: "Logitech HD Webcam C525"
  Hotplug: USB
  Vendor: usb 0x046d "Logitech, Inc."
  Device: usb 0x0826 "HD Webcam C525"
  Revision: "0.10"
  Serial ID: "CEB1D9D0"
  Driver: "uvcvideo"
  Driver Modules: "uvcvideo"
  Device File: /dev/input/event0
  Device Files: /dev/input/event0, /dev/input/by-id/usb-046d_HD_Webcam_C525_CEB1D9D0-event-if02, /dev/input/by-path/platform-1c1d000.usb-usb-0:1:1.2-event
  Device Number: char 13:64
  Speed: 480 Mbps
  Module Alias: "usb:v046Dp0826d0010dcEFdsc02dp01ic0Eisc01ip00in02"
  Driver Info #0:
    Driver Status: uvcvideo is active
    Driver Activation Cmd: "modprobe uvcvideo"
  Config Status: cfg=new, avail=yes, need=no, active=unknown
  Attached to: #20 (Hub)
...



 

Find out capability of the cam,
octoprint@orangepizero:~$ sudo apt-get install v4l-utils

octoprint@orangepizero:~$ sudo v4l2-ctl --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
        Type: Video Capture

        [0]: 'YUYV' (YUYV 4:2:2)
                Size: Discrete 640x480
                        Interval: Discrete 0.033s (30.000 fps)
                        Interval: Discrete 0.042s (24.000 fps)
                        Interval: Discrete 0.050s (20.000 fps)
                        Interval: Discrete 0.067s (15.000 fps)
                        Interval: Discrete 0.100s (10.000 fps)
                        Interval: Discrete 0.133s (7.500 fps)
                        Interval: Discrete 0.200s (5.000 fps)
                Size: Discrete 160x120
                        Interval: Discrete 0.033s (30.000 fps)
                        Interval: Discrete 0.042s (24.000 fps)
                        Interval: Discrete 0.050s (20.000 fps)
                        Interval: Discrete 0.067s (15.000 fps)
                        Interval: Discrete 0.100s (10.000 fps)
                        Interval: Discrete 0.133s (7.500 fps)
                        Interval: Discrete 0.200s (5.000 fps)
...

Install MPEG streamer
octoprint@orangepizero:~$ apt-get install cmake libjpeg8-dev
octoprint@orangepizero:~$ git clone https://github.com/jacksonliam/mjpg-streamer.git
octoprint@orangepizero:~$ cd mjpg-streamer/mjpg-streamer-experimental
octoprint@orangepizero:~/OctoPrint/mjpg-streamer/mjpg-streamer-experimental$ make
octoprint@orangepizero:~/OctoPrint/mjpg-streamer/mjpg-streamer-experimental$ make install


 

Test MPEG streamer,
octoprint@orangepizero:~$ sudo mjpg_streamer -i "input_uvc.so -r 1280x720 -f 30" -o "output_http.so"
MJPG Streamer Version: git rev: 501f6362c5afddcfb41055f97ae484252c85c912
 i: Using V4L2 device.: /dev/video0
 i: Desired Resolution: 1280 x 720
 i: Frames Per Second.: 30
 i: Format............: JPEG
 i: TV-Norm...........: DEFAULT
UVCIOC_CTRL_ADD - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Mode: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Frequency: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Disable video processing: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Raw bits per pixel: Inappropriate ioctl for device (25)
 o: www-folder-path......: disabled
 o: HTTP TCP port........: 8080
 o: HTTP Listen Address..: (null)
 o: username:password....: disabled
 o: commands.............: enabled
setting signal to stop
 i: cleaning up resources allocated by input thread
force cancellation of threads and cleanup resources
 o: cleaning up resources allocated by server thread #00
done
 

The stream is now available at,
http://192.168.68.112:8080/?action=stream
change 192.168.68.112 to the device IP address.

 

Octoprint setup


Before configuring Octoprint, install ffmpeg:
octoprint@orangepizero:~$ sudo apt-get install ffmpeg
 

Octoprint start/stop scripts
octoprint@orangepizero:~$ mkdir scripts
octoprint@orangepizero:~$ cd scripts

 

Content of scripts/webcam.sh

#!/bin/bash
# Start / stop streamer daemon

case "$1" in
    start)
        /home/octoprint/scripts/webcamDaemon.sh >/dev/null 2>&1 &
        echo "$0: started"
        ;;
    stop)
        pkill -x webcamDaemon
        pkill -x mjpg_streamer
        echo "$0: stopped"
        ;;
    *)
        echo "Usage: $0 {start|stop}" >&2
        ;;
esac

 

Content of scripts/webcamDaemon.sh 
#!/bin/bash

MJPGSTREAMER_HOME=/home/octoprint/mjpg-streamer/mjpg-streamer-experimental
MJPGSTREAMER_INPUT_USB="input_uvc.so"

# init configuration
camera="auto"
camera_usb_options="-r 640x480 -f 30"

if [ -e "/boot/octopi.txt" ]; then
    source "/boot/octopi.txt"
fi

# runs MJPG Streamer, using the provided input plugin + configuration
function runMjpgStreamer {
    input=$1
    pushd $MJPGSTREAMER_HOME
    echo Running ./mjpg_streamer -o "output_http.so -w ./www" -i "$input"
    LD_LIBRARY_PATH=. ./mjpg_streamer -o "output_http.so -w ./www" -i "$input"
    popd
}


# starts up the USB webcam
function startUsb {
    logger "Starting USB webcam"
    runMjpgStreamer "$MJPGSTREAMER_INPUT_USB $camera_usb_options"
}

# echo configuration
echo camera: $camera
echo usb options: $camera_usb_options

# keep mjpg streamer running if some camera is attached
while true; do
    if [ -e "/dev/video0" ] && { [ "$camera" = "auto" ] || [ "$camera" = "usb" ] ; }; then
        startUsb
    elif [ "`vcgencmd get_camera`" = "supported=1 detected=1" ] && { [ "$camera" = "auto" ] || [ "$camera" = "raspi" ] ; }; then
        logger "Not supported"
    fi

    sleep 120
done


 

Make scripts executable,
 
octoprint@orangepizero:~/scripts$ chmod a+x webcam.sh
octoprint@orangepizero:~/scripts$ chmod a+x webcamDaemon.sh



 

Edit Octoprint configuration
octoprint@orangepizero:~/scripts$ nano ~/.octoprint/config.yaml



 

Append to the end of config.yaml 
system:
  actions:
  - action: streamon
    command: /home/octoprint/scripts/webcam.sh start
    confirm: false
    name: Start video stream
  - action: streamoff
    command: /home/octoprint/scripts/webcam.sh stop
    confirm: false
    name: Stop video stream
    


Add user octoprint to video group


octoprint@orangepizero:~$ sudo usermod -aG video octoprint
 

Restart Octoprint

octoprint@orangepizero:~$ sudo service octoprint restart

Comments

Post a Comment

Popular posts from this blog

Install/Setup Xfce and TightVNC to Armbian OrangePi

Following https://www.instructables.com/id/The-OrangePi-Lite/ , the next instruction after network started is usually VNC server setup. Many Internet resources suggested TightVNC, an open source software for this purpose. However, Armbian distribution for OrangePi does not include any X window desktop, therefore install TightVNC itself is just half of the journey. This is how I complete the work. 1. Make sure Armbian is updated: sudo apt-get update sudo apt-get upgrade 2. Install XFont used by TightVNC server sudo apt-get install xfonts-base 3. Install TightVNC server sudo apt-get install tightvncserver 4. Create a user dedicate for this service. I use user ID 'vnc' sudo adduser vnc gpasswd -a vnc sudo 5. Setup this user for VNC server, vncserver :1 We need to assign a password. No need to specify a view-only password. Use the command to confirm the server is running and the default options applied ps -ef | grep Xtightvnc 6. VNC Client can now conn

Configure OrangePi Lite WiFi

There are many websites and Youtube videos show you how to install Armbian to OrangePi with a SD-card. There are many ways to connect to the initial console of the device. Most of them tell you to utilize the LAN interface. This is a quick and easy method, however, my OrangePi Lite simply lacks a LAN interface. We need to setup Wifi before we can use it for console connection. But without the console how can we setup Wifi connection? Finally I used a USB to TTL device. With only 3 wires I connected the device to OrangePi: Rx-Tx, Tx-Rx, and GND-GND. Then I can use PuTTY to bring up the console thru SSH. Up to this point basically I am following this website: https://www.instructables.com/id/The-OrangePi-Lite/ . I continued with the instructions and successfully connect to WiFi, but puzzle why I have do that with so many manual editings. Especially when I edit the /etc/network/interface file, it has a remark say "# Network is managed by Network manage". My question is can N