Stretch 2.0 switching with scripts trough HTTP API

Plugwise Forum about Plugwise devices and the Source software.

Stretch 2.0 switching with scripts trough HTTP API

Postby Phoenix » Wed May 13, 2015 2:05 pm

Okay, so i've also finished the python script to switch circles on /off using the Stretch 2.0 HTTP API
Please note: All scripts work only with the stretch 2.X.X firmware!

Original page: http://domoticx.com/plugwise-stretch-2- ... t-scripts/

Stretch 2.X.X HTTP API:
API HTTP adres:
Code: Select all
http://[stretchip]/core/appliances;id=[applianceid]/relay

API POST gegevens:
Code: Select all
<relay><state>[on/off]</state></relay>


If you have adjusted the scripts to another language (PERL?/LUA?), please post it here ;-)

PHP Example:
Code: Select all
<?php
// A PHP script to switch Plugwise Circles with a Stretch 2.0 with firmware 2.X.X
// Version 1.0, 2014-11-18 by Sebastiaan Ebeltjes

// 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 = "97b7844d4da34fe7b60eda3d4bc823da"; // The ID of the circle, look in http://[STRETCHIP]/core/appliances
$SwitchStatus = "on"; // on/off

// Kernel
$AuthBase64 = base64_encode("stretch:".$StretchId);
$Postdata = "<relay><state>".$SwitchStatus."</state></relay>";
$HTTPHandle = fsockopen($StretchIp, 80, $errno, $errstr, 30);
if (!$HTTPHandle) {
    echo "$errstr ($errno)<br />\n";
} else {
    $Data = "POST /core/appliances;id=".$ApplianceId."/relay HTTP/1.1\r\n";
    $Data .= "Host: ".$StretchIp."\r\n";
    $Data .= "Content-Type: application/xml\r\n";
    $Data .= "Content-length: ".strlen($Postdata)."\r\n";
    $Data .= "Authorization: Basic ".$AuthBase64."\r\n";
    $Data .= "Connection: Close\r\n\r\n";
    $Data .= $Postdata;

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


Python example:
Code: Select all
# A Python script to switch Plugwise Circles with a Stretch 2.0 with firmware 2.X.X
# Version 1.0, 2015-05-13 by Sebastiaan Ebeltjes

# Importeren van bibliotheken
import urllib2 # Bibliotheek om data te ontvangen en te versturen via het HTTP protocol.
import base64 # Bibliotheek voor base64 coderingen.

# Stretch 2.0 setup
stretchip = "http://192.168.102X" # 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 = "750a4e07ed8840528f8ceb5c313eff3d" # The ID of the circle, look in http://[STRETCHIP]/core/appliances
switchstatus = "on" # on/off

# Kernel
postadres = stretchip + "/core/appliances;id=" + applianceid + "/relay"
postgegevens = "<relay><state>" + switchstatus + "</state></relay>"

httpdata = urllib2.Request(postadres)
httpdata.add_header("Host", stretchip)
httpdata.add_header("Content-Type", "application/xml")
httpdata.add_header("Content-length", str(len(postgegevens)))
httpdata.add_header("Authorization", "Basic " + base64.b64encode("stretch:" + stretchid))
httpdata.add_header("Connection", "Close")
httpdata.add_data(postgegevens)

httphandle = urllib2.urlopen(httpdata)

if httphandle.code != 200:
   print "error:" + httphandle.read()


Python Example with commandline parameters:
Switch example:
sudo python schakelen.py 750a4e07ed8840528f8ceb5c313eff3d on

Code: Select all
# A Python script to switch Plugwise Circles with a Stretch 2.0 with firmware 2.X.X + commandline parameters
# Version 1.0, 2015-05-13 by Sebastiaan Ebeltjes

# Importeren van bibliotheken
import urllib2 # Bibliotheek om data te ontvangen en te versturen via het HTTP protocol.
import base64 # Bibliotheek voor base64 coderingen.
import sys # Bibliotheek voor systeem handelingen.

if len(sys.argv) < 3:
  print "Usage: script.py [Appliance ID] [on/off]"
  exit()

# Stretch 2.0 setup
stretchip = "http://192.168.0.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 = sys.argv[1] # Commandolijn parameter 1
switchstatus = sys.argv[2] # Commandolijn parameter 2

# Kernel
postadres = stretchip + "/core/appliances;id=" + applianceid + "/relay"
postgegevens = "<relay><state>" + switchstatus + "</state></relay>"

httpdata = urllib2.Request(postadres)
httpdata.add_header("Host", stretchip)
httpdata.add_header("Content-Type", "application/xml")
httpdata.add_header("Content-length", str(len(postgegevens)))
httpdata.add_header("Authorization", "Basic " + base64.b64encode("stretch:" + stretchid))
httpdata.add_header("Connection", "Close")
httpdata.add_data(postgegevens)

httphandle = urllib2.urlopen(httpdata)

if httphandle.code != 200:
   print "error:" + httphandle.read()


Autoit3 example:
Code: Select all
; ------------------------------------------------------------------------------
; Autoit3 script to switch Plugwise Circles with a Stretch 2.0 with firmware 2.0.x)
; Version 1.0, 2013-10-07 by Sebastiaan Ebeltjes
; ------------------------------------------------------------------------------

; 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]/core/appliances
Global $SwitchStatus = "on" ;on/off

; Kernel
Global $PostAdres = "http://" & $StretchIp & "/core/appliances;id=" & $ApplianceId & "/relay"
Global $PostGegevens = "<relay><state>" & $SwitchStatus & "</state></relay>"

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", $PostAdres, False)
$oHTTP.SetRequestHeader("Host", $StretchIp)
$oHTTP.SetRequestHeader("Connection", "Close")
$oHTTP.SetRequestHeader("Content-Type", "application/xml")
$oHTTP.SetRequestHeader("Authorization", "Basic " & _Base64Encode("stretch:" & $StretchId))
$oHTTP.Send($PostGegevens)
$PostResponse = $oHTTP.ResponseText

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

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 ;_Base64Encode
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)

Return to Plugwise Forum

Who is online

Users browsing this forum: No registered users and 1 guest

cron