Click here to Skip to main content
15,885,546 members
Articles / Operating Systems / Windows
Tip/Trick

Control a tp-link Smart Plug from Windows Without Extra Programs

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
28 May 2022CPOL6 min read 30.9K   6   24
Activating and deactivating a smart socket from Windows
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:

BAT
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:

BAT
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:

BAT
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:

BAT
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:

BAT
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:

BAT
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer robotecnik
Spain Spain
I'm from Catalonia (in Spain) and a specialist in CNC, PLC, robotics and automation in general.

From 1998 I've been developing software applications for the automation market.

I'm using different technologies in each case to get the proper result, fieldbus communications, special electronics, special laser sensors, artificial vision, robot arms, CNC applications, PLC's...

www.robotecnik.com[^] - robots, CNC and PLC programming

Comments and Discussions

 
Question{"error_code":-20571,"msg":"Device is offline"} Pin
Alexandre1722018-Apr-24 21:47
Alexandre1722018-Apr-24 21:47 
Question{"error_code":-20571,"msg":"Device is offline"} Pin
amilo12318-Dec-23 21:09
amilo12318-Dec-23 21:09 
AnswerRe: {"error_code":-20571,"msg":"Device is offline"} Pin
Joan M19-Dec-23 7:59
professionalJoan M19-Dec-23 7:59 
QuestiongetDeviceList list returning the list of devices, but status: "0" and then set_relay_state returns {"error_code":-20571,"msg":"Device is offline"} Pin
vllanso29-Mar-23 1:52
vllanso29-Mar-23 1:52 
Questionno longer work, probably hasnt for a while - {"error_code":-20571,"msg":"Device is offline"} Pin
stylemessiah26-Nov-22 12:49
stylemessiah26-Nov-22 12:49 
AnswerRe: no longer work, probably hasnt for a while - {"error_code":-20571,"msg":"Device is offline"} Pin
Joan M26-Nov-22 22:53
professionalJoan M26-Nov-22 22:53 
GeneralRe: no longer work, probably hasnt for a while - {"error_code":-20571,"msg":"Device is offline"} Pin
stylemessiah27-Nov-22 12:44
stylemessiah27-Nov-22 12:44 
GeneralRe: no longer work, probably hasnt for a while - {"error_code":-20571,"msg":"Device is offline"} Pin
Joan M27-Nov-22 16:29
professionalJoan M27-Nov-22 16:29 
GeneralRe: no longer work, probably hasnt for a while - {"error_code":-20571,"msg":"Device is offline"} Pin
stylemessiah28-Nov-22 0:38
stylemessiah28-Nov-22 0:38 
GeneralRe: no longer work, probably hasnt for a while - {"error_code":-20571,"msg":"Device is offline"} Pin
Joan M18-Dec-23 20:02
professionalJoan M18-Dec-23 20:02 
AnswerHad to use the US server to make this work. Pin
Member 1528405010-Jul-21 18:38
Member 1528405010-Jul-21 18:38 
GeneralRe: Had to use the US server to make this work. Pin
Tim Rude7-Nov-21 9:50
Tim Rude7-Nov-21 9:50 
GeneralRe: Had to use the US server to make this work. Pin
Joan M28-May-22 1:35
professionalJoan M28-May-22 1:35 
Question{"error_code":-20571,"msg":"Device is offline"} Any idea why I'm seeing this? Pin
Member 1510276216-Mar-21 12:28
Member 1510276216-Mar-21 12:28 
AnswerRe: {"error_code":-20571,"msg":"Device is offline"} Any idea why I'm seeing this? Pin
Jason Case16-Mar-21 17:29
Jason Case16-Mar-21 17:29 
GeneralRe: {"error_code":-20571,"msg":"Device is offline"} Any idea why I'm seeing this? Pin
speed-of-heat19-Apr-21 9:16
speed-of-heat19-Apr-21 9:16 
AnswerRe: {"error_code":-20571,"msg":"Device is offline"} Any idea why I'm seeing this? Pin
Member 145266924-Jun-21 16:23
Member 145266924-Jun-21 16:23 
AnswerRe: {"error_code":-20571,"msg":"Device is offline"} Any idea why I'm seeing this? Pin
Member 1525810022-Jun-21 5:57
Member 1525810022-Jun-21 5:57 
AnswerRe: {"error_code":-20571,"msg":"Device is offline"} Any idea why I'm seeing this? Pin
Member 1528405010-Jul-21 17:03
Member 1528405010-Jul-21 17:03 
AnswerRe: {"error_code":-20571,"msg":"Device is offline"} Any idea why I'm seeing this? Pin
Joan M28-May-22 1:36
professionalJoan M28-May-22 1:36 
Never faced this issue, maybe it's because you are from outside Europe.
Check the other URLs to be used in case you are in the US.
I've updated the tip accordingly.
Plus added a more automated version of the script.
www.robotecnik.com[^] - robots, CNC and PLC programming

QuestionError stating device offline Pin
Member 1098357222-May-20 4:30
Member 1098357222-May-20 4:30 
AnswerRe: Error stating device offline Pin
Joan M28-May-22 1:37
professionalJoan M28-May-22 1:37 
QuestionNon cloud version Pin
FlorentSteiner17-Jul-19 23:57
FlorentSteiner17-Jul-19 23:57 
AnswerRe: Non cloud version Pin
Joan M18-Jul-19 4:32
professionalJoan M18-Jul-19 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.