Click here to Skip to main content
14,575,900 members

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

Rate this:
5.00 (4 votes)
Please Sign up or sign in to vote.
5.00 (4 votes)
19 Jul 2019CPOL
Activating and deactivating a smart socket from Windows

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.

History

  • 19th July, 2019: Initial version

License

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

Share

About the Author

Joan M
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

 
QuestionError stating device offline Pin
Member 1098357222-May-20 4:30
MemberMember 1098357222-May-20 4:30 
QuestionNon cloud version Pin
FlorentSteiner17-Jul-19 23:57
MemberFlorentSteiner17-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.

Tip/Trick
Posted 17 Jul 2019

Tagged as

Stats

5.6K views
5 bookmarked