## Turning old mini-routers into useful remote cameras

18-02-2020

I've had a few of these tp-link TL-WR703N, little blue boxes laying around for some time now. I used quite a few in a previous gig sometime ago where they proved quite useful. Since then, I've not had a lot of use for them. They languished for some time in the future projects box, until now!

As part of a bigger project, I need some remote cameras. From my 3D Scanning dome project I also had a bunch of Logitech c910 cameras and so I wondered, would it be possible to create a remote webcam using the tp-link and the c910s? Something I could just plug into the mains and it would work, over wifi, with no further user intervention? Turns out it is indeed possible though it requires a small amount of faff.

Some might think this is a silly project, but I've a few good reasons for giving this a go, which I'll talk about at the end. I'm glad I started and finished this project and I hope that it might be useful for some folks.

### Building the right OpenWRT

I use OpenWRT on most of my projects. At one point this project branched off into something called LEDE but is now back to OpenWRT again. One of the older versions still support the TL-WR703N so I figured this was the place to start. OpenWRT has a lot of cool features, software and documentation through it's wiki. It's an excellent resource for these wanting to mess with their network hardware, and their page on the WR703N is excellent.

The biggest problem is the amount of memory these things contain. They have 4MB of flash memory and 32M of RAM, so not an awful lot! This means that we need to cut down on some of the packages included in OpenWRT by default. With some space freed up, we can install the bits we need for the webcam, perform setup over SSH and we shoud be done.

Version 17.01.07 is the version of OpenWRT you can use with this box. You can download it from http://downloads.openwrt.org/releases/17.01.7/targets/ar71xx/generic/lede-imagebuilder-17.01.7-ar71xx-generic.Linux-x86_64.tar.xz. Once you've decompressed the archive, I ran the following command:

make -j4 image PROFILE=tl-wr703n-v1 PACKAGES=\
"-libiwinfo-lua -liblua -libubus-lua -libuci-lua -lua \
-luci -luci-app-firewall -luci-base -luci-lib-ip \
-luci-proto-ppp -luci-theme-bootstrap -uhttpd -uhttpd-mod-ubus \
kmod-fs-ext4 kmod-usb-storage kmod-scsi-core block-mount \
kmod-lib-crc32c kmod-crypto-crc32c kmod-video-uvc mjpg-streamer motion"


It's worth breaking this down a little. I'm removing all the web-based configuration packages - called Luci - along with the lua scripting language and andy themes, plus support for ipv6 (as I doubt I can make use of that yet). This should free up a bit of space.

The packages I need for the webcam include kmod-video-uvc, motion and mjpg-streamer. Motion probably isn't essentially but I've used it before and I can be fun!

We should check the size of the resulting image. We can do that with the following command:

du -b bin/targets/ar71xx/generic/lede-17.01.7-ar71xx-generic-tl-wr703n-v1-squashfs-factory.bin


The next bit depends on what is already installed on your little tp-link. I had a previous version of OpenWRT installed so I used scp to copy the image to the /tmp directory on the router. It's important it goes into /tmp as the following command relies on that. If you have another operating system on this router, you can follow whatever image upgrade steps that OS suggests.

The command I used was:

sysupgrade -v /tmp/lede-17.01.7-ar71xx-generic-tl-wr703n-v1-squashfs-factory.bin


The process so far could be adapted to whatever circumstance you are using the router in. There are quite a few software packages one can install in the base image. In fact, I believe one can customise the kernel and do all sorts of things if one desired. For our purposes though, this should be enough.

### Setting up the networking

The next process is to ssh into our router (using root as the username. I believe the password is password or just blank at this point). OpenWRT tends to default to 192.168.1.1 so use an ethernet cable and set your interface accordingly. Wifi isn't enabled by default, so the next step is to turn it on:

uci set wireless.@wifi-device[0].disabled="0"
uci commit wireless
wifi


We can then scan for wifi networks, making sure we can see the one we want to use.

iw dev wlan0 scan


Now we should setup the default wifi and all the credentials, by editing /etc/config/wireless

config wifi-device 'wlan0'
option type 'mac80211'
option hwmode '11ng'
option path 'platform/ar933x_wmac'
option htmode 'HT20'
list ht_capab 'SHORT-GI-20'
list ht_capab 'SHORT-GI-40'
list ht_capab 'RX-STBC1'
list ht_capab 'DSSS_CCK-40'
option country 'GB'
option txpower '20'
option channel '3'
option disabled '0'
config wifi-iface
option device 'wlan0'
option network 'wwan'
option ssid '<SSID>'
option wds '1'
option encryption 'psk2'
option mode 'sta'


Change the and to these for your network. We now need to make this router visible to the rest of the world over the wifi and setup the ethernet to something a little more useful so it doesn't interfere with anything else on the network. I'm setting this router to have a static ip on 10.9.8.1 which I write onto the case with a sharpie so I never forget in the future. Some of these routers still have IP addresses I don't know! Edit /etc/config/network

config interface 'loopback'
option ifname 'lo'
option proto 'static'

config globals 'globals'
option ula_prefix 'fd5b:b346:5d47::/48'

config interface 'lan'
option ifname 'eth0'
option type 'bridge'
option proto 'static'
option ip6assign '60'

config interface 'wwan'
option proto 'dhcp'


Finally, we can set a hostname and restart all the things:

uci set system.@system[0].hostname="StCammy"
uci commit system


### Video time

Sometimes, I've noticed that the video packages aren't actually installed. When I try to run mjpg-streamer or motion, the system complains. If that happens, then one can fix it as follows:

opkg update
opkg install kmod-video-uvc mjpg-streamer motion


Your router needs to be connected to the internet at this point. Hopefully it should be.

We should be ready to run mjpg-streamer and actually see something. Plug in the camera and run the following command:

mjpg_streamer -i "input_uvc.so -d /dev/video0" -o "output_http.so"


This runs the program, looking for the first video device, spitting the output to a webserver. Remember that there isn't a lot of memory available on this device, so saving video or even images is probably not going to work,

You can then open a web-browser and type in the following address:

http://10.9.8.1:8080/?action=stream
http://10.9.8.1:8080/?action=snapshot


If these don't work, use the IP address that the router has picked up from your wifi network. this page has some good advice on using webcams with the WR703N. It's worth a look.

We should check the firewall at this point too as that might be stopping your connection. We want to allow certain ports to be accessible from the outside world:

vim /etc/config/firewall
/etc/init.d/firewall restart


We want mjpg-streamer to start with the right options every time we turn on the router. We should start by editing the mjpg-streamer config files. There are two of them:

vim /etc/init.d/mjpg-streamer
vim /etc/config/mjpg-streamer


The options in these files are self-explanatory, though pay attention to the resolution and FPS. I found that 640x480 and 5fps was fairly stable. High resolutions and frame-rates seemed to work for a short time, but the router would then stop responding. Not entirely sure why but there is a lot of data to process and shift and these routers aren't exactly power-houses.

Finally, run the following command:

/etc/init.d/mjpg-streamer enable


This sets up mjpg-streamer to launch at boot. At this point, it should all be working. You can setup port forwards or do something more interesting with the camera, but all that should be needed is a power socket and you are good to go.

### Why do this?

There are products on the market that are much easier to setup, probably higher resolution and more reliable. But I place more trust in these little routers I've setup. OpenWRT is a pretty good project that I personally trust. I can tell where all the data is going, how secure these cameras are and most importantly, I have complete control over them.

Some of the big problems we face in computing and technology at the moment include who-owns-the-data, security, surveillance culture and e-waste. This project tackles all of these problems head-on. By putting all the pieces together, I've saved some tech ending up in landfill. I've learned a bit more about OpenWRT and above all, I've brought a little more joy into my life.

Recently, I've been travelling around the UK a lot, and sometimes, it's just nice to be able to look at how your back-garden is turning out. Rather than being afraid and angry at all the surveillance cameras around London, this one cheers me up. Proof that we can turn our tech-negatives into positives.