I have a problem with the network in my home in that, at times, I need to reset my wireless router and other network components. This doesn’t happen too often, but when it does, I need to go all the way to the basement where the network components are kept, and unplug each one, wait 15 seconds, and plug them back in. In my case, I have found that I don’t necessarily need to bring them up in order, but simply need to power cycle the whole lot of them to reset my network. But…it’s a pain.
Note: This tutorial assumes a basic working knowledge of Linux. For more information concerning Linux in general, consider taking a look at the Linux resources at: http://www.barryhubbard.com/category/linux/.
What I want to do?
I would like to have a smart plug that I can trigger through a web interface to power cycle my network components. I would like to click on a link in my browser and have the smart plug automatically power off the components, wait 15 seconds, and power them back on.
Of course, once I power off the network, I will lose any connection to the smart plug (which connects only through WiFi). My WiFi router is one of the devices that I want to be able to reset. So, the smart plug has to automatically power back on the plug, once I initiate the “restart” sequence.
What do I have?
Last year, I received one of these little smart plugs made by a company called KanKun.
Actually, finding out who made them was half the battle. My packaging didn’t mention Kankun at all, but called it BESTOPE smart plug. Other information on the plug indicates that it is made by Konke. They appear to be one in the same, though.
The software for this smart plug wasn’t overly useful. Although there is an app designed to be able to turn the plug on or off from anywhere in the world, I found it somewhat unreliable. Also, due to the device needing to have an active WiFi connection to be controlled, the app wasn’t going to help with my whole reset problem.
Thanks to some information I found over at Anites Website I found that the smart plug is simply running BusyBox, a stripped down embedded Linux system. The nice thing is that this is Linux, which means that we can play.
There are some basic tools available to us, including an already installed Web server, able to execute CGI scripts.
Initial Log In
The first thing you must do is log in to the smart plug. You do this through ssh. You will need a computer with both a WiFi adapter and an SSH client.
Reset the Smart Plug
To reset the Smart Plug back to factory settings, press and hold the front button for 5 seconds.
Once you release the button, the blue light on the front of the unit should stay on solid for approximately 30 seconds. This indicates that the unit is booting. Once booting is complete, you should be able to open the WiFi settings on your computer and see a new wireless network with an SSID of OK_SP3. You will need to connect to this network.
Once connected to the network, you can run ifconfig (Linux) or ipconfig (Windows) to determine your network settings. You should have been assigned an IP address by the KanKun smart plug. You should also note the default gateway, as this is the IP address of the smart plug.
The smart plug should have an IP address of 192.168.10.253.
From here, ssh into the smart plug using the following credentials:
Username: root Password: p9z34c
The anites site has additional default passwords that are sometimes used for the device, if you are having trouble logging on.
Some Initial Setup
Once logged on for the first time, there are a couple of things that are good to do:
- Set a new password
- Attach the smart plug to your own wireless network
Setting a new password
You are already logged in as the root user. So, all you need to do to set a new password for the root user is to execute the following command:
Then enter the password that you would like to use. This will now become the password that you will use going forward.
Attach the smart plug to your wireless network
I highly recommend assigning your smart plug a static IP address. This will allow you to always know where your smart plug is located on the network in the future.
My basic network configuration is:
Network: 192.168.2.x Gateway: 192.168.2.1 Netmask: 255.255.255.0 WiFi SSID: MyNetwork WiFi Password: my_password
I would like to assign
192.168.2.12 as the address for my smart plug.
So, first, you will need to edit the /etc/config/wireless file using vi. For information on using vi, look at: http://www.barryhubbard.com/linux/useful-linux-commands-working-with-text/. To edit the file, simply type the following at the command prompt:
# vi /etc/config/wireless
/etc/config/wireless to match your settings. Based on the above configuration, my
wireless file would look like this:
config wifi-device radio0 option type mac80211 option channel 11 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 # REMOVE THIS LINE TO ENABLE WIFI: option disabled 0 config wifi-iface option device radio0 option network wwan option mode sta option ssid 'MyNetwork' option key 'my_password' option encryption psk
Next, you will need to modify the
/etc/config/network. The contents should be:
config interface 'loopback' option ifname 'lo' option proto 'static' option ipaddr '127.0.0.1' option netmask '255.0.0.0' config globals 'globals' option ula_prefix 'fdab:ee91:1053::/48' config interface 'lan' option ifname 'eth0' option type 'bridge' option proto 'static' option ipaddr '192.168.10.253' option netmask '255.255.255.0' option ip6assign '60' config interface 'wwan' option proto 'static' option ipaddr '192.168.2.12' option netmask '255.255.255.0' option gateway '192.168.2.1'
Notice the addition of the “wwan” section, appended to the file. This should include the information for your network, including the static ip address you would like assigned to your smart plug. Also, the
netmask should match your network setup.
Once complete, you will need to restart the switch for the settings to take effect. You can restart the smart plug by executing the following command:
How to turn the smart plug on and off
Turning the smart plug on and off is simple and can be done from the command prompt:
Turning the smart plug off
# echo 0 > /sys/class/leds/tp-link:blue:relay/brightness
Turning the smart plug on
# echo 1 > /sys/class/leds/tp-link:blue:relay/brightness
Script to automatically restart
The next step is to put the above commands into a script to do the following:
- Turn the smart plug off
- Wait 15 seconds
- Turn the smart plug on
Ultimately this will run from a web page, so I created a new directory under the
/www directory called cgi-bin, by executing the following command:
# mkdir /www/cgi-bin
Next, create a script, in my case, I called it
restart.cgi by doing the following:
# vi /www/cgi-bin/restart.cgi
restart.cgi to contain the following:
#!/bin/sh echo "Content-Type: text/html" echo "" RELAY_CTRL=/sys/class/leds/tp-link:blue:relay/brightness echo 0 > $RELAY_CTRL sleep 15 echo 1 > $RELAY_CTRL echo '<br><br>' echo '<HTML><body><h1><center>MyNetwork has been Reset</center></h1></HTML>'
Finally, you should make this executable. In my case, I trust this network and am not overly worried about security, so I will make this executable by everyone (assuming that if they are logged into the Smart Plug, I’m already in trouble). Make
restart.cgi executable by doing the following:
# chmod a+x /www/cgi-bin/restart.cgi
You can test whether the script works or not by executing the following from the shell:
If the script is working correctly, you should see the red light turn off, wait 15 seconds, and see the red light turn on.
Create web page front in for restart
The smart plug already has a web server running. All we need to do is create an index.html file which can call our script. This can be done with the following:
# vi /www/index.html
Make the contents of the following match:
<HTML> <HEAD><TITLE>MyNetwork Restart</TITLE></HEAD> <BODY><center> <br><br><h1> To Restart the Newtork, click <a href=cgi-bin/restart.cgi>here</a></h1> </center></BODY> </HTML>
Now, to test the script, simply log on to your web browser and navigate to http://192.168.2.12 (or the static IP address you specified up above). You should see something like the following:
If you click the “here” link, you should see your plug reset (after a 15 second power off)
Restart Network Services
If you are using the smart plug to restart your network, you have a little bit of a problem. Once the network restarts, your smart plug will lose its connection to the network. To fix that, you will need to create a script to perform a delayed network restart.
You can create the script using the following command:
# vi /root/scripts/delayed_network_restart
And make the contents of your script:
#!/bin/sh sleep 120 /sbin/ifdown wwan sleep 15 /sbin/ifup wwan
This will wait 2 minutes and restart the network services, this should give enough time for the network to power up and settle. You can extend this time as necessary.
Finally, make the script executable:
# chmod a+x /root/scripts/delayed_network_restart
Now we need to add a call to our network_restart script to our restart.cgi file. Modify the cgi script to add the call to our restart script immediately after turning on the power to the plug. The modified script should look as follows:
#!/bin/sh echo "Content-Type: text/html" echo "" RELAY_CTRL=/sys/class/leds/tp-link:blue:relay/brightness echo 0 > $RELAY_CTRL sleep 15 echo 1 > $RELAY_CTRL /root/scripts/delayed_network_restart echo '<br><br>' echo '<HTML><body><h1><center>MyNetwork has been Reset</center></h1></HTML>'
Force the smart plug on at startup
As a final note, I wanted to make sure that if the power ever went out, the smart plug would be forced on at startup. Typically, the smart plug should resume whichever state it was in prior to the power loss, but I wanted to force this. Additionally, I need the networking components to restart after the initial power up (once the network is established)
In testing, I found that the restoring of the power state was the last thing done in startup, even after the rc.local was allowed to complete, so the work-around was a two step process:
- Create an executable script to run 30 seconds after launch
- Run the script at startup
First, create a scripts directory under the root user
# mkdir /root/scripts
Next, create the following file:
# vi /root/scripts/power_on
With the contents:
#!/bin/sh sleep 30 echo 1 > /sys/class/leds/tp-link:blue:relay/brightness /root/scripts/delayed_network_restart
By forcing the script to sleep 30 seconds before starting, you will ensure that the smart plug has time to complete its initialization procedure and place the plug in whichever state it thinks in should be in. Also, we call the delayed_network_restart script that we created earlier, to run after the initial boot.
Make it executable
# chmod a+x /root/scripts/power_on
Run the Script at startup
# vi /etc/rc.local
Add the following line before the
How to Automatically restart network when connection lost
So, what if you want the network to automatically restart if you sense a loss in connection. Let’s make some modifications to the file above.
Doing so will involve a few steps:
- Write a script to detect if we have an internet connection
- If no connection is present, restart the network
- Add the script to the crontab scheduler to run on a schedule (for example, every 5 minutes)
Write a script to detect an internet connection
First, we need to choose a good reliable server to “ping” to test whether or not we have a connection. For this, I will use the google dns server located at 188.8.131.52, although you can choose any other known good server. Once concern is that as setup above, the smart plug does not have dns enabled, so we should ping by ip address, instead of host name.
General Connection Detection
A simple bash script for detecting whether or not we have a network connection looks like:
#!/bin/bash SERVER=184.108.40.206 ping -c 3 $SERVER > /dev/null 2>&1 if [ $? -ne 0 ] then echo "No connection found" else echo "Connection found" fi
The IP address of the server we will check a connection to is specified at the top. In this case, it is one of dns servers for google. I am assuming that if I cannot reach google’s dns server, it is a problem with my connection, not theirs. (I pay far less for my internet connection!)
Basically, this script will execute a
ping command, and returns a value of 0 when a ping is successful (as the return status of the command). The output of the ping command is discarded to /dev/null. The
-c option specifies that 3
pings should be sent (in case there is a problem and one is lost).
Modify the script to restart the network if no connection is present
Now, using a combination of the scripts from before, we can create a new script that will run a test (once) and restart the network if no connection is found. In my case, it is stored in the
/root/scripts folder. Create the file with the following command
# vi /root/scripts/connection_check.sh
Make the contents of the file:
#!/bin/bash SERVER=220.127.116.11 RELAY_CTRL=/sys/class/leds/tp-link:blue:relay/brightness ping -c 3 $SERVER > /dev/null 2>&1 if [ $? -ne 0 ] then echo 0 > $RELAY_CTRL sleep 15 echo 1 > $RELAY_CTRL /root/scripts/delayed_network_restart.sh fi
And finally, make it executable with the command:
# chmod a+x /root/scripts/connection_check.sh
Add the script to crontab
The final step is to have the script automatically run at set intervals. In my case, I have decided to check for an internet connection every 5 minutes. If the connection has gone away, it will reset the network.
The following line needs added to root’s crontab:
*/5 * * * * sh /root/scripts/connection_check.sh
This will run
/root/scripts/connection_check.sh every 5 minutes. You can edit the crontab with the following command:
# crontab -e
And add the above line, as you normally would using
Ensure that cron is running on startup
During testing, I noticed that cron does not appear to start correctly at startup. The easiest fix for this was to simply add the cron initialization to the rc.local file. If it is already running, this should not be a problem.
Simply, modify the rc.local file using the following command:
# vi /etc/rc.local
And add the following line prior to the
exit 0 command:
This will restart the cron process once the system is up and running.
rc.local file will look like this:
# Put your custom commands here that should be executed once # the system init finished. By default this file does nothing. sleep 5 /sbin/kkeps_on & /sbin/kkeps_off & /sbin/kkeps_seekwifi & /sbin/kkeps_reconect & /root/scripts/power_on & /etc/init.d/cron restart exit 0
That’s it. Once you have completed and tested these steps, you can plug your network components into this plug (likely using a power strip) Just make sure that the total amount of items plugged in to the smart plug do not exceed the 10A rating of the plug singulair pill. Most networking components should be well within this range. And that is how you make a KanKun smart plug power cycle your network components.
With the two scripts shown above, you should be able to either restart the smart plug on command, or have it automatically restart when it detects that the internet connection has been lost.