PLUGIN: HttpSwitch (for Cheap WiFi Switches like KK-SR3)

HttpSwitch Plugin … or HTTP-triggered switch

Introduction

This plugin allows to control cheap (15US$ or so) WiFi Based Switches like the Kankun Wifi Smart plug
based on HTTP-GET calls and JSON Responses
i will try to make it more flexible in matters of variables and url-calls, right now the urls are hardcoded and so are the expected responses

  • the plugin will ping the switch to find out its online [urn:mios-nullx8-com:serviceId:HttpSwitch1:PingStatus]
  • the poll will also get the signal level and display it on the dashboard [urn:mios-nullx8-com:serviceId:HttpSwitch1:LinkStatus]

Compatible with

  • Kankun Wifi Smart plug http://goo.gl/Nqivgt (need to be “hacked”, done in 4 small steps … info in the Plugin zip under _Hacks)

Requirements

  • UI5 only: akb-json need to be on the vera to parse json responses (use I_HttpSwitch1_UI5.xml)
  • a few braincells, 5 minutes of your life, 1 or 2 cup of Coffee (or Red-Bull)

Installation

  • download and upload the files in the zip file. (only the 4 files in the root, files in subfolders are not required on the vera)
  • mios apps.mios.com or in “Apps->Install apps” by searching “httpswitch”.

after reload … make sure you set an ip in the field “address” … and reload again!

Usage

settings:

  • address = ip adress of the switch
  • poll = time interval for the switch being polled

1/ from the dashboard
click ON or OFF.

1/ via luup

luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = 1}, <idPlugin>)

History

[ul][li]1.13 24 Aug 2015

  • added some reference of a website about hte “hack”
  • Zip file here to download the json.cgi and some bad documentation on how to do it
    [/li][li]1.13 15 Aug 2015
  • private json decode function to avoid json lib errors thanks to cybrmage for this code sniplet
    [/li][li]1.1 14 Aug 2015
  • added UI5 Implementation file (with some reason startup Lua can not detect the version)
  • on UI7 loads the default json lib (still required akb-json on UI5)
    [/li][li]1.0 10 Aug 2015
  • no ip warning
  • added to store[/li]
    [li]0.5 9 Aug 2015
  • ping the Switch to ensure its online (skip polling if no ping reply much faster than waiting the wget to timeout)[/li]
    [li]0.3 3 Aug 2015
  • show wifi signal of the switch[/li]
    [li]0.21 3 Aug 2015
  • read json responses (requiress now ‘akb-json’)[/li]
    [li]0.1 26 Jul 2015
  • Init (just on off … no status)[/li][/ul]

ToDo

[ul][li]use of default IP-Field instead of custom field (dont know how to do that yet)[/li]
[li]custom URI[/li]
[li]custom responses (maybe a set of possible responses or response relations in variable … no idea yet)[/li]
[li]offline Warning[/li]
[li]multiple devices[/li]
[li]small format alignment errors in Device Settings - UI5[/li]
[li]small alignment errors in main Screen - UI7[/li][/ul]

Interesting device, so how safe are these?

I found a youtube clip in Russian (I think) where it is opened and analyzed but the video is out of focus most of the time and I cant understand the language.

I have serval of them
2 on my ip cameras (so i can “restart” them) 99,99% of the time they turned on
Another one on the coffee maker, no issues yet

Are these things available for Canada/US plugs?

yeah but they more expensive for no reason http://goo.gl/ApmBvp
if you order a chinese one … you get an adapter for free

yeah but they more expensive for no reason http://goo.gl/ApmBvp
if you order a chinese one … you get an adapter for free[/quote]

Marketing :slight_smile:

At a quick glance it looks OK but it appears that it would block the second outlet on a standard North American Duplex outlet.

i might buy one of the ‘universal’ versions mostly top play with the Plug In. BTW, is the source for the plug in available? I’ve never written a plug in and mostly i’m curious about structure and coding.

its not encrypted … GPL License …
its all clear text in the zip file

Could your plugin be modified so it can read status/control openhab switches, which can also be controlled/read with REST calls?
This would make an excellent link from Vera to Openhab (and not vice versa as existing today via an openhab binding)

[quote=“stefaanbolle, post:8, topic:188358”]Could your plugin be modified so it can read status/control openhab switches, which can also be controlled/read with REST calls?
This would make an excellent link from Vera to Openhab (and not vice versa as existing today via an openhab binding)[/quote]
thats a good idea … i played with openHAB … but iam allergic to Java (unless its in my Cup)
i made something semilar already as vera controller … (php based)

i’ll install openHAB somewhere to test …
give me a few days :wink:

with some reason in startupLua luup.version does not work
until i find out how to check for the lua version i added a “I_HttpSwitch1_UI5.xml” to use on UI5 devices with akb-json

First Post Updated

If you move your application code out of the implementation file (I_HttpSwitch1.xml) into it’s own lua file (ie: L_HttpSwitch1.lua), the startup code will have access to the luup.version_branch, luup.version_major and luup.version_minor system variables.

Once you move the code to its own file, you need to add a reference to it in the implementation file:

<implementation>
	<files>L_HttpSwitch1.lua</files>
	<startup>main</startup>
	<actionlist>
		-- add actions here
	</actionlist>
<implementation>

Really? I did not know that make much difference.
Made me gray hairs last night about luup.version

that (and the version about table check you suggested in the json thread)
results in decode error

LuaInterface::CallFunction_Job device 245 function SHttpSwitch1_HttpSwitch1_On_job failed [string "..."]:69: attempt to call field 'decode' (a nil value) 

only on UI5
works perfectly on UI7

if i “just” do endecoder = require("akb-json")

everything works without change any code

the code it fails is really simple

		local status, result = luup.inet.wget(url,6)
		
		if status == 0 then
			local data = endecoder.decode(result)
			local PowerState = 	data['state'];
			luup.variable_set(HS_SID, "Status", PowerState, parentDevice)
			luup.variable_set("urn:upnp-org:serviceId:SwitchPower1", "Status", PowerState, parentDevice)

......

confused

[quote=“nullx8, post:13, topic:188358”]that (and the version about table check you suggested in the json thread)
results in decode error[/quote]

That is really strange… Both the EVL3 plugin and the Wink Connect plugin check the luup.version_[bulid,major,minor] variables in the startup code using a seperate lua file…

If you don’t need to encode json, use the decode_json function that I posted in the json thread… It’s small, self contained, and does not require any external libraries…

If you need to encode json, let me know… I have a compact, self contained encode function too…

No encoding, i simply read status informations, all “put” is straight http-get hardcoded

I give you all the time nullx8. Great that you want to invest time in controlling openhab from vera!
Big thanks

Sent from my iPhone using Tapatalk

thanks for that, works like a charm on UI5 and UI7 …
updated Zip and mios.com already

thanks again

I’ve ordered two switches I found on eBay that appear to be identical

http://www.ebay.com/itm/181692367853?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

Are there any recent updates to the PlugIn? Is it on the App Store or do you plan to put it there?

They apear to be the same, just dont get the Circle ones, they cheaper but lack the wifi repeater feature and they cant (yet) hacked

I made a few changes (added a ping check, so the vera wont try to get the status if the ping fails, makes things alot more stable) and no more UI5 file :slight_smile: