Skip to main content

Automatically rebooting Vstarcam HD C7838WIP Wireless IP Camera

I have 6 IoT devices in my house. Most of them are DIY and I have documented their build processes on this blog. One of them is a commercial product.
  1. Weather Station (OpenWrt) blogpost
  2. Energy Meter (OpenWrt) blogpost
  3. Ambient Sensors (Particle Photon) blogpost
  4. ADS-B Receiver (PiAware on RPi2) instructions on piaware
  5. DIY Security Camera (motionEye/RPi) blogpost
  6. Commercial Security Camera (Vstarcam HD C7838WIP)
Out of these, the ADS-B Receiver and the Vstarcam (both running linux) kept losing connection to the cloud regularly. I have a few theories behind why this might be happening:

  1. Both these devices lie in the fringe area (near door/windows) of my house far away from the home router which is placed in the center of the house. The WiFi signal from my neighbors might be causing interference and decrease in signal quality for these devices and the WiFi driver might be trying hard to maintain connection before giving up. I know this because I looked up the system logs of the fringe devices and video streaming on my laptop hiccups when I sit next to the window. Also this is what my balcony's spectrum looks like:

    Oh soo many WiFi's!!
  2. The WiFi chipset might be buggy
  3. The WiFi drivers might be buggy
  4. Some other application running on these devices causes the freeze up
  5. We experience power failure for very short duration a couple of times in the day. I have a UPS system for my house but the next higher networking device in the hierarchy (my ISPs) reboots everytime this happens - it does not have any backup and the diesel generator to which it is connected takes 10-20 seconds to come online. This might cause the devices to somehow lose connection to the cloud.
The Raspberry Pi2 used for ADS-B receiver is quite capable and I was able to quickly write a cron job that runs every 10 minutes to check if it has a sane connection to the internet and if it doesn't, it reboots the Pi. A blog post documenting it is here.

As for the Vstarcam HD C7838WIP, well its a whole different story. The Linux distro for RPi2 is an SD card image which has many command line utilities pre-installed which makes our life easier. The Vstarcam has its linux running from a flash chip inside it and so the manufacturer had to limit the number of software packages that make it up. But I was able to figure out how to get around those restrictions and have a script running in the background doing its job of detecting internet outage and rebooting the device. Here is how you go about it:


You might have heard of Mirai botnet and there vulnerability it uses to infect security cams like these. Console access to most of these security cams can be gained by simply telnetting into it (although from within the same LAN that they are on) and using the login as "root" and password as "123456" (Read more here). The password "123456" is hard coded into the read only file system and cannot be changed.

Using PuTTY to telnet (not SSH) into the VStarcam
As you can see, the rootfs is mounted in Read Only Mode and so root password cannot be changed.
This is not a big issue since the camera cannot be accessed from outside the LAN anyway.

The df and ls command show us the layout of the memory partitions and filesystem.

The internal flash memory is 32MB
And you can see that I have a 32GB SD card inserted for saving the video recordings.
The /system and /mnt/sda0 (SD card) are the only two read-write areas of this camera. So we can write and save a hand script to check the "online-ness" of the camera in either of these location. I chose to save it in /system (which is inside the camera itself and not on the easily removed/lost/corrupted SD card)

So I cd over to /system and use the command "vi pingtest.sh" to save the following script.
(Press i to put vi in insert mode, then past the code into your PuTTY, then press Esc and then type :wq! to save and exit)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
while true; do
        echo "Waiting for 10 minutes"
        sleep 10m
        return_value=$(ping -c 2 -W 4 8.8.8.8)
        return_value=$?

        case $return_value in
                0)echo "Ping result: ONLINE"
                  ;;
                *)echo "Ping result: OFFLINE. Rebooting..."
                  reboot
                  ;;
        esac
done

Once the script is set we need to mark it as executable. Issue the command "chmod +x pingtest.sh".

And now we need to set the script to run in the background at boot up. For that we need to add it to the init script which the manufacturers have created for their on purposes. Issue the command "vi /system/init/ipcam.sh" and add the following line to the end of that script and save it: "/system/pingtest.sh &"

Editing the ipcam.sh

(Use i to insert and Esc and type ":wq!" to save and exit)

The ampersand makes sure that the script is begins running in the background.
After you have saved the script, reboot and check if its is in fact running using the ps command:

ps command shows that the pingtest.sh script is in fact running and so is the "sleep 10"
So what this script does is to check if it is able to ping the Google's Public DNS server. If yes then it goes to sleep for 10 minutes and repeats the process. If the ping fails, it issues the reboot command.

And this will ensure that the camera always stays connected to the internet - ofcourse there will be a downtime of a few seconds when the camera is rebooting and not recording, but that is something I can live with.

In case you don't want to reboot the camera, but just want to toggle the WiFi interface, use the following contents for /system/pingtest.sh script instead:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
while true; do
        echo "Waiting for 10 minutes"
        sleep 10m
        return_value=$(ping -c 2 -W 4 8.8.8.8)
        return_value=$?

        case $return_value in
                0)echo "Ping result: ONLINE"
                  ;;
                *)echo "Ping result: OFFLINE. Restarting wifi..."
                  ifconfig ra0 down
                  ifconfig ra0 up
                  ;;
        esac
done

Comments