How I configured my OpenWRT router with web camera to have real time video feed
Hello everyone and thanks for reading my first post here!
Today I decided I want to finally try connecting webcam to my router to get additional video surveillance possibilities, here are some notes on configuring everything to work. Main purpose is just to remember what i did for future – but if someone will find it useful then even better 🙂
I’m renting flat alone for the first time – before I always had room- or flatmates. Of course having your own apartment has a lot of advantages, but there are also some obvious drawbacks.
My biggest concern as for now was security – when living with someone you can count on them. If I was going out for few or more days – i knew someone was there and inc case something would happen – they would contact me. On one hand – some accidents might happen – even short circuit, broken pipe etc, on the other – i have some photography equipment so I’m afraid of burglary too.
But now that I live alone – what to do? First time I left flat for few hours i just left my PC turned on and was connecting to it through RDP and checking… Camera app 🙂 This is not viable solution for many reasons, mostly – my PC uses quite a lot of power and there is no control, just one, constant field of view.
First thing I did after returning was ordering Xiaomi Mi Home Security Camera. To be honest – this is full package offering everything I really needed – putting it in between kitchen and room allows it to cover most of my flat, it didn’t required literary any network configuration – it is just working through Xiaomi servers without need to connect to my network, it allows to transmit also audio in both ways, most of all – it detects motion and sends puch notification to my phone. If something would happen – I’ll know straight away.
But when for example detecting motion – it can look only in one direction. Of course most possible problems that could happen will be located in the kitchen, where also main entry is located, but still, would be nice to have more coverage.
Also I found another, a bit strange problem – I was going back to my flat and I needed to print some documents to have them ready when I’ll arrive. I quickly connected to my VPN through WireGuard android app, printed what I needed on my network printer, went to Mi Home app to check if it went through… And printer – and most of my IT related, non-PC stuff – happen to be located almost directly under camera, invisible to it. When I finally came home prints of course were there waiting, but still, even in case of emergency – a lot of electronic equipment in one place it potential source of problems – and I had no way to check it remotely.
And here comes my beloved Netgear R6220 with OpenWRT 19. More on it I’ll post on another occasion – but tl;dr version – my w hole home network is built around it and I’m just amazed how much such dirt chip device can do after flashing it with OpeWRT. Right now it works of course as router, switch and access point, but I have on it for example SMB and FTP servers for access to files on small HDD connected to it as NAS, or, most important, VPN WireGuard server to easy and securely access everything on my network without need to open any unnecessary ports. And it just waits for more without breaking a sweat!
This router has just one USB port, but I’ve connected to it small USB hub with 4 ports and external 5V 2A power source, so it can easily handle 2,5″ HDD etc., and serves also as power source for Mi Camera and i was able to connect to it also webcam. After that – it’s time to PuTTy it!
First thing – I’ve check webcam I wanted to use is compatible with USB Video Device driver for Linux – definitely made everything else easier.
Next I had to make sure I have USB drivers installed:
# opkg update # opkg list-installed | grep usb kmod-usb-core - 4.14.171-1 kmod-usb-ehci - 4.14.171-1 kmod-usb-ledtrig-usbport - 4.14.171-1 kmod-usb-storage - 4.14.171-1 kmod-usb2 - 4.14.171-1 kmod-usb3 - 4.14.171-1
In my case I already had needed drivers – kmod-usb-core, kmod-usb-storage and kmod-usb2 from the time i was configuring USD disk. Next step – video drivers: kmod-video-core and kmod-video-uvc
# opkg install kmod-video-core kmod-video-uvc
I double checked camera is visible in the system:
# cd dev dev# ls | grep video video0 # ls -ltrh /dev/video0 crw------- 1 root root 81, 0 Jul 24 23:29 /dev/video0
Ok, looks cool 🙂 First I decided I want basic video streaming capabilities, if it will work then I’ll work on other features like saving video, motion detection etc. For that purpose mjpg-streamer seems perfect. So let’s to it:
# opkg install mjpg-streamer
Then some configuring, I’ve read that on routers performance might be quite bad, but I wanted to first try higher settings and if needed go down. Fir that purpose I’ve set resolution to full HD and fps to 30 – curious what will happen. I left port 8080 as it was unused on this device, and also changed enabled to 1. Rest I left as defaults. Whole configuration (except, of course, for credentials):
option input 'uvc' option output 'http' option device '/dev/video0' option resolution '1920x1080' option yuv '0' option quality '80' option fps '30' option led 'auto' option www '/www/webcam' option port '8080' option username 'X' option password 'X' option enabled '1'
After that I started the service:
# /etc/init.d/mjpg-streamer start
Went to http://wnekowrt:8080, logged in with credentials from config file… And it works. And oh boy – how it works, on Full HD with fps set to 30 the video is visibly smooth!
Unfortunately after checking CPU usage – it was definitely putting a strain on it, as can be seen on bellow graph – spike around midnight is the time I was testing it:
It could be easily lowered with lowering setting – but to be honest why? Right now I’m planing to use it only in situations where I need to check something outside of Xiaomi’s reach, and for future use as surveillance – I’ll be using another package for it. If somebody would like mjpeg-stream to start on boot it can be done with:
As I decided I’ll start it only on-demand I can easily do it with commands:
/etc/init.d/mjpg-streamer start /etc/init.d/mjpg-streamer stop
Maybe it’s not most convenient way, but taking into account it’s just supplementary method to Mi Camera I can live with it for now. To check stream now from my phone (PC would be similar, just different apps) I need to:
- Connect to my VPN through WireGuard to get access to my network,
- Connect to router with mobile SSH client,
- Start camera service,
- Connect to stream using one of possible methods:
- In browser navigate to http://192.168.1.1:8080
- In VLC connect to stream http://192.168.1.1:8080?action=stream
- Stop camera service when not needed.
On my VPN I’m using split tunneling, traffic to local network goes through tunnel to my router, but traffic to internet goes as usual, to not slow it down unnecessary, so I’m also not passing DNS traffic, that’s why I have to use IP addresses, as it wont resolve local names, more on that in VPN post soon.
And that’s all for today! I hope it will be useful for someone. Next I want to get more functionality out of it – but that’s for another day!