Sunday, September 2, 2012

Making an IP webcam using an old router



Recently I got my hands on an old Comtrend CT536+ router. It wasn't working. When turned on, some times it worked for a while, but most of the times it just didn't even boot. I decided to try fixing it, so I opened the unit, had a look at its internals and found two capacitors in the power source with a slight bulging. These capacitors could be victims of the annoying capacitor plague, so I replaced them with new low ESR ones and... the router started working!

One of the damaged capacitors next to the two new ones

Installing a USB Host port

Since I took the trouble to open the router, I decided to add some enhancements. The components for the USB Host were not populated in the PCB, so I added a USB Host connector, a capacitor, some SMD resistors and some wiring to get the extremely useful USB Host port. I also added some wires to connect a 3.3V UART, just in case I need to access the GNU+Linux console.

USB mod

Finally I used a file to enlarge the hole for the Ethernet ports in the back of the router, to make room for the new connector. This is how the back of the router looks like now:

The new USB Host connector, ready to be tested

WARNING: Before you plug anything to the new USB Host, you must have in mind the 5V for the host are provided by a 78L05 linear regulator. This regulator was already populated inside the router (I don't know why, because it's only used for the USB Host) and can only supply 100 mA. Some USB devices can draw up to 500 mA, so you have to be careful. Only plug devices drawing less than 100 mA. If you need to plug something needing more juice, use a self-powered USB Hub to plug it, or remove the 78L05 regulator and add a bigger one.

Router internals, including the USB mod

Installing OpenWrt and USB Host driver

Unfortunately, the USB Host port is useless with the firmware that comes with the router. The solution to this problem is installing OpenWrt, a GNU+Linux distro suited for routers. Installation process for this router is dead easy, because it can be done directly using the Firmware Update function in the router configuration web page. Just download the OpenWrt image for this router and point it to the Firmware Update page.

After installing OpenWrt, it's recommended to configure the network. There are tons of ways of doing it, depending on your setup. Just have a look to the OpenWrt Wiki. You can also ask in the forum.

Once you have network working, to get the USB Host working, you have to install the host controller kernel module. The host controller in my router is an OHCI one. Other routers may have a different host controller (UHCI or EHCI). To install the OHCI host controller driver, access the router using SSH and use OPKG package manager:
opkg update
opkg install kmod-usb-ohci
The driver should be automatically loaded by the kernel, but if it is not, you can load it entering the command:
insmod usb-ohci
You can plug some USB stuff and have a look to the kernel messages (dmesg) to see if it's working. If you need to use the useful lsusb command, you can install usbutils package, but don't do it unless needed, because these routers usually have an extremely reduced storage (CT536+ only has 4 MB), so installing stuff can easily fill it.

Installing the webcam drivers

Now we can try getting the webcam working. To stream the video I use mjpg-streamer. But before using it, we have to configure the webcam. Supported webcams can be sorted in two families:

  1. The ones that have UVC support.
  2. The ones that have SPCA support.

I have tried two webcams. The first is this one I bought at DealExtreme. lsusb command lists it as Aveo Technology Corp (1871:0142). It supports UVC profile. The second one is a Logitech QuickCam for Notebooks (046d:08dd). It's supported by SPCA.

To get UVC webcams working, just install kmod-video-uvc package:
opkg install kmod-video-uvc
To get SPCA webcams working, you have to find its driver first. Search it inside this list. For example, for my Logitech webcam, the driver is zc3xx, so I have to install kmod-video-gspca-core + kmod-video-gspca-zc3xx:
opkg install kmod-video-gspca-core kmod-video-gspca-zc3xx

mjpg-streamer

Once you have the drivers for your webcam installed, plug it in the USB port and have a look to the kernel messages (dmesg) to see if it's properly recognized. If everything is OK, install mjpg-streamer:
opkg install mjpg-streamer
You can configure some mjpg-streamer parameters by editing /etc/config/mjpg-streamer. To start mjpg-streamer daemon, use the comand:
/etc/init.d/mjpg-streamer start
You can also enable it so it will be automatically started each boot, using the command:
/etc/init.d/mjpg-streamer enable
Once it's running, to watch the video captured by the webcam and streamed through mjpg-streamer, launch for example VLC media player, and select Media/Open Network Stream... (Ctrl+N). Then in the dialog enter the URL: http://<ip_address_of_the_router>:8080/?action=stream and click the Play button. Of course replace <ip_address_of_the_router> with the address (or dns name) matching your router. You should be able to see the video stream. Congratulations! You can also input that URL in any browser and it should work. It worked for me in Firefox 15 and Opera, but Chromium 21 displayed only a still image. The router can be configured as a wireless/wired IP webcam, and it retains its old functionality, so it still can be used as a standard router!
Opening network streams using VLC

You might wonder which driver is better: SPCA or UVC. These are my findings:
  • Using SPCA with the Logitech webcam, I got a very good framerate at 640x480 resolution. As this camera supported the MJPEG profile, mjpg-streamer only has to encapsulate the frames captured by the camera and send them. With this camera, router CPU usage is very low (around 5%).
  • Using UVC with the Aveo webcam I could only get 320x240 resolution and a very poor framerate (around 1 fps, I think). This camera only supports YUYV profile, so mjpg-streamer has to transcode the image captured by the camera. Even with the poor resolution and framerate, router CPU usage with this webcam is higher (around 15-20% with default compression quality). I wasn't able to make this webcam work with higher resolution/framerate.
Maybe the driver used (either SPCA or UVC) doesn't matter, but to avoid image transcoding, I suppose cameras supporting the MJPEG profile are preferred over the ones not supporting it.

Troubleshooting:

If your webcam supports only the YUYV profile (like my Aveo webcam), you will have to explicitly tell mjpg-streamer to use this profile, or it will not start. Edit /etc/init.d/mjpg-streamer and add the -y switch to the --input section. The line in which mjpg-streamer is called (the last line in the start{} section) should look like:
    [ $enabled -gt 0 -a -c $device ] && sleep 3 && $SSD -S -m -p $PIDF -q -x $PROG -- --input "input_uvc.so -y --device $device --fps $fps --resolution $resolution" --output "output_http.so --port $port" &
Note the -y switch after input_uvc.so, that enables YUYV mode.

18 comments:

  1. Replies
    1. Maybe. The router must meet two requirements:
      1.- It must have a USB Host port, to plug the webcam in.
      2.- It must be supported by OpenWrt. Have a look here: http://wiki.openwrt.org/toh/start

      Even if you can't find a USB Host connector in the case of the router, there's the possibility it has an internal USB Host, like the Comtrend router I have used. You'll have to investigate about your particular router.

      Delete
  2. When I try to install kmod-video-uvc I recive an error:

    Installing kmod-video-uvc (2.6.32.10-1) to root...
    Downloading http://downloads.openwrt.org/backfire/10.03/brcm63xx/packages/kmod-video-uvc_2.6.32.10-1_brcm63xx.ipk.
    Not downgrading package kernel on root from 2.6.32.27-1 to 2.6.32.10-1.
    Collected errors:
    * opkg_install_cmd: Cannot install package kmod-video-uvc.

    Am I using a wrong version of openwrt? I recently updated it

    ReplyDelete
    Replies
    1. It looks like the kernel module version doesn't match your kernel. Maybe you will have to manually (and carefully) downgrade your kernel to version 2.6.32.10, but before doing it, I would try asking in OpenWrt forums. Maybe someone can point you to modules built for your kernel.

      Delete
  3. Can this be done without the use of a USB Host? For instance could I use a converter (USB Extender) to convert the USB o/p to a cat6 o/p, plug it into the router and then configure the Webcam drivers using OpenWrt?

    ReplyDelete
    Replies
    1. I don't get you. Do you want to connect a USB to Ethernet adapter to the router, and then connect the webcam using the ethernet interface? Why would you do that?

      Delete
    2. This comment has been removed by the author.

      Delete
    3. well..Simply put, could we forego the installation of the USB host port? Instead could we use a cat6 cable as an input to the router, like webcam->converter->cat6 ->router. I'm not an electronics guy, installing the port seems beyond me, so was just wondering if it was possible this way.

      Delete
    4. When you write cat6, I suppose you mean Ethernet (I don't know other interfaces using CAT6 cable). If using some kind of converter you make your webcam have an Ethernet interface, then you don't need to connect it to the router (unless you want it to be accessed via WiFi), as it can already be connected to any network you want.

      Anyway, AFAIK there are no USB Host to Ethernet converters (there are USB Device to Ethernet, but you need a USB Host in this case). You could make one by using something like a RaspberryPi, but I suppose that's not what you want.

      Delete
    5. I was referring to something like this
      (http://www.amazon.com/SANOXY%C2%AE-over-Extension-Cable-Adapter/dp/B003BDMK3S/ref=sr_1_3?s=electronics&ie=UTF8&qid=1414124377&sr=1-3&keywords=ethernet+cable+usb+cable)

      And yes - wifi was what I had in mind, that way it becomes like a cctv that I can access from anywhere on the network(even when the PC is switched off) and also because the door(where I want to place my camera) is too far from my PC, so a wired connection isn't feasible.

      Delete
    6. Even though that uses CAT6 cable, it is still USB (uses USB protocol and signals), so it needs the USB Host in the router to be able to connect to the webcam.

      Delete
    7. yeah I feared that might be the case. Thanks for your time though. Yours was the most extensive article by far and was a rare find for me, really appreciate it. I never thought I'd find a way to embed webcam drivers into the router, that was pure gold!

      let me paint the whole picture for you. just in case you have a solution I might have overlooked.

      WHAT I HAVE : An old TP-Link router, a philips webcam, 100 feet of ethernet cable, and those converters I mentioned earlier.

      WHAT I WANTED TO DO: To place the camera at the door so it became like an ip camera that I could access from the internal network.

      METHOD 1: the simplest way to go about it was to connect the camera to the webcam with an extended usb cable. But this was not possible because of the length limitations of a usb cable.

      METHOD 2: then I read about certain Ethernet hacks we could do by slicing the Ethernet cable and connecting them to usb male and female ports which is essentially what those converters do. But this method wouldn't be successful either coz it suffered from the same attenuation and clocking limitations as before.

      METHOD 3: then there was your solution which was perfect because it would have worked on wifi and would also not require my PC to be switched on all the time which would have been necessary for the other two cases.

      is there any other way you can think of? coz if not I'll need to spend some time learning how to install the usb port on the router.

      Delete
    8. Unfortunately I do not know any other way. Either you hack the router to add a USB Host port (if possible), or you could also buy a router supported by OpenWRT that already has a USB Host port.

      Delete
    9. hehe... buying doesn't seem all that appealing... I'll learn the hack XD. Thanks a lot for the help.

      Delete
    10. Is there a tutorial or a walk-through for the hack? I found this, but dunno if it'll do..

      http://spritesmods.com/?art=sweexusb&page=2

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Can i install the OpenWRT firmware into my NuCom R5000UNv2 and work this?

    ReplyDelete
  6. To make it a little more interesting, you can now combine the opencv hello world code and you will get a nice setup for further image processing experiments with yourself in front row.
    Hikvision 8MP Cameras

    ReplyDelete