Building a wireless sensor hub

Those of you not thinking “A what?” are probably already thinking “Why?”; the best answer I have is mostly just because I can. Of course, there was a little more to it than that. I had several little problems or projects in mind, and this had the potential to solve several of them at a stroke.

So, what were these problems? Well, I wanted to:

  • extend my home network into the garage
  • be warned if the power failed in the garage
  • monitor the temperatures both outside, in the garage, and inside the garage freezer
  • have the option to install a 1-wire weather station on the garage roof

What I needed was a small wireless computer system, with a couple of ethernet interfaces, capable of bridging my home wireless network back onto wired ethernet in the garage. It also needed to run some simple scripts to report it’s status over the wireless network, and be capable of managing both a 1-wire network, and any other sensors that I might need in the future. Clearly it would be a bonus if it were to be cheap.

And it turns out that thanks to the ubiquitous nature of Linux, and the inquisitive nature of the worlds hacker community, it’s perfectly feasible to do all that with a single, cheap off-the-shelf box; a Linksys WRT54GL wireless router. Which I just happen to have lying around spare. Of course, it also needs a special third-party version of the firmware in combination with a little bit of reconfiguration to act as a wireless bridge. And it also needs some “minor” hardware alterations to enable some hidden serial ports, allowing a simple (but incorrectly drawn!) serial to 1-wire adapter to be added inside the routers case. So it’s not exactly trivial, but the price is definitely right, and it makes for an interesting project for a rainy Sunday. Or two.

Earlier this week I ordered all the parts to enable the serial ports, and to construct the serial to 1-wire adapter. Once they arrive I’ll combine the two circuits onto a single small piece of stripboard, which I can then install within the router case, exposing the 1-wire interface through a panel-mounted socket. In the meantime I can at least get on with the other aspects of the conversion.

So today I replaced the firmware, updated the configuration so the system acts as a wireless bridge, and installed software on both the router and my home server to allow my home server to interrogate the 1-wire sensors attached to the router, over the wireless network. Until I modify the router hardware and attach some sensors I can run the software on the router in a test-mode, which simulates a range of sensors, allowing me to test the rest of the solution.

Taking this in more detail:

  1. I chose to use OpenWRT as my replacement firmware. This is fairly similar to a standard Linux distribution, but has the advantage of a simple web-based GUI (called Luci) for management. Better however, is a replacement web-based GUI called X-Wrt, who package up their GUI and the base OpenWRT firmware together, in ready-to-flash images. For my WRT54GL, I needed the image at http://downloads.x-wrt.org/xwrt/kamikaze/8.09.2/brcm-2.4/default/openwrt-wrt54g-squashfs.bin.
    This can be used with the standard Linksys firmware “upgrade” to reflash the router. If you have already reflashed the router with a third-party firmware then you’ll typically find that you must use a .trx file. Fortunately you can convert .bin files to .trx files, as .bin files are simply .trx files with a header on the front. Usually the header is 32 bytes long; you can tell by examining the .bin file, and looking for the start of the .trx data, which starts with “HDR0”:

    richard@t60p:~/Downloads$ hexdump -C openwrt-wrt54g-squashfs.bin
    00000000 57 35 34 47 00 00 00 00 09 0c 1d 04 47 01 55 32 |W54G........G.U2|
    00000010 4e 44 00 00 1f 00 00 00 00 00 00 00 00 00 00 00 |ND..............|
    00000020 48 44 52 30 00 10 21 00 f3 aa 87 16 00 00 01 00 |HDR0..!.........|
    00000030 1c 00 00 00 04 09 00 00 00 c0 07 00 1f 8b 08 00 |................|
    00000040 00 00 00 00 02 03 a5 57 4d 6c 5b 59 19 3d be ef |.......WMl[Y.=..|

    You can then do the conversion with the command:

    dd if=openwrt-wrt54g-squashfs.bin of=openwrt-wrt54-squashfs.trx bs=32 skip=1
  2. Having loaded the firmware, the router will reboot. You must then telnet into the router using one of the LAN ports. The router will have an IP address of 192.168.1.1. At this stage there is no password, so set one using the passwd command, which will allow you to use SSH and the web-based GUI.
    You now need to disable the telnet service. You can do this most easily from the web-based GUI; surf to http://192.168.1.1/index.html. Disable the telnet service, and then stop it. Your telnet connection will be dropped. Log back into the router using SSH instead; ssh root@192.168.1.1, entering your new password.
  3. You now need to configure the networking on the router. This is done using the files /etc/config/network and /etc/config/wireless. Back them both up, and then change them as follows:

    # /etc/config/network
    #### VLAN configuration
    config switch eth0
    option vlan0 "0 1 2 3 5*"
    option vlan1 "4 5"
    #### Loopback configuration
    config interface loopback
    option ifname "lo"
    option proto static
    option ipaddr 127.0.0.1
    option netmask 255.0.0.0
    #### LAN configuration
    config interface lan
    option type bridge
    option ifname "eth0.0"
    option proto dhcp
    # -or-
    # option proto static
    # option ipaddr an-ip-on-your-subnet
    # option netmask your-subnet-netmask
    # option gateway your-gateway-ip-address
    # option dns your-dns-server-ip-address
    #### WAN configuration
    config interface wan
    option ifname "eth0.1"
    option proto dhcp

    and:

    # /etc/config/wireless
    config wifi-device wl0
    option type broadcom
    option channel auto
    config wifi-iface
    option device wl0
    option network lan
    option mode sta
    option ssid yourssid
    # psk = WPA Personal, psk2 = WPA2 Personal
    # wpa = WPA Enterprise, wpa2 = WPA2 Enterprise
    option encryption psk
    option key "your key here"
  4. Now disable and stop the dnsmasq and firewall services using the web-based GUI. Failing to do this will prevent the bridge from passing your DHCP requests through to your home network correctly.
  5. Reboot the router, and it should connect to your wireless network, requesting a dynamic IP address for its bridge interface. In my case, I configured my main DNS/DHCP server to always give my WRT54GL the same “dynamic” IP address whenever it connects, making it easier to access. You can achieve the same effect by defining the bridge interface with an appropriate static IP address if you prefer.
  6. I then SSH’d back into the router, and updated the package management system by running the command opkg update. I was then able to install OWServer on the router by simply entering the command opkg install owserver. As I have no 1-wire hardware for OWServer to examine, for the time being I use it in simulation mode, using the command owserver –fake=05,10,21 –foreground, which allows me to test my home servers connectivity to the OWServer software.
  7. On my home server, I downloaded http://sourceforge.net/projects/owfs/files/owfs/2.7p29/owfs-2.7p29.tar.gz/download, which I saved locally. I then untarred this, and compiled (as a simple test) the OWShell programs. Do a configure in the root of the untarred directory, and then change down into owfs-2.7p29/module/owshell, and run the commands:

    make
    sudo make install

    I can now do owdir -s router-host:4304 and be presented with a response from the OWServer running on the router. From here its a short step to getting the Perl and PHP language bindings created, allowing me easily script up access to the 1-wire network hosted on the WRT54GL. But first I need to get some real sensors attached to the router…
Advertisements

6 thoughts on “Building a wireless sensor hub

  1. Pingback: Network upgrade « Richard's Blog

  2. Pingback: 2010 in review « Richard's Blog

  3. can you tell me how to interface a sensor with router bcoz i’m doing tis as my final sem project..and mail me detailed info abt tis project..please

    • I’m not sure what more you want. This post talks about the unused internal 3.3v serial lines in the WRT54GL router, and points to a way of connecting them to a 1-wire microlan. Adding 1-wire sensors to the microlan is then trivial. Reading those sensors is then simple using the referenced software.

      Admittedly, the referenced circuit needs some work, but should be enough to point you in the right direction. You’ll need to do something about the serial lines being 3.3v logic and the circuit 5v logic. But that should be easily resolved with a level converter like the MAX3375, MAX3394 or NXP GTL2002.

      Ultimately, if this is for an examined course then you should be doing this work for yourself, not cribbing it all off the Internet.

  4. Pingback: Building a wireless sensor hub (part ii) « Richard's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s