OpenWrt

OpenWrt is an open source Linux based firmware alternative for routers. Check out www.openwrt.org.

Important: OpenWrt works best with at least 4Mb of flash and at least 16Mb of RAM. See http://oldwiki.openwrt.org/CompleteTableOfHardware.html for a hardware compatibility list. Do not buy hardware with less memory (like the WRT54GS v5 or later, where Linksys switched from Linux to VxWorks). If possible, avoid hardware marked with WIP. This stands for "Work In Progress".

Generic setup example: Linksys WRT54GL

Download the most recent firmware image of Kamikaze suitable for your router from www.openwrt.org. For the Broadcom based Linksys WRT54GL, the file is called openwrt-wrt54g-squashfs.bin. This can be flashed on the router, using the webinterface. Please wait 2 more minutes after uploading. The router will reboot itself automatically. After that, you should be able to login using telnet (IP-address 192.168.1.1). Then, it is important to run these commands on the router:

nvram set boot_wait=on
nvram set boot_time=10
nvram commit
reboot

This enables you to reflash your router in the future using TFTP.

Here is the procedure for flashing using TFTP. First poweroff the router. Then, on the host, install tftp-hpa and enter these commands:

tftp 192.168.1.1
tftp> binary
tftp> trace
tftp> put openwrt-wrt54g-squashfs.bin

Power on the router and wait for 2 minutes. After that, the router will reboot itself with the new firmware.

After you change the root password on the router, using the passwd command, telnet will be disabled and you will be able to login using SSH.

On the WRT54GL there are 2 serial ports, the first one being used as console. The /dev entries are:

  • /dev/tts/0 for the 1st port (0)
  • /dev/tts/1 for the 2nd port (1)

On the CL7B version I have, the ports can be found on JP2. Pinout:

Pin Function
1 3.3V
2 3.3V
3 TX_1
4 TX_0
5 RX_1
6 RX_0
7 NC
8 NC
9 Gnd
10 Gnd

On the WAG54G v2, there is one serial port on JP3:

Pin Function
1 Gnd
2 RX
3 TX
4 NC
5 3.3V

These ports work on 3.3 volts. You can use a MAX232 to do some level shifting. Using a 7805 you can convert the 12 volts from the power supply to 5 volts, suitable for the MAX232. For RX, use 2 resistors as divider for converting 5 to 3.3 volts. TX can be connected to the MAX directly.

By default, the first port works on 115200,8,1 and the second one on 9600,8,1 using software flow control only.

The network settings can be found on/etc/config/network and /etc/config/wireless. An example of wireless:

config wifi-device wl0
   option type broadcom
   option channel 5

   # REMOVE THIS LINE TO ENABLE WIFI:
   # option disabled 1

config wifi-iface
   option device wl0
   option network lan
   option mode ap
   option ssid netwerknaam
   option encryption wep
   option key 1
   option key1 'xxxxxxxxxxxxxxxxxxxxxxxxxx'

Using the wifi command you can test new settings without rebooting the router. On the host you should be able to connect wirelessly using:

iwconfig eth1 essid test key xxxxxxxxxxxxxxxxxxxxxxxxxx

However recent versions of Ubuntu / Kubuntu ship with a broken version of iwconfig so this may not work. If that's the case use a GUI, like knetworkmanager. If iwconfig works for you, you can either supply an IP address manually using ifconfig or obtain it dynamically from the router using dhclient.

Setting up a cross compiler / toolchain

You can build new programs for your router, but compiling must be done on the host because of limited storage space. Setting up a build environment with a cross compiler is easy but requires a working internet connection and a lot of space. First install the software needed:

aptitude install build-essential bison flex gawk \
   libncurses5-dev zlib1g-dev subversion autoconf

Then download kamikaze_8.09.tar.bz2 and build the sources as regular user (not root) using:

make menuconfig
make V=99

The V=99 stands for verbose mode (give some messages during the build).

During building, many components are downloaded, like the kernel, the compiler, patches etc., so you should have a working internet connection. After this, put staging_dir/toolchain-mipsel_gcc-4.3.3+cs_uClibc-0.9.30.1/usr/bin in your PATH and use mipsel-openwrt-linux-uclibc-gcc to compile something. You can transfer the executable to your router using scp.

Asus WL500GPV2

(work in progress, last update 07-06-2009)

This router has a built-in USB 2.0 interface, so there are lots of extra possibilities here. For example, you can:

  • attach webcams for video streaming
  • attach external drives / memory sticks for extra storage
  • make printers and scanners work wirelessly
  • use serial port adapters to control DIY electronics wirelessly
  • use bluetooth adapters for interfacing with mobile phones, GPS devices and even the Wiimote...
  • attach USB hubs in case you just ran out of USB ports

I figured to use it as a webcam server with motion detection.

Support of the V2 version (the WL500GPV2) is not ready yet. In particular Wifi on Broadcom devices using the 2.6 kernel does not work and we need the 2.6 kernel because it has better multimedia support. To workaround the Wifi problem, I'll just plug in a cheap USB dongle for wireless networks. The Edimax EW-7318Ug and the Sitecom WL-172 both cost about 12 euro's and work with the rt73usb driver which appears to be supported by OpenWrt. The zd1211 is also supported. I found a spare Linksys WUSBF54G which is zd1211 based, so I'll be using that. There is one catch, though. The drivers of those USB dongles do not support master mode (aka AP or access point mode), so we need to use either Ad-Hoc mode or STA (aka station / client / managed) mode. Note that this hack is temporarily until the Wifi driver for the built-in device is finished.

As far as the webcam is concerned, it is best to use a UVC compatible webcam. The Logitech E3500 is cheap (25 euros) and works for me, but it is not a high quality webcam in my opinion. There is lots of motion blur when the light conditions are poor. The early Philips webcams are a lot better, but unfortunately the PWC drivers needed for these webcams crash under OpenWrt. The Philips SPC530NC is UVC compatible and should work, like the Logitech E3500, but I haven't tested it yet.

You can store the images caught with the webcam on a USB memory stick / drive. You might need a hub because there are only 2 USB ports on the router.

A couple of things you should know regarding the flash procedure:

  • when you have changed the IP address of your router using Asus' webinterface, the CFE bootrom uses that address instead of 192.168.1.1
  • if you cannot remember what IP address you put into your router, it is good to know you can restore to factory defaults by pressing the restore button at the back of the router for 5 seconds while the router is running. After that the router should be accessible using 192.168.1.1.
  • to flash a new image, power off, and while holding the restore button power on, and keep the button pressed for 5 seconds. The power led will be blinking at 1Hz. Try pinging. If that works start to tftp the new image.
  • after flashing and booting OpenWrt, the power led may be off. This is no problem. In my case the led turns on when the SSH daemon is running.
  • you can place a symlink from /var/www/kamikaze to the location of your packages. This is easier than changing the settings of Apache.

Ok, so here are the actual steps done so far:

  • Install software needed for building OpenWrt:
    aptitude install build-essential bison flex gawk \
       libncurses5-dev zlib1g-dev subversion autoconf
    
  • Download the latest Kamikaze trunk:
    svn checkout svn://svn.openwrt.org/openwrt/trunk kamikaze
    svn checkout svn://svn.openwrt.org/openwrt/packages packages
    
  • Symlink the packages you're interested in. In my case:
    cd kamikaze/package
    ln -s ../../packages/libs/jpeg
    ln -s ../../packages/multimedia/motion
    ln -s ../../packages/net/ntpclient
    cd ..
  • Do a "make menuconfig" in the kamikaze directory. Choose "Broadcom BCM947xx/953xx" (not "...[2.4]" !) as target system and "No WiFi" as target profile. Select the stuff you need. You can also choose "Select all packages by default" in "Global build settings", but note that not all packages may build correctly. Fortunately r19904 (dated 28-02-2010) builds all the way.
  • Do a "make V=99" to build everything. This takes a long time. If you have multiple cores, use the -j option (right after "make") followed by the number of cores plus one. This will compile multiple files in parallel. If an error occurs, though, it is difficult to see where exactly the error occurred in case the -j option was used. In that case restart "make" without the -j option.
  • TFTP the image kamikaze/bin/brcm47xx/openwrt-brcm47xx-squashfs.trx to the router.
  • Configure the webserver to host kamikaze/bin/brcm47xx/packages/target-mipsel_uClibc-0.9.30.1. The easy way:
    cd /var/www
    ln -s /path/to/kamikaze/bin/brcm47xx/packages kamikaze
  • On the router set the password using the "passwd" command. A few moments after that you can login using SSH. Telnet will be disabled.
  • Edit the line starting with "src/gz" in /etc/opkg.conf to reflect your webserver. Mine looks like this
    src/gz snapshots http://192.168.1.2/kamikaze
    dest root /
    dest ram /tmp
    lists_dir ext /var/opkg-lists
    option overlay_root /jffs
    
  • Do a "opkg update", followed by:
    opkg install kmod-fs-vfat kmod-usb-storage kmod-usb2 kmod-usb-ohci kmod-nls-cp437 kmod-nls-iso8859-1 kmod-nls-base
    opkg install kmod-zd1211rw
    opkg install kmod-i2c-core kmod-i2c-algo-bit kmod-input-evdev kmod-video-uvc
    opkg install libpthread motion ntpclient
    rm /etc/TZ
    echo 'CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00' > /etc/TZ
    uci set ntpclient.@ntpserver[0].hostname=1.openwrt.pool.ntp.org
    uci commit ntpclient
    
    The latter 4 commands configure the timesettings (timezone is Central European Time, and timeserver is 1.openwrt.pool.ntp.org, adjust if needed).
  • Plugin the wireless dongle in the router and reboot the router. This will create /etc/config/wireless. Edit the ESSID, setup the security, and comment out the "disabled" line. At the time of writing there appears to be no support at all for USB wireless dongles running in Master (Access Point) mode. However, Ad-Hoc mode is supported in the zd1211rw driver, so I had to change "mode ap" into "mode adhoc". You can also use client (STA) mode in order to let your router connect to another accesspoint. Here is an example of /etc/config/wireless which lets you do that:
    config wifi-device wlan0
       option type mac80211
       option channel x
    
    config wifi-iface
       option device wlan0
       option network lan
       option mode sta
       option ssid xxxx
       option bssid xx:xx:xx:xx:xx:xx
       option encryption wep
       option key xxxxxxxxxxxxxxxxxxxxxxxxxx
    
    Change the channel number, ssid, bssid (mac address of your access point) and key according to you needs. You can find most of them using "iwlist scan". For changing the IP address, edit the /etc/config/network file. Next, run the "wifi" command or reboot the router. If you chose to use Ad-Hoc mode, you should be able to see the router on your host. When you connect to your router do not forget to set Ad-Hoc mode.
  • Rename the old /etc/motion.conf and create a new one with the following contents:
    webcam_port 88
    webcam_localhost off
    webcam_maxrate 10
    webcam_motion on
    output_normal off
    locate on
    target_dir /root
    # snapshot_interval 1
    # width 640
    # height 480
    # gap 5
    daemon on
    quiet on
    Notes:
    • webcam_port enables the webserver at the specified port.
    • webcam_localhost is for preventing access to hosts other than localhost.
    • webcam_maxrate is the maximum number of frames per second that is sent to the webclient.
    • webcam_motion decreases the framerate to webclients to 1 frame per second when no motion is detected. If motion is detected the framerate increases to webcam_maxrate.
    • output_normal specifies whether or not the images should be saved.
    • locate specifies whether or not a bounding box should be drawn around the moving object.
    • snapshot_interval is for taking a snapshot every n seconds.
    • width and height specify the desired width and height of the images.
    • gap is the number of seconds after the last motion event before stopping the recording of that event.
    • daemon is for running motion in the background.
    • quiet surpresses any messages or beeps.
  • create /etc/init.d/motion with the following content:
    #!/bin/sh /etc/rc.common
    
    START=99
    
    start() {
       motion
    }
    stop() {
       killall motion
    }
    
  • make it executable:
    chmod 755 /etc/init.d/motion
    
  • and set it up as startup script:
    cd /etc/rc.d
    ln -s ../init.d/motion S99motion
    
  • create a default web page /www/index.html with the following content:
    <img src="http://192.168.1.1:88" />
    
  • Reboot
  • Using a webbrowser go to http://192.168.1.1/ to view the images

Links