Mir sind die Allnet-Steckdosen aktuell zu teuer und die 433-MHz-Kisten von der Reichweite her zu dünn. Also habe ich mir so eine EDIMAX SP-1101W WLAN-Steckdose besorgt und damit etwas rumgespielt. Herausgekommen ist das untenstehende Script. Für mich reicht’s, auch wenn man’s zweifelsohne noch optimieren kann.
Installation:
- Script in IPS anlegen, dabei die URL anpassen (User ist immer admin, Standardpasswort ist 1234)
- Zyklisches Ereignis anlegen, um das Script automatisch zu starten (holt bei mir alle 2 Minuten den aktuellen Status der Steckdosen, notwendig weil man die Dosen auch direkt am Gerät, also an IPS vorbei, schalten kann)
- Bool-Variable anlegen und dabei Skript im Profil hinterlegen
<?
// [TODO] Fehlerbehandlung
define('EDIMAX_ID', 12345); // ID der Variable
define('EDIMAX_URL', 'http://admin:1234@hostname_oder_ip:10000/smartplug.cgi');
// Wir holen erst mal den aktuellen Status. Das brauchen wir als Grundlage der Schaltentscheidung bei Aufruf via WebFront und sowieso für das zyklische Ereignis (s. unten).
$state = edimax_status();
if ($_IPS['SENDER'] == 'WebFront')
{
if ($_IPS['VALUE'] == false AND $state == true)
{
edimax_off();
$state = false;
}
else if ($_IPS['VALUE'] == true AND $state == false)
{
edimax_on();
$state = true;
}
}
// Immer gut
SetValueBoolean(EDIMAX_ID, $state);
// http://www.elv.at/topic/edimax-sp-1101w-funktionsweise-installation-ansteuerung-im-lokalen-netzwerk-vom-pc-aus.html
function edimax_on() {
$xml = '<?xml version="1.0" encoding="utf-8"?><SMARTPLUG id="edimax"><CMD id="setup"><Device.System.Power.State>ON</Device.System.Power.State></CMD></SMARTPLUG>';
$r = edimax($xml);
if (!empty($r) AND array_key_exists('CMD', $r) AND $r['CMD'] == 'OK') return true;
return false;
}
function edimax_off() {
$xml = '<?xml version="1.0" encoding="utf-8"?><SMARTPLUG id="edimax"><CMD id="setup"><Device.System.Power.State>OFF</Device.System.Power.State></CMD></SMARTPLUG>';
$r = edimax($xml);
if (!empty($r) AND array_key_exists('CMD', $r) AND $r['CMD'] == 'OK') return true;
return false;
}
function edimax_status() {
$xml = '<?xml version="1.0" encoding="UTF8"?><SMARTPLUG id="edimax"><CMD id="get"><Device.System.Power.State></Device.System.Power.State></CMD></SMARTPLUG>';
$r = edimax($xml);
if (!empty($r) AND array_key_exists('CMD', $r) AND array_key_exists('Device.System.Power.State', $r['CMD']))
{
if ( $r['CMD']['Device.System.Power.State'] == 'ON' ) return true;
}
return false;
}
function edimax($xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, EDIMAX_URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest=" . $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$data = curl_exec($ch);
curl_close($ch);
$r = json_decode(json_encode(simplexml_load_string($data)), true);
return $r;
}
?>
EDIT Freitag, 11. Juli 2014, 23:40h: SetValueBoolean statt SetValueInteger facepalm