OpenHAB HTTP Binding & The KanKun Wifi Plug

How To Hack This Cheap Chinese Wifi Plug And Control It From OpenHAB

On this page

The KanKun Wifi Plug

While looking for a decent but cheap smart switch, I bought a Kankun Smart Wifi Plug Socket ($23 with Prime shipping on Amazon.com). Turns out the poorly translated instructions are confusing, the app isn't much better, and triggering the switch takes several seconds because it goes via some Chinese cloud server.

The KanKun switch

But as luck would have it, smart people figured out the switch runs OpenWRT, a Linux distribution for embedded devices. You can SSH into it (using MobaXterm, like when you connect to your Pi), and install a script that lets you trigger it directly. Suddenly the response time goes from seconds to nearly instantaneous.

To "hack" it, start by plugging it in an outlet. After 10-20 seconds, you should see a new wifi network called 0K_SP3 show in your computer's wireless settings. Connect to it.

Step 1: connect to the KanKun's Wifi

When you're connected, fire up MobaXterm and start an SSH session to IP 192.168.10.253 as root:

Step 2: SSH session

When it asks for a password, try p9z34c. If that doesn't work, it might be admin or 1234. Your session will look like this:

Step 3: log in as root

Now we need to tell the switch how to connect to your wifi network. OpenWRT doesn't have nano, so you'll need to edit files using vi.

vi /etc/config/wireless
Vi is a pain in the ass to work with, so here's all you need to know to get through this:
  • When you start, vi is in command mode. So don't type anything except i to enter edit mode.
  • Make the changes like in any text editor
  • Then go back to command mode by pressing the ESC key, and type :wq. This will save the file and exit vi.
Have the file look like this screenshot, where you enter your own wifi's SSID and password where I blanked out mine. You can leave the top section unchanged:

Step 4: edit the wireless settings

The second file you need to edit is where you tell the switch to connect over wifi. You can also assign a static IP address (in this case, I chose 192.168.1.70). The only thing that's different from the default is the bottom section I added:

vi /etc/config/network

Step 5: edit the network settings

Finally, reboot.

reboot
If your computer doesn't automatically reconnect to your home wifi you can do that now. When the plug is back up you should be able to reconnect over SSH using the IP you gave it. If it doesn't work, you can reset to factory settings and start all over by keeping the button pressed for 4-5 seconds.

Optional but recommended: change the password

passwd

Now we need install a script called relay.cgi that lets you control the switch over http. We'll install it in the /www/cgi-bin folder:

mkdir /www/cgi-bin

To copy over the file, I used WinSCP. It's an FTP client that also supports the "SCP" protocol, and it's free.

Now back in MobaXTerm, make the file executable:

chmod +x /www/cgi-bin/relay.cgi

And that's it! You can exit WinSCP and MobaXterm - from now on you can command the switch using your browser. Try these URLs:

  • http://192.168.1.70/cgi-bin/relay.cgi?on
  • http://192.168.1.70/cgi-bin/relay.cgi?off

Optional: There's also a JSON version of the script, if you prefer that.

Not optional: Take a few seconds to realize you just logged into your outlet to install a web script.

Install The HTTP Binding

If you've been following guides on this site, you already have the http binding in the addons_repo folder. Copy the addon from addons_repo to addons (remember to replace 1.7.0 with the version you have):

sudo cp /opt/openhab/addons_repo/org.openhab.binding.http-1.7.0.jar /opt/openhab/addons/org.openhab.binding.http-1.7.0.jar

If you don't have the addons_repo folder, you can get the binding by downloading the addons zip from the OpenHAB downloads page.

There's no need to edit openhab.cfg for this binding.

Configuring A Switch In OpenHAB

In the default.items file, you configure the switch with an HTTP binding by telling it what the URLs are for turning it on and off (change 192.168.1.70 to your switch's IP address):

Switch	KanKun1 "KanKun" (GF_Living) { http=">[ON:GET:http://192.168.1.70/cgi-bin/relay.cgi?on] >[OFF:GET:http://192.168.1.70/cgi-bin/relay.cgi?off]" }

Adding a switch to the default.sitemap is very simple:

Frame label="Switches" {
	Switch item=KanKun1
}

The KanKun switch in OpenHAB
Your switch in OpenHAB. Click the right part to switch between states.

Go Back

Guides