admin – IT Nerd Space http://itnerd.space Blog about Cloud, Automation, Android, Smart things... Thu, 27 Jul 2017 22:59:31 +0000 en-US hourly 1 https://wordpress.org/?v=4.7.5 https://i2.wp.com/itnerd.space/wp-content/uploads/2016/10/cropped-99789e30b0a6eac11f33246750ca29f9.jpg?fit=32%2C32 admin – IT Nerd Space http://itnerd.space 32 32 133306427 Looking back and Forward: Coming soon to this blog http://itnerd.space/2017/07/27/looking-back-and-forward-coming-soon-to-this-blog/ http://itnerd.space/2017/07/27/looking-back-and-forward-coming-soon-to-this-blog/#respond Thu, 27 Jul 2017 21:19:35 +0000 https://itnerd.space/?p=361 I started this blog writing a series of articles about Azure App Service Architecture, mainly trying to explain and illustrate how I understand it works, beyond what one can find publicly in Microsoft (extensive) documentation. Then I got an HS100 TP-Link smartplug: a “connected thing”, a plug that is connected to Internet (don’t say that to any person, most will look at you like o.0?!), a plug that you can control from an App on your phone, from wherever you are: at home, or from outside: from anywhere in the world. And  I guess Oops I did it again!

Interface the HS100 with everything

I couldn’t resist to imagine many potential applications and possible integrations with online (cloud) services that are publicly and freely available, like IFTTT; I’m also a big fan of Android Tasker, Node-Red, the MQTT pub/sub protocol, and Raspberry Pi / NodeMCU hooked with sensors and hardware pieces attached on the GPIO pins) that make more connected things, so literally ideas are all over the places in my heads, on papers, in Keep notes…

Like this one that just popped in my mind:

Switch off the main lights in the salon, dim the lights around the sofa and TV, power on the media server box (smartplug) and TV when I sit on the sofa, with a NodeMCU controller and a pressure sensor (FSR)! Or maybe I’d prefer to have all that triggered when I power on the TV, but only in a certain time slot?…

Told you I have crasy ideas ;). Totally got to try to do that BTW!

Error: Time not found

What I only lack really is time. First, time to try and make some of those idea work (they do eventually with perseverence and I always learn a lot in the process), then time to write about them (here).

I didn’t realize the time it take to write articles people need to write good articles. I usually take notes of what I do, notes that I understand, but publishing those is no value to anyone. You need to structure the ideas, give them some form so the result make sense for the reader, enough at least so they can be inspired in trying also themselves to do, learn and make something that work for them out of what I wrote. Unfortunately due to the lack of time, I don’t enter into too much details, and I assume the reader is tech-savvy enough to read, understand and try by himself. That means some reader gets frustrated because I missed some parts they didn’t know… Sorry about that.

Wins so far

Back to the HS100: there were some (very good) resources publicly available already out there that showed how to interact with an HS100 plug, from shell scripts based on cURL, php and python libraries or Node.js modules, even a Node-Red. But they all had the same problem: they only worked withing the same local network (WiFi/LAN) the plug was connected to. Ugh bummer.

The Kasa App and the Alexa integration were proof that TP-Link was hosting a cloud service, but no API was publicly available yet.

So I started to work on figuring out how the App talked to the TP-Link Cloud endpoint, and started to publish it here. The series got (modestly) popular among the maker and tinkerers ;), and people used the comments to exchange ideas, issues, and come with solutions. So far, I’ve published (not in chronological order)

So far the whole process was rather hacky and manual I reckon, but still one could achieve the goal of switching a smartplug from a remote place (outside of the smartplug local network), like from one’s smartphone (Tasker), or an IFTTT recipe. It already enabled some cool logics.

Pitfalls: what about scenes?

Soon after having found out how to trigger one TP-Link HS100 smartplug, the most frequently asked question in the comments has been ¿how can I power On/Off multiple devices from ITFFF?

After all it’s a feature that people know from the Kasa App: you define buttons in the app that will put several of you smartplugs in a state you define.

Related to my idea above, a scene would be:

“Time to watch some TV” scene: Switch off the main lights in the salon, dim the lights around the sofa and TV, power on the media server box (smartplug) and TV.

You can define that in Kasa, but what about calling it from IFTTT? You can’t. You can only call one POST call (via Maker), in an IFTTT recipe.

The second to most request in the mind of all is probably related to the TOKEN that will eventually expire sooner of later, making it a hassle to edit all your IFTTT Recipes/Tasker tasks/whatever scripts… manually!

Well, I’m happy to say this was until now!

More Wins coming soon…

This week I’ve been working hard on this (it’s the second time I try it, and this time I made it!).

This is what I have done:

  • I’ve created a node.js module to easily interact (power On/Off, get state) with any number of HS100/HS110 smartplugs and LB100/LB110/LB120 and LB130 smartbulbs (set on/off, set brightness, set color). The module handles the login, token retrieval, and device triggering, in almost no call.
  • I’ve created a Glitch example app in which one can define scenes. As Glitch publishes a URL, you can then trigger one “scene” with only one call (http://url.glich.me/scene1/) and with only one POST call it will do whatever you want to! Not only that but you can even define as many scenes as you want in a single Glitch app ( each with a different end-point: http://url.glich.me/scene2/, http://url.glich.me/scene3/…).
  • Next I will create a library of new TP-Link Node-Red nodes to control the smartthings from Node-Red!

All that is already working fine for me, so this is what I’ll talk about in next posts (and I’ll publish the code for everything 😉 ) when I get some more time to talk about it. (Believe it or not, it took me more than two hours to write this post)!

So, stay tuned if you are interested. I suggest you follow me on Twitter, and follow this blog, recommend on Disqus (the heart icon).

Ah and well… all this is free, but you might see some interesting (or not) Ads around my content. I won’t mind if you all start clicking everywhere. I’m not suggesting anything… :p

]]>
http://itnerd.space/2017/07/27/looking-back-and-forward-coming-soon-to-this-blog/feed/ 0 361
How to authenticate to TP Link cloud API with Tasker http://itnerd.space/2017/06/19/how-to-authenticate-to-tp-link-cloud-api-with-tasker/ http://itnerd.space/2017/06/19/how-to-authenticate-to-tp-link-cloud-api-with-tasker/#respond Mon, 19 Jun 2017 23:03:06 +0000 http://itnerd.space/?p=356 We’ve seen in a previous post what is the protocol to authenticate to TP Link cloud API. Now, what about doing that from Tasker you can ask me? Well, that’s very simple actually!

To implement that in Tasker I have used RESTask for Tasker (because I had troubles having the native HTTP Post action work for me):

RESTask for Tasker (Free, Google Play) →

Next create 3 global variables in Tasker (self-explanatory I guess):

  • TPLUSER
  • TPLPASS
  • TPLTERM (use the UUIDv4 you got above)

Then create a new Task with the following 4 actions:

The first action will be a Variable Set %payload:

Name:%payload
To: { "method" :"login",
"params" : {
  "appType" :"Kasa_Android",
  "cloudPassword" :"%TPLPASS",
  "cloudUserName" :"%TPLUSER",
  "terminalUUID" :"%TPLTERM" } }

The second action will be the RESTask call:

Further down we need to set the custom body to %payload:

Then we set the Headers:

The third action will be a Javascriptlet with the following code, to extract the token (from the rtres, passed from  RESTask) to the %mtoken variable:

var mtoken = JSON.parse(rtres).result.token;

Finally, here the fourth action will just be a Flash action, that will show the token:

Flash [ Text:%mtoken Long:On ]

The whole task should end up looking like this:

You can run the Task and it should flash the token on the screen!! Yay

]]>
http://itnerd.space/2017/06/19/how-to-authenticate-to-tp-link-cloud-api-with-tasker/feed/ 0 356
How to authenticate to TP-Link cloud API http://itnerd.space/2017/06/19/how-to-authenticate-to-tp-link-cloud-api/ http://itnerd.space/2017/06/19/how-to-authenticate-to-tp-link-cloud-api/#comments Mon, 19 Jun 2017 23:01:45 +0000 http://itnerd.space/?p=345 This is breaking news! No need to hack into your phone’s backups any more to obtain the TP-Link token!

First you need to generate an UUID v4. You can open https://www.uuidgenerator.net/version4 and pick the one they give you there (no need to create one every time, one is fine, just keep it). This will represent your Client Term ID (like the one of your Kasa App).

Now this is how you can authenticate to the TP Link cloud backend and obtain a token:

We have to do a POST request to https://wap.tplinkcloud.com, with the following payload:

{
 "method": "login",
 "params": {
 "appType": "Kasa_Android",
 "cloudUserName": "XXXXX",
 "cloudPassword": "XXXXX",
 "terminalUUID": "MY_UUID_v4"
 }
}

In the payload, replace cloudUserName and cloudPassword with your TP-Link (or Kasa) credentials, and the terminalUUID with the one you got above.

Remember to pass the Header Content-Type: application/json as well.

No need to have any extra tool, you can simply try it with Hurl.it from your browser:

What you’ll get should look like this:

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

Isn’t that cool? Of course you can implement that in any language, Shell script with cURL, Python, Node.js,… Your token is response.result.token.

In a next post I’ll show how to do the same with Tasker! 😀

From here you can also have a look at this post: Get the end point URL and HS100 Device ID.

]]>
http://itnerd.space/2017/06/19/how-to-authenticate-to-tp-link-cloud-api/feed/ 19 345
Remotely control your HS100 timer (API) http://itnerd.space/2017/06/05/remotely-control-your-hs100-timer-api/ http://itnerd.space/2017/06/05/remotely-control-your-hs100-timer-api/#comments Mon, 05 Jun 2017 18:43:38 +0000 http://itnerd.space/?p=334 Thanks to a comment by Jacob K. on a previous post, I looked into how to set/unset the HS100 plug timer via the TP-Link API call.

To replay the steps below, be sure to already have:

In the next step, I recommend you use Hurl.it service to test the API calls, it’s the easiest way.

First let’s retrieve the timer Rule ID

Destination:

Body:
{“method”:”passthrough”, “params”: {“deviceId”: “YOUR_DEVICE_ID_HERE”, “requestData”: “{\”count_down\”:{\”get_rules\“:null}}” }}
 Then hit the Launch Request button. The result body should look like this:
{“error_code”:0,”result”:{“responseData”:”{\”count_down\”:{\”get_rules\”:{\”rule_list\”:[{\”enable\”:0,\”id\”:\”YOUR_RULE_ID_HERE\”,\”name\”:\”\”,\“delay\”:1800,\”act\”:0}],\”err_code\”:0}}}”}}
Have a look at the id field! I call it the Rule Id (YOUR_RULE_ID_HERE). we’ll need it in the next step. I would say it never changes but I am not sure.

Now you can enable/disable the timer like this:

Destination:
Body:
{“method”:”passthrough”, “params”: {“deviceId”: “YOUR_DEVICE_ID_HERE”, “requestData”: “{\”count_down\”:{\”edit_rule\“:{\”name\”:\”\”,\”act\”:0,\”enable\”:1,\”id\”:\”YOUR_RULE_ID_HERE\”,\”delay\”:1800}}}” }}
Use the following values in your body:
  • act: (action)
    • Use 0 to power off the plug after the timer
    • Use 1 to power on the plug after the timer
  • enable:
    • Use 0 to disable the timer
    • Use 1 to enable the timer
  • delay:
    • timer delay in seconds

Have fun!!

]]>
http://itnerd.space/2017/06/05/remotely-control-your-hs100-timer-api/feed/ 1 334
How to get the TP-Link HS100 cloud end-point URL? http://itnerd.space/2017/05/21/how-to-get-the-tp-link-hs100-cloud-end-point-url/ http://itnerd.space/2017/05/21/how-to-get-the-tp-link-hs100-cloud-end-point-url/#comments Sun, 21 May 2017 22:50:44 +0000 http://itnerd.space/?p=323 If you have followed my series on how to control the TP-Link HS100 smart plug by communicating with TP-Link Cloud services, you might have seen that there are some common issues related to the URL one need to point the requests to.

I’ve found out a way to know what is the URL, no more hit and miss needed any more!

The only thing that you’ll need is your TP-Link token (see how to get it). In a terminal, run the following command:

curl -s --request POST "https://wap.tplinkcloud.com?token=YOUR_TOKEN_HERE HTTP/1.1" \
 --data '{"method":"getDeviceList"}' \
 --header "Content-Type: application/json"

As an alternative, you can use hurl.it, an online version of curl, like shown on the picture below:

As Parameters use simply this:

{"method":"getDeviceList"}

As the name of the method implies, this will list all your HS100 devices (maybe other, please tell me what you see!). You should get something like this:

{
 "result" : {
 "deviceList" : [
 {
 "appServerUrl" : "https://eu-wap.tplinkcloud.com",
 "isSameRegion" : true,
 "deviceMac" : "",
 "status" : 1,
 "hwId" : "XXXX....",
 "deviceId" : "XXXX....",
 "oemId" : "XXXX....",
 "fwVer" : "1.0.8 Build 151101 Rel.24452",
 "deviceType" : "IOT.SMARTPLUGSWITCH",
 "alias" : "My Smart Plug",
 "fwId" : "BFF24826FBC561803E49379DBE74FD71",
 "deviceName" : "Wi-Fi Smart Plug",
 "deviceHwVer" : "1.0",
 "role" : 0,
 "deviceModel" : "HS100(EU)"
 }
 ]
 },
 "error_code" : 0
}

In the results above, we’ll get a list of all our TPLink devices, with their alias (the custom name we have given it), the deviceId, and the appServerUrl.

The appServerUrl is the one we’ll need to use in further POST requests, to control the plug.

]]>
http://itnerd.space/2017/05/21/how-to-get-the-tp-link-hs100-cloud-end-point-url/feed/ 12 323
Control your TP-Link HS100 smartplug with Tasker http://itnerd.space/2017/02/26/control-your-tp-link-hs100-smartplug-with-tasker/ http://itnerd.space/2017/02/26/control-your-tp-link-hs100-smartplug-with-tasker/#comments Sun, 26 Feb 2017 13:56:25 +0000 http://itnerd.space/?p=252 We’ve seen in a previous post how to switch our TP-Link HS100 smartplug from command line, from anywhere (not only the local network). In this post we’ll see how to create some Tasker tasks to control our smartplug. From that, we can imagine any useful/crazy profiles triggered from any events, time, location and switch the plug On/Off, for example “when I arrive home, switch on the Christmas Tree…” 🙂

To quickly recap, this is the command that can change the plug state (depending on the %state variable, 0: switch off, 1: switch on):

curl --request POST "https://eu-wap.tplinkcloud.com/?token=%token HTTP/1.1" \
  --data '{"method":"passthrough", "params": {"deviceId": "%deviceId", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":%state}}}" }}' \
  --header "Content-Type: application/json"

Task 1: Switch Plug On 💡

Using an HTTP Post action

In Tasker let’s create a new Task called “Switch Plug On 💡”, then add  3 “Variable Set” actions, to set the value of %deviceId, %token and %state to the right values for you.

Then we’ll use an HTTP Post action, that we’ll configure like this:

  • Server:Port: https://eu-wap.tplinkcloud.com
  • Path: ?token=%token
  • Data / File: {“method”:”passthrough”, “params”: {“deviceId”: “%deviceId”, “requestData”: “{\”system\”:{\”set_relay_state\”:{\”state\”:%state}}}” }}
  • Timeout: 30
  • Content Type: application/json

Note: Remember to change the URL above to the one that works for you (check the Common Issues section at the end of first post and its comments).

The resulting Tasker Task will look like this:

In text form, it looks like this:

Switch Plug On 💡 (143)
 A1: Variable Set [ Name:%deviceId To:YOUR_DEVICE_ID_HERE Recurse Variables:Off Do Maths:Off Append:Off ] 
 A2: Variable Set [ Name:%token To:YOUR_TOKEN_ID_HERE Recurse Variables:Off Do Maths:Off Append:Off ] 
 A3: Variable Set [ Name:%state To:1 Recurse Variables:Off Do Maths:Off Append:Off ] 
 A4: HTTP Post [ Server:Port:https://eu-wap.tplinkcloud.com Path:?token=%token Data / File:{"method":"passthrough", "params": {"deviceId": "%deviceId", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":%state}}}" }} Cookies: User Agent: Timeout:30 Content Type:application/json Output File: Trust Any Certificate:Off ]

Using a Run Shell action (2nd way)

If you have curl available from the command line on your Android phone, you can alternatively use a Run Shell action, instead of the HTTP Post action.

In this case, the Tasker Task would look like this:

And in text form it would read like this:

Switch Plug On 💡 (143)
 A1: Variable Set [ Name:%deviceId To:YOUR_DEVICE_ID_HERE Recurse Variables:Off Do Maths:Off Append:Off ] 
 A2: Variable Set [ Name:%token To:YOUR_TOKEN_ID_HERE Recurse Variables:Off Do Maths:Off Append:Off ] 
 A3: Variable Set [ Name:%state To:1 Recurse Variables:Off Do Maths:Off Append:Off ] 
 A4: Run Shell [ Command:curl --request POST "https://eu-wap.tplinkcloud.com/?token=%token HTTP/1.1" --data '{"method":"passthrough", "params": {"deviceId": "%deviceId", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":%state}}}" }}' --header "Content-Type: application/json" Timeout (Seconds):0 Use Root:Off Store Output In: Store Errors In: Store Result In: ]

Task 2: Switch Off Plug

Once you have the Switch On task working, you can simply clone it in Tasker and change the %state variable to 0, and that will give you the “Switch Off Plug” task!

Profiles ideas

Do not hesitate to contribute in the comments with any Profile idea you have to play with your TP-Link HS100 smartplug.


You can acquire Tasker from the Play Store, personally I believe it’s really worth the money. You can also download a 7 days free trial from Tasker official website.

Tasker (€2.99, Google Play) →

In a next post I’ll show how you can control your smart plug from your Android smartwatch, which is super cool!

]]>
http://itnerd.space/2017/02/26/control-your-tp-link-hs100-smartplug-with-tasker/feed/ 2 252
Script to open URLs and take screenshots http://itnerd.space/2017/01/03/batch-to-open-urls-and-take-screenshots/ http://itnerd.space/2017/01/03/batch-to-open-urls-and-take-screenshots/#comments Tue, 03 Jan 2017 17:46:05 +0000 http://itnerd.space/?p=167 Today I wanted to visually check a lot of URLs, to see if the page was loading fine, or if it was giving any kind of error. So I had an Excel file with the name of some applications (Azure WebApps), and for each the list URLs of that site, and I needed to load each URL and see, in a browser, what the pages look like.

The good thing is that depending the result, I could easily identify it if I had a high level view of all the pages. That’s were the thumbnail view comes in!

To do that I needed to automate several individual steps that I would later combine.

So I created a Frankenstein Powershell script combining all the pieces together, which you can see here.

The input I’m using is an Excel file, of which I’ll use two columns: Name and Hostnames, which contains a list of comma-separated URLs, of which I’ll only take the first one. I’ll use the Name column to name the output screenshot file.

This is how the Excel file would look like:

We load it using Import-Excel CmdLet. In the example case above, we would get:

PS C:\> Import-Excel D:\temp\file.xlsx

Name HostNames
---- ---------
google google.com, www.google.com
microsoft www.microsoft.com
yahoo yahoo.com

I didn’t find a way to take a screenshot of a non visible window, so I am showing the browser, and taking the screenshot of the region. For my purpose it works and it’s quite simple, but that presents some disadvantages (you cannot use the region of the screen while running the script, or you risk altering the result in the screenshot).

Another disadvantage is that it involves some precaution and some manual preparation, to position the windows, and modify the script accordingly, the first time at least.

  • Open a Powershell command line window
  • Run the following command. This will open an Internet Explorer window. Place it on your desktop, so that it will not overlap with the Powershell window. They can be side by side. It will be easier if you have two monitors, as you can place the browser window alone on a monitor.
 $IE=new-object -com internetexplorer.application
 $IE.visible=$true
  • Run the following commands and take note of the results:
 $IE.Top
 $IE.Left
 $IE.Width
 $IE.Height
  • Replace the results in the script. That way, each time we open a new browser the script will reposition it in the same location on your screen!

That’s it. Now you just have to run the script and it will do the job.

In the case of our example input file above, the output we’d get would look like in the output folder places in Thumbnail View:

In my particular use case, this below is what I was looking for:

As you can see, by placing the images in Thumbnail View, we can rapidly identify and classify the corresponding Web sites into 4 groups:

  1. Page seems to load fine
  2. Default initial Azure page (possibly no content deployed)
  3. Page doesn’t load, or takes too long
  4. Some HTTP error

Furthermore, within the first category, a quick inspection of the image will show if the page loads apparently fine, or if it shows some content problem.

If you have hundreds of sites, it can save you some precious time!

Some considerations:

  • I’m not sure what happens if the screen switch to screensaver mode. It may not work as expected. So, either deactivate the screensaver, or keep moving the mouse (out of the browser’s way) while the script runs.
  • I use a new IE instance for each new URL, that is, I don’t recycle the IE instance for several URLs, for a simple reason: if the URL doesn’t load, I would possibly take a screenshot of the previously loaded URL, which is not what I want.
]]>
http://itnerd.space/2017/01/03/batch-to-open-urls-and-take-screenshots/feed/ 2 167
How to run Android Java code from Tasker http://itnerd.space/2017/01/01/how-to-run-android-java-code-from-tasker/ http://itnerd.space/2017/01/01/how-to-run-android-java-code-from-tasker/#respond Sun, 01 Jan 2017 23:13:26 +0000 http://itnerd.space/?p=156 When it comes to Automation in Android, there is no doubt that Tasker is the absolute king. Although I admit the UI is not fancy — at all,  and the learning curve is quite harsh, Tasker has demonstrated being the most powerful app in the Automation space.

Tasker (€2.99, Google Play) →

One of the little know features of Tasker is the ability to run Android Java code, sort of.

That requires some learning, as everything in Tasker, but can prove very useful to further extend Tasker possibilities.

Let see a simple example, to illustrate the concept. Let say we want to enable Bluetooth. The corresponding Android Java code would be something like :

In Tasker we would use a couple of Java Function actions that we put in a Tasker task :

First:

BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();

Then:

bta.enable();

This is the resulting Tasker task:

Bluetooth Enable (71)
 A1: Java Function [ Return:bta Class Or Object:BluetoothAdapter Function:getDefaultAdapter
{BluetoothAdapter} () Param: Param: Param: Param: Param: Param: Param: ] 
 A2: Java Function [ Return: Class Or Object:bta Function:enable
{boolean} () Param: Param: Param: Param: Param: Param: Param: ]

This is very powerful and can unleash some new possibilities.

Say you want to enable LinkedIn synchronization on WiFi only. Tasker has no native actions to handle Android accounts synchronization. You need to access the ContentResolver Java objects and methods. This is something  you can do with Tasker! I’ll detail this example in a future post.

]]>
http://itnerd.space/2017/01/01/how-to-run-android-java-code-from-tasker/feed/ 0 156