Page 1 of 1

Switching on the Stretch using HTTP

Posted: Thu Apr 11, 2013 4:13 pm
by Post-IT
I'm trying to switch circles using a HTTP call.

In the past I've done this using the iphone interface in Source:

Code: Select all

http://plugwiseserver/iphone/index.html?page=all&cmd=SwitchOff&app=6
For the Stretch this is different. Using Firebug I've determined these URLs are used when clicking in the webinterface:

Code: Select all

http://plugwiseserver/minirest/appliances;id=a0c85803b05744408f556c6510fc7a2/power_state=off
http://plugwiseserver/minirest/appliances;id=a0c85803b05744408f556c6510fc7a2/power_state=on
However when I put those URLs in my browser it returns an error:

Code: Select all

<error id="308">
<message>unknown URI: power_state=off</message>
</error>
Anyone who can help me with this?

Re: Switching on the Stretch using HTTP

Posted: Thu Apr 11, 2013 11:47 pm
by Post-IT
The 308 error (Unknown URI) is returned when using the GET-method. Looking in to the packets a POST-method is needed for the Stretch to react correctly.

To compile the correct HTTP-call you need:
- appliance id you want to switch, this can be found by looking at the main list at http://yourstretchip/minirest/modules or http://yourstretchip/minirest/appliances
- the base64 notation of the username password > base64_encode(username + ":" + password)

Depending on what software/script you are using or making you will have to add CR and LF on each line (or with "\r\n" in my case). Also end the complete HTTP-call with an extra CR+LF.

Code: Select all

POST /minirest/appliances;id=12bf4d8e662a4d6980460973ecd7dbf/power_state=on HTTP/1.1\r\n     << use power_state=off or on
Host: 192.168.1.201\r\n                                             << change to your Stretch IP
Accept: */*\r\n
Authorization: Basic Z290Y2hhOmR1bWJhc3Mh\r\n                   << this is your "username:password" in base64 code
Connection: keep-alive\r\n
Content-Length: 0\r\n
\r\n
Hope this helps others to use their Stretch and HTTP-calls. Be reminded, this is not supported by PW (times many... :lol: )

Re: Switching on the Stretch using HTTP

Posted: Sun Apr 28, 2013 9:48 pm
by Phoenix
@Post-IT,

1) With wich software/OS do you send this package with?
2) Therefore...are you sending a RAW packet to a IP?

I tried this in JAN 2013, when i captured some packets with WireShark, i've translated it to Autoit (or VB) and tried many time's with no luck till today!

With autoit It gives me a HTTP 200 back, but nothing get's switched!, i tried many configurations...i guess we have to search further...

Ps. i also do not understand why there is a ; in the http string, this makes the POST command wierd, i know that the appliances is a LUA (appliances.lua) script, but for a normal "php" call it should be: appliances?id= (instead of appliances;id)

Re: Switching on the Stretch using HTTP

Posted: Sun Apr 28, 2013 11:17 pm
by Phoenix
Ok, i've tried it with a PHP script...it WORKS perfectly!! :mrgreen:
See here: http://phoenixinteractive.mine.nu/websi ... p=117#p117

The PHP code:

Code: Select all

<?php
// A PHP script to switch Plugwise Circles with a Stretch 2.0 (tested on firmware 1.0.40)
// Version 1.0, 2013-04-28 by Sebastiaan Ebeltjes
// Greetings from Deventer, The Netherlands

// Stretch 2.0 setup
$StretchIp = "192.168.1.102"; //The IP adres of the Stretch 2.0, like: 192.168.1.X
$StretchId = "STRETCHID"; //The 8 letters of the Stretch 2.0 ID

// Switch setup (circle)
$ApplianceId = "71abf031c54f400ca77c8ae6957ad7cf"; // The ID of the circle, look in http://[STRETCHIP]/minirest/appliances
$SwitchStatus = "on"; // on/off

// Kernel
$AuthBase64 = base64_encode("stretch:".$StretchId);
$HTTPHandle = fsockopen($StretchIp, 80, $errno, $errstr, 30);
if (!$HTTPHandle) {
	echo "$errstr ($errno)<br />\n";
} else {
	$Data = "POST /minirest/appliances;id=".$ApplianceId."/power_state=".$SwitchStatus." HTTP/1.1\r\n";
    $Data .= "Host: ".$StretchIp."\r\n";
    $Data .= "Authorization: Basic ".$AuthBase64."\r\n";
    $Data .= "\r\n";

    fwrite($HTTPHandle, $Data);
    while (!feof($HTTPHandle)) {
        echo fgets($HTTPHandle, 128);
    }
    fclose($HTTPHandle);
}
?>
Enjoy!

Ps. now transforming it to a Autoit/VB code... :)

Re: Switching on the Stretch using HTTP

Posted: Sun Apr 28, 2013 11:23 pm
by Post-IT
I'm now using a Gira homeserver to send a raw TCP packet, but I've configured this part by testing with a restclient (CocoaRestClient in my case) from my OSX system. Both of them work correctly.

I've noticed the webserver almost always returns HTTP 200 OK (response header) as long as you send a valid HTTP request. To see if the data is handled correctly you should also look at the response body. HTTP 200 OK is only a signal from the webserver, not from the stretch software. The 308 error was a parse error, not a HTTP error.

I would suggest you remove all the lines I did not use in my query (X-Request, Referer, UserAgent, etc.) as this will only complicate things with variables which are not needed.


On the semicolon, I think it is used as a replacement argument separator. W3C posted an advisory note on webserver development back in 1999 which stated they should implement ";" as an argument separator in stead of "&" because "&" would have to be escaped. However it is rarely used. Maybe Plugwise chose to do this due to script things.

Re: Switching on the Stretch using HTTP

Posted: Mon Apr 29, 2013 12:32 am
by Phoenix
Hi Post-it,

Yeah i got it working in PHP (see my post above), i now need to translate it to autoit (if possible)

This is the HTTP 200 message comming from the NGINX server on the Stretch :)
Image

Ps. Thanks for the explanation about the semicolon!

Re: Switching on the Stretch using HTTP

Posted: Mon Apr 29, 2013 4:42 pm
by Post-IT
Does Autoit send CR+LF after each line and an extra one in the end? I don't see any of that in your Autoit code but you did use \r\n in the PHP code.

Re: Switching on the Stretch using HTTP

Posted: Mon Apr 29, 2013 4:46 pm
by Phoenix
Post-IT wrote:Does Autoit send CR+LF after each line and an extra one in the end? I don't see any of that in your Autoit code but you did use \r\n in the PHP code.
It seems the Autoit3 code doesn't need it, maybe it's done automaticly in the "Object handler for winhttp.winhttprequest.5.1": ObjCreate("winhttp.winhttprequest.5.1")?, but it works and that's the goal! :lol:

Re: Switching on the Stretch using HTTP

Posted: Wed May 01, 2013 9:58 pm
by TANE
This sounds good.

When data conversion is available I will also swap to the Stretch.
At the moment using Lua scripting from Fibaro HC2 for switch ON/Off the devices.

Turn Kids Computer ON:

Code: Select all

plugwise = Net.FHttp("192.168.1.100", 8080) 
plugwise:setBasicAuthentication("user", "password") 
response = plugwise:GET("/cc.html?applid=59&cmd=on") 
fibaro:log(response) 
My hope is that something like this will work with the Stretch

Re: Switching on the Stretch using HTTP

Posted: Tue Mar 17, 2015 10:07 am
by nslmanu
Phoenix wrote:Ok, i've tried it with a PHP script...it WORKS perfectly!! :mrgreen:
See here: http://phoenixinteractive.mine.nu/websi ... p=117#p117

The PHP code:

Code: Select all

<?php
// A PHP script to switch Plugwise Circles with a Stretch 2.0 (tested on firmware 1.0.40)
// Version 1.0, 2013-04-28 by Sebastiaan Ebeltjes
// Greetings from Deventer, The Netherlands

// Stretch 2.0 setup
$StretchIp = "192.168.1.102"; //The IP adres of the Stretch 2.0, like: 192.168.1.X
$StretchId = "STRETCHID"; //The 8 letters of the Stretch 2.0 ID

// Switch setup (circle)
$ApplianceId = "71abf031c54f400ca77c8ae6957ad7cf"; // The ID of the circle, look in http://[STRETCHIP]/minirest/appliances
$SwitchStatus = "on"; // on/off

// Kernel
$AuthBase64 = base64_encode("stretch:".$StretchId);
$HTTPHandle = fsockopen($StretchIp, 80, $errno, $errstr, 30);
if (!$HTTPHandle) {
	echo "$errstr ($errno)<br />\n";
} else {
	$Data = "POST /minirest/appliances;id=".$ApplianceId."/power_state=".$SwitchStatus." HTTP/1.1\r\n";
    $Data .= "Host: ".$StretchIp."\r\n";
    $Data .= "Authorization: Basic ".$AuthBase64."\r\n";
    $Data .= "\r\n";

    fwrite($HTTPHandle, $Data);
    while (!feof($HTTPHandle)) {
        echo fgets($HTTPHandle, 128);
    }
    fclose($HTTPHandle);
}
?>
Enjoy!

Ps. now transforming it to a Autoit/VB code... :)
Hey Phoenix :)

I'm trying to follow your php script ... but mine is going into infinite loop ....

I have to desactivate this :

//fwrite($HTTPHandle, $Data);
//while (!feof($HTTPHandle)) {
//echo fgets($HTTPHandle, 128);
//}
//fclose($HTTPHandle);

and doing an echo for $Data : i have this on the html page result :

POST /minirest/appliances;id=5q4d1sf65sd14f56sd14f56sdf1g45dsfg64151116451/power_state=on HTTP/1.1 Host: 192.168.0.11 Authorization: Basic q65s41df564qsd1f54qds1==

seems ok for you ?

Many thanks for your help and reading :)

Re: Switching on the Stretch using HTTP

Posted: Mon May 11, 2015 10:52 pm
by Phoenix
Hi nslmanu,

Have you used the right script for your firmware??

/minirest/appliances is for the old v1.0.x firmware....

I wrote it all down here, both firmware and PHP/Autoit3 examples: http://domoticx.com/plugwise-stretch-2- ... t-scripts/

A Python example will follow! :D

Re: Switching on the Stretch using HTTP

Posted: Sun May 17, 2015 8:08 pm
by nslmanu
Many thanks , now it's ok :) i changed the script.

Btw, i'm sad because i have to use the script hosted on my NAS Synology to launch it and use the appliances on my Stresch ... so it's not really standalone application in fact. :(

Btw many thanks for your help and script :)

Re: Switching on the Stretch using HTTP

Posted: Sun Mar 19, 2017 6:57 pm
by Collin01
Post-IT wrote:I'm trying to switch circles using a HTTP call.

In the past I've done this using the iphone interface in Source:

Code: Select all

http://plugwiseserver/iphone/index.html?page=all&cmd=SwitchOff&app=6
For the Stretch this is different. Using Firebug I've determined these URLs are used when clicking in the webinterface:

Code: Select all

http://plugwiseserver/minirest/appliances;id=a0c85803b05744408f556c6510fc7a2/power_state=off
http://plugwiseserver/minirest/appliances;id=a0c85803b05744408f556c6510fc7a2/power_state=on
transfers Dieppe
However when I put those URLs in my browser it returns an error:

Code: Select all

<error id="308">
<message>unknown URI: power_state=off</message>
</error>
Anyone who can help me with this?
I can help you, can you send me a pm please?

Re: Switching on the Stretch using HTTP

Posted: Mon Mar 20, 2017 11:15 am
by Post-IT
That was four years ago. If you read the full thread you'll notice the problem got solved.