This article discusses how to control a tp-link Smart Plug from Windows without using any extra programs.
Introduction
I got a set of wonderful loudspeakers that came with a drawback... when they are standby, they automatically accept any bluetooth pairing... this means that, at any time, any neighbor in range can connect to the speakers and activate them at full volume.
As the first time this happened has been after the warranty period, I've searched for an external solution.
I'm using those loudspeakers at my workstation so they will be ON only when I'm in front of the computer.
I had a tp-link smart socket HS100 in the office... but, even though it would have been very convenient, I did not want to use a Google assistant to activate/deactivate the loudspeakers... Neither did I want to use the cell phone application to control the socket.
That left me with a wish: controlling the socket from the computer... one batch file to start it, one batch file to stop it...
All the scripts have been modified from the ones that appear on this page.
The scripts in that page perhaps work with Linux, but they don't in Windows 10, what I've done is adapt the syntax to the Windows needs.
So thank you very much https://itnerd.space/.
Background
If you are using an older version of Windows 10 or the curl
command is not working on your machine, you can always download a Linux port of the command to your computer.
In order to connect to the tp-link cloud servers, we will be using POST
, therefore curl or any similar software is needed.
In order to be able to follow those steps, you will have to configure the socket using the KASA APP in your cell phone, once everything works there, you should be able to use these steps to activate and deactivate the power outlet from your computer.
Using the Code
We will need some data to be able to interact with the smart socket:
Data We Must Know in Advance
cloudPassword
: the password we use to connect to the tp-link devices using the KASA APP cloudUserName
: the user name we use to connect to the tp-link devices using the KASA APP
Data We Create Manually
terminalUUID
: a UUID that we can make in any web around
Data We Get in the First Steps of the Process
Token
: You get it from the user and password, from that point, you can use it to control your sockets. DeviceID
: the device we want to control identifier
Notes
To execute the curl
commands, open a CMD window and simply copy and execute it there.
First, second and third steps must be done only once. After that, you can go directly to the fourth step.
First Step: Getting the UUID
If you don't know how to get a UUID, you can use any web site dedicated to generate them like https://www.uuidgenerator.net/version4.
There, you will automatically get a UUID.
Copy that UUID, you'll need it for the next step.
Each time you refresh the page, you get a new UUID.
Second Step: Getting the Token
We will use curl
to get the token of our user in TP-LINK
:
curl -X POST -H "Content-Type: application/json"
-d "{ \"method\" : \"login\", \"params\" : {\"appType\" : \"Kasa_Android\",
\"cloudPassword\" : \"OUR PASSWORD HERE\", \"cloudUserName\" : \"OUR USER NAME HERE\",
\"terminalUUID\" : \"YOUR JUST ACQUIRED UUID HERE\" }}" https://eu-wap.tplinkcloud.com
As you can see, you'll need to know the username, the password and provide one UUID to get a valid token from TP-LINK servers.
Third Step: Getting the Device ID
Again, curl
is our preferred tool here to require the devices list we have in our TP-LINK
account:
curl -X POST -H "Content-Type: application/json" -d "{ \"method\" : \"getDeviceList\" }"
https://eu-wap.tplinkcloud.com?token=YOUR JUST ACQUIRED TOKEN HERE
As you can see, we are using the valid token we've got from TP-LINK
servers in the second step.
This step will give us an output with all the devices in our house/office... we will have to copy the ID of the device we want to control.
Fourth Step: Activating or Deactivating the Socket
The final step and the objective of this tip/trick:
We can now activate our socket by using this command:
curl -X POST -H "Content-Type: application/json" -d
"{ \"method\" : \"passthrough\", \"params\" : { \"deviceId\" : \"OUR DEVICE ID HERE\",
\"requestData\" : '{ \"system\" : { \"set_relay_state\" : { \"state\" : 1}}}'}}"
https://eu-wap.tplinkcloud.com/?token=OUR TOKEN HERE
The important bit here is the number 1
after the state
clause... this tells the socket to get powered on.
In order to deactivate the socket, we will replace that 1
for a 0
as:
curl -X POST -H "Content-Type: application/json"
-d "{ \"method\" : \"passthrough\", \"params\" : { \"deviceId\" : \"OUR DEVICE ID HERE\",
\"requestData\" : '{ \"system\" : { \"set_relay_state\" : { \"state\" : 0}}}'}}"
https://eu-wap.tplinkcloud.com/?token=OUR TOKEN HERE
Once you've reached this point, you can directly use the commands in this step to control the plug, no need to perform all the steps each time. Steps 1, 2 and 3 are needed only the first time.
Hope this helps you to be able to connect and disconnect devices from your computer.
In my case, I can simply double click an icon on my desktop to get the loudspeakers active and once I stop using the computer, I can deactivate them again, double clicking another desktop icon.
Reducing the Repetitive Work
As far as I have seen while using this, the only thing that gets updated from time to time is your TOKEN.
And to complicate things a little, TPLink sometimes changes the way the token is given to you.
You will have to get all the data following the previous steps from 1 to 4, but, after the first time, you will be able to reduce the amount of work.
I've done this to reduce the amount of times you are forced to update your scripts.
Getting Your Token Automatically
As mentioned before, this token is changing every now and then, and each time this happens, you are forced to follow the steps again from the beginning, not a big deal, but it would be better to avoid needing to do that.
The following script looks for your token automatically given the right parameters:
FOR /F delims^=^"^ tokens^=26 %%G IN ('curl -X POST -H "Content-Type: application/json"
-d "{ \"method\" : \"login\", \"params\" : {\"appType\" : \"Kasa_Android\",
\"cloudPassword\" : \"OUR PASSWORD HERE\", \"cloudUserName\" : \"OUR USER NAME HERE\",
\"terminalUUID\" : \"YOUR JUST ACQUIRED UUID HERE\" }}" https://eu-wap.tplinkcloud.com')
do set obtainedToken=%%G
echo %obtainedToken%
At the moment of writing this tip, the right token to get was the 26th, to check if this is still correct, execute the previous script and check if you get the same token than with the previous version, you will have to modify the 26 number in the script when TPLink changes it again.
Once you know the right number (26) to get the token automatically, you are ready to create a complete script to automate the process:
FOR /F delims^=^"^ tokens^=26 %%G IN ('curl -X POST -H "Content-Type: application/json"
-d "{ \"method\" : \"login\", \"params\" : {\"appType\" : \"Kasa_Android\",
\"cloudPassword\" : \"OUR PASSWORD HERE\", \"cloudUserName\" : \"OUR USER NAME HERE\",
\"terminalUUID\" : \"YOUR JUST ACQUIRED UUID HERE\" }}" https://eu-wap.tplinkcloud.com')
do curl -X POST -H "Content-Type: application/json" -d "{ \"method\" : \"passthrough\",
\"params\" : { \"deviceId\" : \"OUR DEVICE ID HERE\", \"requestData\" : '{ \"system\" :
{ \"set_relay_state\" : { \"state\" : 1}}}'}}" https://eu-wap.tplinkcloud.com/?token=%%G
Using this script will power on the plug for you and will keep working even if your token changes.
Of course, you can do the same to power off your plug.
Extra Notes
Several CodeProject users asked how to be able to use this from the US and not only from Europe.
I have not been able to test it, but it seems you can replace the urls in the scripts from https://eu-wap.tplinkcloud.com to https://use1-wap.tplinkcloud.com or https://wap.tplinkcloud.com to get it working in the US, and https://aps1-wap.tplinkcloud.com to get it working in the Asia Pacific region.
History
- 19th July, 2019: Initial version
- 28th May, 2022: Automate it a little bit more