Switching circles with Stretch directly with PHP or Autoit3

Plugwise Forum about Plugwise devices and the Source software.
Post Reply
Phoenix
Starting Member
Starting Member
Posts: 48
Joined: Sun Apr 28, 2013 9:40 pm
Location: Netherlands (Deventer)
Contact:

Switching circles with Stretch directly with PHP or Autoit3

Post by Phoenix »

Hi All,

With some time and effort i have found a way to switch circles with the Strech 2.0 directly, so you can make your own software to switch stuff! there is NO Plugwise software required for these scripts!

I've posted a PHP and Autoit3 example of how to do it!, enjoy!
PHP (dutch): http://phoenixinteractive.mine.nu/websi ... f=27&t=116
Autoit3 (dutch): http://phoenixinteractive.mine.nu/websi ... f=27&t=117

Comments:
PHP script: The PHP window doesn't close after switching
Autoit script: A messagebox will popup if the response <> 200 HTTP OK so you can see the error

If you have any improvements!, let us know!

PHP code:

Code: Select all

<?php
// A PHP script to switch Plugwise Circles with a Stretch 2.0 (tested on firmware 1.0.40)
// v1.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);
}
?>
Autoit code:

Code: Select all

; ------------------------------------------------------------------------------
; Autoit3 script to switch Plugwise Circles with a Stretch 2.0 (tested on firmware 1.0.40)
; v1.0, 2013-04-28 by Sebastiaan Ebeltjes
; Greetings from Deventer, The Netherlands
; ------------------------------------------------------------------------------

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

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

; Kernel
Global $PostAdres = "http://" & $StretchIp & "/minirest/appliances;id=" & $ApplianceId & "/power_state=" & $SwitchStatus

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", $PostAdres, False)
$oHTTP.SetRequestHeader("Host", $StretchIp)
$oHTTP.SetRequestHeader("Authorization", "Basic " & _Base64Encode("stretch:" & $StretchId))
$oHTTP.Send()
Global $PostResponse = $oHTTP.ResponseText

If $oHTTP.Status <> "200" Then MsgBox(64, "Foutmelding", $PostResponse)
Exit

; Functie voor BASE64 Encoding
Func _Base64Encode($sData)
    Local $oXml = ObjCreate("Msxml2.DOMDocument")
    If Not IsObj($oXml) Then
        SetError(1, 1, 0)
    EndIf

    Local $oElement = $oXml.createElement("b64")
    If Not IsObj($oElement) Then
        SetError(2, 2, 0)
    EndIf

    $oElement.dataType = "bin.base64"
    $oElement.nodeTypedValue = Binary($sData)
    Local $sReturn = $oElement.Text

    If StringLen($sReturn) = 0 Then
        SetError(3, 3, 0)
    EndIf

    Return $sReturn
EndFunc
Huisautomatisering - Domotica - Elektronica - IT consulting - Software ontwikkeling - 3D printing - Maatwerk
Website: domoticx.nl / Webshop: domoticx.nl/webwinkel / Knowledge Center: http://domoticx.com
Phoenix
Starting Member
Starting Member
Posts: 48
Joined: Sun Apr 28, 2013 9:40 pm
Location: Netherlands (Deventer)
Contact:

Re: Switching circles with Stretch directly with PHP or Autoit3

Post by Phoenix »

This week i'll write a CommandLine Interface (CLi) for Windows for the Plugwise Stretch 2.0, like i did with COCO TPC-200/300 here: http://www.domoticaforum.eu/viewtopic.php?f=40&t=8641

Stay tuned! :D


*EDIT:
DONE :D : http://www.domoticaforum.eu/viewtopic.p ... 288#p63288
Huisautomatisering - Domotica - Elektronica - IT consulting - Software ontwikkeling - 3D printing - Maatwerk
Website: domoticx.nl / Webshop: domoticx.nl/webwinkel / Knowledge Center: http://domoticx.com
Post Reply

Return to “Plugwise Forum”