TP-Link/Kasa Bulbs and Plugs Control (Old, Unofficial Integration)

Dave, I installed Server Ultimate on my Note 4. When start the server it is throwing an error on script:

2017-07-13 12:50:03 [Carlos] - at node.js:965:3
2017-07-13 12:50:03 [Carlos] - at startup (node.js:151:18)
2017-07-13 12:50:03 [Carlos] - at Function.Module.runMain (module.js:451:10)
2017-07-13 12:50:03 [Carlos] - at Function.Module._load (module.js:314:12)
2017-07-13 12:50:03 [Carlos] - at Module.load (module.js:357:32)
2017-07-13 12:50:03 [Carlos] - at Object.Module._extensionsā€¦js (module.js:426:10)
2017-07-13 12:50:03 [Carlos] - at Module._compile (module.js:387:25)
2017-07-13 12:50:03 [Carlos] - at exports.runInThisContext (vm.js:53:16)
2017-07-13 12:50:03 [Carlos] - SyntaxError: Unexpected token <
2017-07-13 12:50:03 [Carlos] - ^
2017-07-13 12:50:03 [Carlos] - !DOCTYPE html (Removed <> on long to show here)
2017-07-13 12:50:03 [Carlos] - /storage/emulated/0/Download/TP-LinkServer_oldNode.js:7
2017-07-13 12:50:03 [Carlos] - Got PID '2306ā€™
2017-07-13 12:50:02 [Carlos] - Started Node.js server.
2017-07-13 12:50:02 [Carlos] - Listening for connections.
2017-07-13 12:50:02 [Carlos] - Node.js native loading.
2017-07-13 12:50:02 [Carlos] - Using pie.
2017-07-13 12:50:02 [Carlos] - Preparing.
2017-07-13 12:50:02 [Carlos] - Server started
2017-07-13 12:50:02 [Carlos] - Starting server
2017-07-13 12:50:02 [Carlos] - Request has been send to servers pack
2017-07-13 12:49:50 [power] - Power connectedā€¦
2017-07-13 12:49:50 [wifi] - Wifi connected to network "
******ā€¦
2017-07-13 12:49:49 [system] - Service startedā€¦
2017-07-13 12:49:49 [system] - No cronjobs added.
2017-07-13 12:49:49 [system] - Checking cronjobs.
2017-07-13 12:49:49 [system] - No servers clean started.
2017-07-13 12:49:49 [system] - Checking for clean start serversā€¦
2017-07-13 12:49:49 [system] - Wifi lock setā€¦
2017-07-13 12:49:49 [system] - Keep alive lock setā€¦
2017-07-13 12:49:49 [system] - Service startingā€¦

Which device handler and I will see if I can duplicate.

Iā€™m using this one. TP-Link_LB120_Emeter.groovy.

Thanks.

Loaded on a Android Samsung S3 phone. Ran all commands using the latest device handler on GitHub. No errors.

Some differences on startup: My server says ā€œUsing no pieā€

Probably need more data on the line !DOCTYPE html. I do not see this (and have never seen this). may be related to ā€˜pieā€™

Below is first part of log:

2017-07-13 01:04:22 [test] - Command Response sent to SmartThings
2017-07-13 01:04:22 [test] - deviceCommand sending to IP: 192.168.0.100 Command: {ā€œsmartlife.iot.common.emeterā€:{ā€œget_realtimeā€:{}}}
2017-07-13 01:04:22 [test] - IP: 192.168.0.100 sent command deviceCommand
2017-07-13 01:04:22 [test] - Thu Jul 13 2017 13:04:22 GMT-0400 (EDT)
2017-07-13 01:04:22 [test] - Command Response sent to SmartThings
2017-07-13 01:04:07 [test] - Request has been send to servers pack
2017-07-13 01:04:22 [test] - deviceCommand sending to IP: 192.168.0.100 Command: {ā€œsmartlife.iot.smartbulb.lightingserviceā€:{ā€œtransition_light_stateā€:{ā€œon_offā€:0}}}
2017-07-13 01:04:22 [test] - IP: 192.168.0.100 sent command deviceCommand
2017-07-13 01:04:22 [test] - Thu Jul 13 2017 13:04:22 GMT-0400 (EDT)
2017-07-13 01:04:09 [test] - TP-Link Hub Console Log
2017-07-13 01:04:08 [test] - Got PID '10629ā€™
2017-07-13 01:04:08 [test] - Started Node.js server.
2017-07-13 01:04:08 [test] - Listening for connections.
2017-07-13 01:04:08 [test] - Node.js native loading.
2017-07-13 01:04:08 [test] - Using no pie.
2017-07-13 01:04:08 [test] - Preparing.
2017-07-13 01:04:08 [test] - Server started
2017-07-13 01:04:08 [test] - Starting server
2017-07-13 01:04:07 [test] - Request has been send to servers pack

Carlos, Back to our original topic. You can install android aps on fire tv using app referenced in the below video (the app is in the amazpn app store).

Dave, I think I found the issue. I was looking at ST logs and my hub was offline at the time I was testing it. I believe I had an internet outage and my smartphone switch to 4G network. As I was outside my home network it throw the error.

When I get back home Iā€™ll try again.

Thanks!!

Jimi, See the thread near the end. I found a way to install the node.js and script on an Android device. Free trial, costs US$9. (Server Ultimate at playstore). Requires special script file for ā€˜oldNodeā€™ now on the github site.

Look into PM2 or Forever, both services designed to keep node scripts running. You install them via npm, then they have a special command to install themselves with whatever autostart service your platform uses.

Iā€™ve got PM2 running on my ubuntu box (I already had a media server, so sticking a half-dozen node scripts on it was a no-brainer).

A quick internet search should point you in the right direction.

1 Like

Guys,
Sorry if im stating the obvious, but am I right to assume that lb130 bulbs dont connect directly with the SmartThings hub (much like osrams ans wemos)?
Thanks
Chris

Probable Hub-less (i.e. no bridge) TP-Link Integration within two weeks.

Great question. For now that is true. However, just yesterday we found the golden key. I have started to create a smart app and device handler that will allow direct connect of the 130 to the hub. Probably a 95% chance of success (I already have a proof of concept working). Several weeks at worst.

Dave

Very impressed Dave. What would we do without you!

You would party! A loved one on your arm, great scotch, great music and dancing. When bored, good conversation!

Iā€™m looking to write LAN code for another device if I can, is there anything early you can share about making direct connections work?

Several items:

a. The device must use HTTP communications (vice TCP and UDP) to work. Otherwise, you will need a bridge device that translates a HTTP command to the TCP or UDP supported. (SmartThings does not support UDP/TCP on the LAN.)

b. If your device can not be discovered (i.e., support Universal Plug-n-Play discovery), you will need to make it static (i.e., always the same IP Address (done on your router). You will also need to provide preferences page to enter the IP address.

c. If the device uses security tokens, you will need a way to obtain / update the token from the deviceā€™s cloud site.

d. If you do not have a good API document for your device, I strongly recommend installing node.js on your PC and experimenting with commands and returns. Doing this work through SmartThings is time-consuming.

e. Search on GitHub (www.gitjub.com) for your device / model. There is a lot of work there that may have some API information.

What device are you thinking of doing?

OK, so a bridge will still be required for direct UDP then.

I was hoping your ā€œgolden keyā€ might have been a particular syntax that unlocked the somewhat elusive direct communications that weā€™ve all been waiting for!

Sorry, the golden key is a way to access the TP-Link cloud. (I would actually prefer not going through the cloud.) Allows me to bypass a bridge.

For me, I do not care; however, there are a set of users that do not want to have to set up a bridge. That is why I go forward.

Good to know. Time to see if I can work out something based on what Iā€™ve already seen. Some people seem to have had some success with TCP, so I guess copying and reproducing that is the first step. Iā€™ll post if I make any progress as Iā€™m pretty sure the HS100 (which I have, hence posting here) has a direct TCP interface which could then use whatever I find.

What I am working on is for the HS100 (among other TP-Link devices). It will not require a bridge/hub. It will go directly through the TP-Link cloud interface to control the devices. I have a BETA for the HS100 at

It has instructions on how to set up; however, when I am done, there will be a smart app that will gather the data, install the devices, and manage the token state.

As far as TCP - if you find a way for it to work, tell me. I have spent hours trying to get it to work for the TP-Link Bulbs. (You will need the encryption/decryption algorithm for the TP-Link data in Groovy usable format (not the same as node.js). This one works.

//-- outputXOR. XOR (encrypt) the outbound command. ------------------------------------------------
private outputXOR(command) {
def str = ""
def encrCmd = ā€œ000000ā€ + Integer.toHexString(command.length())
def key = 0xAB
for (int i = 0; i < command.length(); i++) {
str = (command.charAt(i) as byte) ^ key
key = str
encrCmd += Integer.toHexString(str)
}
return encrCmd
}
//-- inputXOR. XOR (decrypt) the inbound response. ------------------------------------------------
private inputXOR(encrResponse) {
def cmdResponse = ""
def key = 0x2B
def nextKey
byte[] XORtemp1
for (int i = 0; i <encrResponse.length(); i++) {
nextKey = encrResponse.charAt(i) as byte
XORtemp1 = nextKey ^ key
key = nextKey
cmdResponse += new String(XORtemp1)
}
return cmdResponse
}

1 Like

No luck sadly. Iā€™m not surprised. Iā€™ve ordered a Pi Zero to run a bridge on. Once I get your TP-link stuff set up on it then Iā€™ll look to re-use/extend it for the Broadlink RM pro which is my other main use case.

There is a lot of node.js to broadlink already done. Just search for ā€œbroadlinkā€. My preferred method is to have the commands syntax in the device handler (groovy) and using the hub simply as a bridge. You may use any/all of my code as you desire. Good luck.