Saturday, January 9, 2016

DIY Surveillance Web Cam with Old Android Smartphone

Nowadays it's pretty common for people to have old smartphones lying around unused. In my case I have a 3-year old Samsung Galaxy S3 which is considered geriatric as a smartphone, but still works fine. It's got camera and Wi-Fi enabled, so I figured I could use it as surveillance camera. Other than the smartphone itself, you also need to have these things in order to be able to access the camera over the web:
  1. An always-on Internet connection (such as DSL or cable Internet).
  2. A Wi-Fi router (either connected to the DSL/Cable modem or integrated with the modem)  
  3. Admin access to the router
  4. Basic knowledge on IP networking is a plus. But don't let this discourage you.
Here are the steps:
Note: the detailed steps in this guide is specific to the Wi-Fi router I have, which is TP-Link TL-MR3420. In general the process should be the same for any home router. You may need to consult the user manual for detailed instructions.

I. Assign static IP to your smartphone

Typically you don't need to concern about the IP address assigned to your devices by the Wi-Fi router. However, in this case the smartphone will be acting as a streaming server. So having a static IP address will make it easier to access the device.
  1. First, lookup the current IP address assigned to the smartphone.
    Go to Android settings -> Wi-Fi settings. Tap on your home network (the one your device connected into), you should then see your current IP address. Take note of it.
  2. Open your router's Administration Web GUI in the browser (from your PC/laptop connected to your home network). For home router, usually the default URL is http://192.168.0.1. For TP-Link, the default username/password is admin/admin.
  3. Go to DHCP -> DHCP Client List. Find the entry that has the IP address noted in previous step. Copy the the MAC address for that device.
  4. Go to DHCP -> Address reservation. Add a new entry that maps the MAC address you noted earlier to a particular IP address. Just make sure the IP is outside the DHCP range. In my case I assign IP address 192.168.0.91 to my old Samsung. Let's assume you also use this IP address.
  5. You may need to reboot the router.
  6. After the router rebooted, check the Wi-Fi settings in the smartphone. It should be using the new IP address now (192.168.0.91).
Alternatively, you can set static IP address directly from the smartphone. So, it's just a matter of preference.

II. Install IP Webcam App in your device

There are several apps that provides surveillance camera function. But this is the first one I found and tried. The GUI is not fancy but it works fine.
  1. Search and install IP Webcam app by Pavel Khlebovich via Google Play Store.
  2. Open the app. Adjust the video preferences as you see fit. For me, I'd rather have relatively low resolution (eg. 640x480) and low bitrate(eg. 700 kbps) so it won't take too much space.
  3. Don't forget to set login/password in the Local Broadcasting section. You don't want to let everybody be able to watch your video stream. Unless of course that is exactly what you want.
  4. Start the server (This option is at the bottom). You can now close the app by pressing the home button. The app will still run in the background.
  5. You can access the surveilance cam using web browser (from your PC/laptop connected to your home network) in this URL: http://192.168.0.91:8080. Select "Browser" as the video renderer.

So now you can access the surveillance cam from other devices/computer. But you have to be connected in your home network to do so. Not very useful.
Thus, the next thing you want to do is to access the web cam over the Internet.

III. Access the surveillance camera over the Internet.
  1. Access your router's Administration Web GUI again (http://192.168.0.1). 
  2. Go to Forwarding --> Virtual Server section. Create a new entry with the following parameter:
       Service Port: 8080
       Internal Port: 8080
       IP Address: 192.168.0.91
    This basically tells your router that any incoming request (from the Internet) to port 8080 must be forwarded to a particular address/port in your home network (192.168.0.91:8080)
  3. Check you home network's public IP address. Just google "my ip". Google will tell you your public IP address. Take note of this.
  4. Now you can access your web cam over the Internet in this URL http://[my-public-IP]:8080
Lastly, the only remaining problem is the fact that usually the public IP for your home network is not permanently assigned to you by your Internet Service Provider. If you're away from home it may not be practical to check your current public IP address. The solution for this is Dynamic DNS. One example of dynamic DNS service is NoIP (http://www.noip.com). It provides the basic service for free.

IV. Configure Dynamic DNS for your home network
  1. Register to www.noip.com. Setup your username and domain name. For free service your domain name is limited to [my-domain].ddns.net
  2. Activate your account by following the confirmation link sent to your email.
  3. Access your router's Administration Web GUI again (http://192.168.0.1). 
  4. The nice thing about TP-Link router is that it has built in support for some dynamic DNS services.
    Go to Dynamic DNS section, then enter your No-IP username, password, and domain.
  5. Now you can access your web cam in this URL http://[my-domain].ddns.net:8080. 
Whenever your router gets new public IP, it will inform NoIP.com. NoIP.com will then update it's DNS server so that your domain will point to your new public IP address. This way you can access your web cam via static URL, instead of an ever changing IP address.

Next you may want to explore the functionalities of IP Webcam app. For example you can record the video continuously in circular mode. So older videos will be purged automatically when storage is low. Note that the video files are stored in the smartphone. So you might want to put large capacity memory card in the device.

Also note that NoIP site says that free domain name expires every 30 days. I'm not sure if it means we can still extend it every 30 days and stay free, or we have to upgrade to paid service. Oh well, we'll see in 30 days.

Of course, using smartphone as a surveillance camera has some drawbacks. First, if it is placed somewhere very accessible, it could be the very first item that gets stolen. Secondly, in the approach above, the video is recorded the device's internal memory. Thus, it if gets stolen, the recordings are lost too. A better approach would be to store the recordings in an external server. That's a project for another weekend.