Powered by coANDco UK

How to connect Gravio to TP Smart Plugs via API

From How2s

  • First, you need to connect your TP Smart Plug to your Wifi
  • Follow the instructions on the KASA App
  • You now need to obtain a few tokens and IDs to hook it up:

Post

Header Content-Type: application/json
{
 "method": "login",
 "params": {
 "appType": "Kasa_Android",
 "cloudUserName": "XXXXX",
 "cloudPassword": "XXXXX",
 "terminalUUID": "MY_UUID_v4"
 }
}

to

https://wap.tplinkcloud.com

whereby the terminalUUID can be a random UUID obtained from https://www.uuidgenerator.net/version4

You should get:

{
 "error_code": 0,
 "result": {
 "regTime": "2017-01-06 08:42:35",
 "email": "XXXXX",
 "token": "YOUR_TOKEN_HERE"
 }
}

To get the devices, post:

{"method":"getDeviceList"}

to

https://wap.tplinkcloud.com?token=YOURTOKEN

You should get

{
 "result" : {
 "deviceList" : [
 {
 "appServerUrl" : "https://eu-wap.tplinkcloud.com",
 "isSameRegion" : true,
 "deviceMac" : "",
 "status" : 1,
 "hwId" : "XXXX....",
 "deviceId" : "XXXX....",
 "oemId" : "XXXX....",

...

 "deviceType" : "IOT.SMARTPLUGSWITCH",
 "alias" : "My Smart Plug",
 "fwId" : "...",
 "deviceName" : "Wi-Fi Smart Plug",
 "deviceHwVer" : "1.0",
 "role" : 0,
 "deviceModel" : "HS100(EU)"
 }
 ]
 },
 "error_code" : 0
}

You will need *appServerUrl* and *deviceId*.

Now use this token, the URL and the device id and device ids to interact:


POST https://eu-wap.tplinkcloud.com/token=YOURTOKEN&appName=Kasa_Android&termID=YOURTERMID&appVer=1.4.4.607&ospf=Android+6.0.1&netType=wifi&locale=en_US HTTP/1.1
Content-Type: application/json
User-Agent: Dalvik/2.1.0 (Linux; U; Android 6.0.1; XXPhoneModelXX)
Connection: Keep-Alive 
Content-Length: 160
Host: eu-wap.tplinkcloud.com
{
 "method":"passthrough",
 "params":{
 "deviceId":"YOURDEVICEID",
 "requestData":"{\"system\":{\"set_relay_state\":{\"state\":1}}}"
 }
}


A sample request to turn of the switch looks like this:

"{\"method\":\"passthrough\",\"params\":{\"deviceId\":\"8006AF8CBB2D3F0710775B5EC7FD1B141AAE7156\",\"requestData\":\"{\\\"system\\\":{\\\"set_relay_state\\\":{\\\"state\\\":0}}}\"}}"

note the triple escaped ".

Please check out our sponsor, thanks to whom How2s.org is FREE: