Jak připojit chytrou zásuvku D-link DSP-W215 k openhabu

Tutorial o tom, jak udělat aby chytrá zásuvka d-link dsp-w215 byla ovládatelná od jinud než z oficiální aplikace dlink. V mém případě používám Openhab ,ale s menšími úpravami jej lze integrovat do jakékoliv jiné aplikace. Následující tutoriál je pro linux. Ale s malou modifikací by mělo být možné použít i na Windows (NODEJS je multiplatformní).

Big thanks to bikerp – github

What i did:

  • Stáhněte kód z následující stránky bikerp github.
  • Upravte kód app.js následovně, abyste jej mohli používat s bindingem exec1 v openhabu. (Pro Openhab2 povolte instalaci EXEC pro verzi 1. Zatím moc nevím jak pracovat s EXEC 2.) Musíte zadat PIN_CODE a IP DSP zásuvky.
var soapclient = require('./js/soapclient');
var fs = require('fs');
var OUTPUT_FILE = "result.txt";
var LOGIN_USER = "admin";
var LOGIN_PWD = "PIN_CODE";
var HNAP_URL = "http://IP_OF_DSP/HNAP1";
var POLLING_INTERVAL = 60000;

var args = process.argv.slice(2);

soapclient.login(LOGIN_USER, LOGIN_PWD, HNAP_URL).done(function (status) {
 if (!status) {
   throw "Login failed!";
 }
 if (status != "success") {
   throw "Login failed!";
 }
 if (args == "on") {
   soapclient.on();
 }
 if (args == "off"){
   soapclient.off();
 }
 if (args == "power"){
   soapclient.consumption().done(function (power) {console.log(power);});
 }
 if (args == "total"){
   soapclient.totalConsumption().done(function (power) {console.log(power);});
 }
 if (args == "temp"){
  soapclient.temperature().done(function (temperature) {console.log(temperature);});
 }
 if (args == "state"){
  soapclient.state().done(function (state) {
   if(state == "true"){
    console.log("ON");
   } else {
    console.log("OFF");
   }
  });;
 }
});
  • Budete také muset nainstalovat NODEJS, aby mohl spustit javascript z terminálu.
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
  • Spusťte „npm install“ uvnitř adresáře s balíčky package.json a app.js. Tím se lokálně nainstalují všechny potřebné moduly.
  • Nastavte items v openhabu. Zde můžete vidět nastavení pro Switch má 1 vstupní hodnotu a dva výstupy. Dva výstupy z openhabu jsou jednoduše ON a OFF. Vstup kontrolujte každých 10 sekund aktuální stav zásuvky v případě vypnutí zásuvky tlačítkem.
  • V následujícím příkladu adresář /usr/share/openhab/dlink_dsp/ je cesta, kde jsem rozbalil obsah z  bikerp github.
Group    DSPW215_group    "Dlink Plug"    <poweroutlet>
Group    DSPW215_stats
Switch    DSPW215    "Plug switch"    <poweroutlet>    (DSPW215_group)        { exec="<[/bin/sh@@-c@@cd /etc/openhab2/dlink_dsp/;node app.js state:30000:REGEX((.*?))] >[ON:/bin/sh@@-c@@cd /etc/openhab2/dlink_dsp/;node app.js on] >[OFF:/bin/sh@@-c@@cd /etc/openhab2/dlink_dsp/;node app.js off]" }
Number    DSPW215_power    "Plug current [%.2f W]"    <chart>    (DSPW215_group,DSPW215_stats)        { exec="<[/bin/sh@@-c@@cd /etc/openhab2/dlink_dsp/;node app.js power:90000:REGEX((.*?))]" }
Number    DSPW215_total    "Plug total [%.2f KWh]"    <chart>    (DSPW215_group,DSPW215_stats)        { exec="<[/bin/sh@@-c@@cd /etc/openhab2/dlink_dsp/;node app.js total:90000:REGEX((.*?))]" }
Number    DSPW215_temperature    "Plug Temperature: [%.2f C]"    <temperature>    (DSPW215_group)        { exec="<[/bin/sh@@-c@@cd /etc/openhab2/dlink_dsp/;node app.js temp:60000:REGEX((.*?))]" }
  • Zde je soubor the default.sitemap
sitemap default label="My Home" {
  Frame label="Living room" {
    Group item=DSPW215_group
  }
}
  • A výsledek na stránce (Můžete také vytvořit Charts a rules pro vytvoření časovače)

Zde je timer.rules

import org.joda.time.*
import org.openhab.model.script.actions.Timer
import org.openhab.core.library.types.*


var Timer masterAlarmTime = null
var Timer masterAlarmTimeOFF = null

rule "Initialize DSPW215 items"
when
    System started
then
    Alarm_DSPW215TimeMins.state
end

rule "DSPW215 alarm time"
when
    Item Alarm_DSPW215TimeMins received update or
    Item Alarm_DSPW215TimeMinsOFF received update
then
    var int minutes = (Alarm_DSPW215TimeMins.state as DecimalType).intValue()
    var int minutesOFF = (Alarm_DSPW215TimeMinsOFF.state as DecimalType).intValue()

    if (masterAlarmTime != null)
        masterAlarmTime.cancel()
    if (masterAlarmTimeOFF != null)
        masterAlarmTimeOFF.cancel()

    // work out when the alarm is to fire - start from midnight
    var DateTime alarmTime = new DateTime()

    // add the number of minutes selected
    alarmTime = alarmTime.plusMinutes(minutes)

    // create a timer to execute the alarm at the specified time
    masterAlarmTime = createTimer(alarmTime) [| 
        if (Alarm_DSPW215.state == ON) 
            Alarm_DSPW215Event.sendCommand(ON)
    ]
    Alarm_DSPW215Time.sendCommand(String::format("%02d:%02d", alarmTime.getHourOfDay(), alarmTime.getMinuteOfHour()))
    
    //To turn off
    var DateTime alarmTimeOFF = alarmTime.plusMinutes(minutesOFF+5)

    masterAlarmTimeOFF = createTimer(alarmTimeOFF) [| 
        if (Alarm_DSPW215.state == ON) 
            Alarm_DSPW215Event.sendCommand(OFF)
    ]

    // update the alarm display time    
    Alarm_DSPW215TimeOFF.sendCommand(String::format("%02d:%02d", alarmTimeOFF.getHourOfDay(), alarmTimeOFF.getMinuteOfHour()))
end

rule "DSPW215 alarm"
when
    Item Alarm_DSPW215Event received command ON
then
    DSPW215.sendCommand(ON)
end

rule "DSPW215 alarm OFF"
when
    Item Alarm_DSPW215Event received command OFF
then
    DSPW215.sendCommand(OFF)
    Alarm_DSPW215.sendCommand(OFF)
end

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *