Switching on the Stretch using HTTP

Plugwise Forum about Plugwise devices and the Source software.

Switching on the Stretch using HTTP

Postby Post-IT » Thu Apr 11, 2013 3:13 pm

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?
Post-IT
Member
Member
 
Posts: 446
Joined: February 2009
Location: Netherlands (Rotterdam)

Re: Switching on the Stretch using HTTP

Postby Post-IT » Thu Apr 11, 2013 10:47 pm

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: )
Post-IT
Member
Member
 
Posts: 446
Joined: February 2009
Location: Netherlands (Rotterdam)

Re: Switching on the Stretch using HTTP

Postby Phoenix » Sun Apr 28, 2013 8:48 pm

@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)
Last edited by Phoenix on Fri May 03, 2013 8:56 pm, edited 1 time in total.
Huisautomatisering - Domotica - Elektronica - IT consulting - Software ontwikkeling - 3D printing - Maatwerk
Website: domoticx.nl / Webshop: domoticx.nl/webwinkel / Knowledge Center: http://domoticx.com
User avatar
Phoenix
Starting Member
Starting Member
 
Posts: 48
Joined: April 2013
Location: Netherlands (Deventer)

Re: Switching on the Stretch using HTTP

Postby Phoenix » Sun Apr 28, 2013 10:17 pm

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... :)
Last edited by Phoenix on Sun Apr 28, 2013 10:45 pm, edited 1 time in total.
Huisautomatisering - Domotica - Elektronica - IT consulting - Software ontwikkeling - 3D printing - Maatwerk
Website: domoticx.nl / Webshop: domoticx.nl/webwinkel / Knowledge Center: http://domoticx.com
User avatar
Phoenix
Starting Member
Starting Member
 
Posts: 48
Joined: April 2013
Location: Netherlands (Deventer)

Re: Switching on the Stretch using HTTP

Postby Post-IT » Sun Apr 28, 2013 10:23 pm

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.
Post-IT
Member
Member
 
Posts: 446
Joined: February 2009
Location: Netherlands (Rotterdam)

Re: Switching on the Stretch using HTTP

Postby Phoenix » Sun Apr 28, 2013 11:32 pm

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!
Huisautomatisering - Domotica - Elektronica - IT consulting - Software ontwikkeling - 3D printing - Maatwerk
Website: domoticx.nl / Webshop: domoticx.nl/webwinkel / Knowledge Center: http://domoticx.com
User avatar
Phoenix
Starting Member
Starting Member
 
Posts: 48
Joined: April 2013
Location: Netherlands (Deventer)

Re: Switching on the Stretch using HTTP

Postby Post-IT » Mon Apr 29, 2013 3:42 pm

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.
Post-IT
Member
Member
 
Posts: 446
Joined: February 2009
Location: Netherlands (Rotterdam)

Re: Switching on the Stretch using HTTP

Postby Phoenix » Mon Apr 29, 2013 3:46 pm

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:
Huisautomatisering - Domotica - Elektronica - IT consulting - Software ontwikkeling - 3D printing - Maatwerk
Website: domoticx.nl / Webshop: domoticx.nl/webwinkel / Knowledge Center: http://domoticx.com
User avatar
Phoenix
Starting Member
Starting Member
 
Posts: 48
Joined: April 2013
Location: Netherlands (Deventer)

Re: Switching on the Stretch using HTTP

Postby TANE » Wed May 01, 2013 8:58 pm

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
Enver Tanriverdi | http://blog.tane.nl
User avatar
TANE
Forum Moderator
Forum Moderator
 
Posts: 4806
Joined: April 2007
Location: Netherlands

Re: Switching on the Stretch using HTTP

Postby nslmanu » Tue Mar 17, 2015 9:07 am

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 :)
nslmanu
Starting Member
Starting Member
 
Posts: 9
Joined: May 2010

Re: Switching on the Stretch using HTTP

Postby Phoenix » Mon May 11, 2015 9:52 pm

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
Last edited by Phoenix on Wed Jun 03, 2015 5:48 pm, edited 1 time in total.
Huisautomatisering - Domotica - Elektronica - IT consulting - Software ontwikkeling - 3D printing - Maatwerk
Website: domoticx.nl / Webshop: domoticx.nl/webwinkel / Knowledge Center: http://domoticx.com
User avatar
Phoenix
Starting Member
Starting Member
 
Posts: 48
Joined: April 2013
Location: Netherlands (Deventer)

Re: Switching on the Stretch using HTTP

Postby nslmanu » Sun May 17, 2015 7:08 pm

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 :)
nslmanu
Starting Member
Starting Member
 
Posts: 9
Joined: May 2010

Re: Switching on the Stretch using HTTP

Postby Collin01 » Sun Mar 19, 2017 5:57 pm

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?
Collin01
Starting Member
Starting Member
 
Posts: 1
Joined: March 2017

Re: Switching on the Stretch using HTTP

Postby Post-IT » Mon Mar 20, 2017 10:15 am

That was four years ago. If you read the full thread you'll notice the problem got solved.
Post-IT
Member
Member
 
Posts: 446
Joined: February 2009
Location: Netherlands (Rotterdam)


Return to Plugwise Forum

Who is online

Users browsing this forum: No registered users and 1 guest