Software

Arduino Source Code and Compiling

As of writing, the cat feeder source code and modified supporting libraries may be found here.

For those uncomfortable with git, download this version of everything via this link (google shortcut to the zip file).

See the cat_feeder subdirectory. You’ll need Linux or OS X to properly extract everything (cygwin might work in Windows?).

We use the excellent arduino.mk project along with Arduino’s command line tools, as installed in the directory

/opt/arduino

To get arduino.mk, click here and follow the installation directions.

Wi-Fi Router Configuration (Arduino)

To flash and configure the TP-Link, connect the ethernet cable into your computer. You may need to download some files to your computer before you do this.

The one we ordered was running a Chinese-language firmware. Using chrome+google translate / guesswork, it’s possible to find the page where you can upload your own. Please read the associated open-wrt wiki, especially the Supported Versions section. The firmware + flash instructions are here. Use the web interface to simplify your life.

Once you’ve installed open-wrt on the router and rebooted, you want to set up the router to run in wifi client mode. Go into the LuCi web interface and set a password. Set up wi-fi client mode and connect to your (hopefully WPA2 protected) home router. Configure the TP-Link LAN to be 192.168.2.0 with the internal router IP as 192.168.2.1 (our home WiFi router uses a LAN network of 192.168.1.1 so there is no conflict).

The Arduino software registers the Wi-Fi shield with MAC address 0xdeadbeeffeed and expects to have IP address 192.168.2.2. It is important to set this properly on the router. Under NetworkDHCP and DNSStatic Leases, add the following settings

Hostname

MAC-Address

IPv4-Address

arduino

--custom-- → de:ad:be:ef:fe:ed

--custom-- → 192.168.2.2

The arduino relies on the router to act as the DNS resolver, and OpenWRT performs transparent DNS forwarding by default.

Wi-Fi Router Configuration (Webcam)

For this section, We roughly followed the instructions here, except with modifications for ease-of-installation and bugfixes.

The OpenWRT web interface has a wonderful installation wizard to download the mjpg-streamer package. This package provides a web service you can use to monitor the cat feeder from anywhere inside the home (and via some additional trickery from an external script, upload photos to your website of choice). To install mjpg-streamer from the OpenWRT LuCi web console:

    1. Go to SystemSoftware.

    2. Under Actions click Update Lists.

    3. Under Status click the tab Available Packages, then type and install the following packages in the ActionsFind Package entry above:

    • kmod-video-uvc

    • mjpg-streamer

Avoid installing any other packages, as this router has extremely limited storage space. From here on, if you don’t know Linux things will get a little confusing. Make sure you have the ssh client installed. We need to configure mjpg-streamer. Commands in black, expected responses in red. You may be asked for a password when logging in. Enter your web admin password.

$ ssh root@192.168.2.1 BusyBox v1.19.4 (2013-03-14 11:28:31 UTC) built-in shell (ash)Enter 'help' for a list of built-in commands. . . .root@OpenWrt:~# cat > /etc/config/mjpg-streamer <<EOF config mjpg-streamer core option enabled "1" option device "/dev/video0" option resolution "640x480" option fps "5" option www "/www/webcam" option port "8080" EOF root@OpenWrt:~# /etc/init.d/mjpg-streamer enable root@OpenWrt:~# /etc/init.d/mjpg-streamer restart root@OpenWrt:~# exit Connection to 192.168.1.101 closed. $

At this point, to test the (connected) webcam, browse from the connected computer to:

http://192.168.2.1:8080/

Port Forwarding

In practice, you will want to allow computers inside the home network to connect to both the webcam and to the control API provided by the Arduino software. To do so requires two steps:

Set a fixed lease IP address for the TP-Link router in your home router’s DHCP settings (our home LAN network is 192.168.1.0 and we set the TP-Link IP address to 192.168.1.101).

Set up port forwarding for the various services on the TP-Link router. While connected to the TP-Link LAN network, open up LuCi and browse to NetworkFirewall Port Forwards. Set up the following port forwards:

Note, this setup allows you to access the LuCi web console from your home network via two steps. Assuming you have set the TP-Link router IP to 192.168.1.101 on your home network:

    1. Open up a tunnel to the webpage via

ssh root@192.168.1.101 -L9000:localhost:80

    1. While the tunnel is running (you are connected), browse to:

cal-align:baseline;whihttp://localhost:9000/

You can access the Arduino control page (once the Arduino is setup and configured):

http://192.168.1.101:8888/

and a JSON REST interface via:

<http://192.168.1.101:8888/json/

You can access the webcam web interface via:

http://192.168.1.101:8080/

(then click Stream, Java, or Javascript).

Uploading Images to a Web Site

We have our home router running a cron job to upload one image a minute to a remote web site. This required setting up ssh keys and other bits that are very technical. For those who are interested, here (roughly) is the crontab entry:

root@homerouter:~# crontab -l */1 * * * * FN=webcam-$(date +"%H_%M").jpg; cd /tmp && ping 192.168.1.101 -c1 -W1 && ping remote.site.com -c1 -W5 && wget -q http://192.168.1.101:8080/?action=snapshot -O $FN && echo -e "cd ./remote_directory/\nput $FN webcam/\nrm webcam.jpg\nsymlink webcam/$FN webcam.jpg" | sftp -o ConnectTimeout=3 -i $HOME/ssh_priv_key remote_user@remote.site.com; rm $FN

This script assumes that there exists directories remote_directory/ and remote_directory/webcam/ in the home directory of remote_user on remote.site.com, and that you are using an ssh keypair with an empty password, for which ssh_priv_key is the private key file. Uploading to a remote site via ftp is probably simpler. The script aboves the commands ping, wget, sftp, and ssh. Dropbear ssh is not sufficient: the OpenSSH version is necessary to work with sftp. Our webhost does not allow remote login via ssh directly; otherwise we would have used scp instead of sftp (it’s much simpler).