Merge branch '2.0' of Karl/KLanding into master
This commit is contained in:
commit
9b84dd6872
90
S20/Orvibo.php
Normal file
90
S20/Orvibo.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Phil Parsons <phil@parsons.uk.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
class Orvibo
|
||||
{
|
||||
|
||||
private $host;
|
||||
private $port;
|
||||
private $mac;
|
||||
private $delay = 10000; //microseconds
|
||||
private $subscribed=false;
|
||||
private $twenties = array(0x20,0x20,0x20,0x20,0x20,0x20);
|
||||
private $zeroes = array(0x00,0x00,0x00,0x00);
|
||||
|
||||
public function __construct($host = '10.10.100.254', $port = 10000, $mac)
|
||||
{
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->mac = $mac;
|
||||
if ($this->subscribed === false) {
|
||||
$this->subscribe();
|
||||
}
|
||||
}
|
||||
|
||||
public function getDelay()
|
||||
{
|
||||
return $this->delay;
|
||||
}
|
||||
|
||||
private function subscribe() {
|
||||
$command = array(0x68,0x64,0x00,0x1e,0x63,0x6c);
|
||||
$command = array_merge($command, $this->mac, $this->twenties, array_reverse($this->mac),
|
||||
$this->twenties);
|
||||
$this->sendCommand($command);
|
||||
$this->subscribed=true;
|
||||
}
|
||||
|
||||
public function on() {
|
||||
if ($this->subscribed === false) {
|
||||
$this->subscribe();
|
||||
}
|
||||
$command = array(0x68,0x64,0x00,0x17,0x64,0x63);
|
||||
$command = array_merge($command, $this->mac, $this->twenties, $this->zeroes, array(0x01));
|
||||
$this->sendCommand($command);
|
||||
}
|
||||
|
||||
public function off() {
|
||||
if ($this->subscribed === false) {
|
||||
$this->subscribe();
|
||||
}
|
||||
$command = array(0x68,0x64,0x00,0x17,0x64,0x63);
|
||||
$command = array_merge($command, $this->mac, $this->twenties, $this->zeroes, array(0x00));
|
||||
$this->sendCommand($command);
|
||||
}
|
||||
|
||||
public function sendCommand(Array $command)
|
||||
{
|
||||
$message = vsprintf(str_repeat('%c', count($command)), $command);
|
||||
for ($try=0;$try<5;$try++) {
|
||||
if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
|
||||
socket_sendto($socket, $message, strlen($message), 0, $this->host, $this->port);
|
||||
socket_close($socket);
|
||||
usleep($this->getDelay()); //wait 100ms before sending next command
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
S20/cron.bat
Normal file
1
S20/cron.bat
Normal file
@ -0,0 +1 @@
|
||||
c:\xampp\php\php.exe c:\xampp\htdocs\s20\cron.php
|
342
S20/cron.php
Normal file
342
S20/cron.php
Normal file
@ -0,0 +1,342 @@
|
||||
<?php
|
||||
|
||||
// Configuration
|
||||
// SERVER root to Domoticz
|
||||
$domoticz_server = 'http://localhost:8080';
|
||||
|
||||
// PLUGS
|
||||
// mac (without special chars and lowercase) = IDX
|
||||
// change next arrays and add / remove lines to add / remove plugs
|
||||
$mienchufe['accf23514ddd'] = '106'; // Description
|
||||
$mienchufe['accf23514eee'] = '107'; // Description
|
||||
|
||||
|
||||
// Do NOT change these
|
||||
$orvibo['ip'] = "0.0.0.0";
|
||||
$orvibo['port'] = 10000;
|
||||
$orvibo['NEED_DISCOVER'] = 1;
|
||||
|
||||
|
||||
function makePayload($data) {
|
||||
$res='';
|
||||
foreach($data as $v) {
|
||||
$res.=chr($v);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function HexStringToArray($buf) {
|
||||
$res=array();
|
||||
for($i=0;$i<strlen($buf)-1;$i+=2) {
|
||||
$res[]=(hexdec($buf[$i].$buf[$i+1]));
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function HexStringToString($buf) {
|
||||
$res='';
|
||||
for($i=0;$i<strlen($buf)-1;$i+=2) {
|
||||
$res.=chr(hexdec($buf[$i].$buf[$i+1]));
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
function binaryToString($buf) {
|
||||
$res='';
|
||||
for($i=0;$i<strlen($buf);$i++) {
|
||||
$num=dechex(ord($buf[$i]));
|
||||
if (strlen($num)==1) {
|
||||
$num='0'.$num;
|
||||
}
|
||||
$res.=$num;
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
//Create a UDP socket
|
||||
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)))
|
||||
{
|
||||
$errorcode = socket_last_error();
|
||||
$errormsg = socket_strerror($errorcode);
|
||||
|
||||
die("Couldn't create socket: [$errorcode] $errormsg \n");
|
||||
}
|
||||
|
||||
echo "Socket created \n";
|
||||
|
||||
// Bind the source address
|
||||
if( !socket_bind($sock, $orvibo['ip'] , $orvibo['port']) )
|
||||
{
|
||||
$errorcode = socket_last_error();
|
||||
$errormsg = socket_strerror($errorcode);
|
||||
|
||||
die("Could not bind socket : [$errorcode] $errormsg \n");
|
||||
}
|
||||
|
||||
echo "Socket bind (".$orvibo['ip'].":".$orvibo['port'].") OK \n";
|
||||
|
||||
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1);
|
||||
|
||||
$discover=1;
|
||||
|
||||
|
||||
$latest_config_read=time();
|
||||
$tiempo_ejecucion=time();
|
||||
$subscribed=array();
|
||||
$ahora = time()+20;
|
||||
|
||||
//Do some communication, this loop can handle multiple clients
|
||||
while(1)
|
||||
{
|
||||
|
||||
// setGlobal((str_replace('.php', '', basename(__FILE__))).'Run', time(), 1);
|
||||
|
||||
if ((time()-$latest_config_read)>10) {
|
||||
$latest_config_read=time();
|
||||
|
||||
if ($orvibo['NEED_DISCOVER']) {
|
||||
echo date('H:i:s')." Need discover flag set ... \n";
|
||||
$discover=1;
|
||||
$orvibo['NEED_DISCOVER']=0;
|
||||
}
|
||||
}
|
||||
|
||||
if ((time()-$latest_discover)>5*60) {
|
||||
echo date('H:i:s')." Discover timeout, rediscovering ... \n";
|
||||
$discover=1;
|
||||
}
|
||||
|
||||
if ($discover) {
|
||||
echo date('H:i:s')." Discovering ... \n";
|
||||
|
||||
$payload = makePayload(array(0x68, 0x64, 0x00, 0x06, 0x71, 0x61));
|
||||
echo date('H:i:s')." Sending multicast: ".binaryToString($payload)."\n";
|
||||
socket_sendto($sock, $payload, strlen($payload), 0, '255.255.255.255', $orvibo['port']);
|
||||
|
||||
$latest_discover=time();
|
||||
$discover=0;
|
||||
}
|
||||
|
||||
echo date('H:i:s')." Waiting for data ... \n";
|
||||
|
||||
//Receive some data
|
||||
socket_set_option($sock,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>10,"usec"=>0));
|
||||
$buf='';
|
||||
@$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
|
||||
if ($remote_ip!=$orvibo['ip'] && $buf!='') {
|
||||
processMessage($buf, $remote_ip, $sock);
|
||||
}
|
||||
$ahora2 = time();
|
||||
// if (file_exists('./reboot') || IsSet($_GET['onetime']) || $ahora2 < $ahora)
|
||||
if (file_exists('./reboot') || IsSet($_GET['onetime']) || $contador > 12 )
|
||||
{
|
||||
print_r($enchufe);
|
||||
|
||||
// MENSAJE
|
||||
//json.htm?type=command¶m=addlogmessage&message=MESSAGE
|
||||
// Initiate curl
|
||||
//$mensaje = urlencode("Mensaje de prueba en LOG");
|
||||
//$url = $domoticz_server.'/json.htm?type=command¶m=addlogmessage&message='.$mensaje;
|
||||
//$respuesta = send_url($url);
|
||||
//$result = file_get_contents($url);
|
||||
|
||||
// Will dump a beauty json :3
|
||||
// var_dump(json_decode($result, true));
|
||||
foreach($enchufe as $k => $v) {
|
||||
if ( $mienchufe[$k] ) {
|
||||
|
||||
$switchcmd = '';
|
||||
if ( $v == "1" ) {
|
||||
$switchcmd = 'On';
|
||||
} elseif ( $v == "0" ) {
|
||||
$switchcmd = 'Off';
|
||||
}
|
||||
|
||||
if ( $switchcmd != '' ) {
|
||||
$url = $domoticz_server.'/json.htm?type=command¶m=switchlight&idx='.$mienchufe[$k].'&switchcmd='.$switchcmd;
|
||||
$respuesta = send_url($url);
|
||||
$respuesta_json = json_decode($respuesta, true);
|
||||
print_r($respuesta_json);
|
||||
$mensaje = urlencode("Orvibo S20 - idx: ".$mienchufe[$k]." - mac: ".$k." - status: ".$switchcmd." - response: ".$respuesta_json['status']);
|
||||
$url = $domoticz_server.'/json.htm?type=command¶m=addlogmessage&message='.$mensaje;
|
||||
$respuesta = send_url($url);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//$mensaje = urlencode("Mensaje de prueba en LOG");
|
||||
//$url = $domoticz_server.'/json.htm?type=command¶m=addlogmessage&message='.$mensaje;
|
||||
//$respuesta = send_url($url);
|
||||
|
||||
}
|
||||
// TURN ON / OFF
|
||||
//
|
||||
|
||||
socket_close($sock);
|
||||
// $db->Disconnect();
|
||||
exit;
|
||||
}
|
||||
|
||||
$contador++;
|
||||
|
||||
}
|
||||
|
||||
socket_close($sock);
|
||||
|
||||
|
||||
function send_url($url) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_URL,$url);
|
||||
$result=curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return($result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
function processMessage($buf, $remote_ip, $sock) {
|
||||
global $subscribed,$enchufe;
|
||||
|
||||
echo date('H:i:s')." $remote_ip : " . binaryToString($buf)."\n";
|
||||
$twenties=array(0x20, 0x20, 0x20, 0x20, 0x20, 0x20);
|
||||
|
||||
$message=binaryToString($buf);
|
||||
$macAddress = '';
|
||||
if (is_integer(strpos($message, 'accf'))) {
|
||||
$macAddress = substr($message, strpos($message, 'accf'),12);
|
||||
}
|
||||
if (!$macAddress) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo date('H:i:s')." MAC: ".$macAddress."\n";
|
||||
$command=substr($message, 8,4);
|
||||
echo "Command: $command \n";
|
||||
// $rec=SQLSelectOne("SELECT * FROM orvibodevices WHERE MAC LIKE '".DBSafe($macAddress)."'");
|
||||
if (!$rec['ID'] && $command!='7161') {
|
||||
echo date('H:i:s')." Unknown device.";
|
||||
return;
|
||||
}
|
||||
if ($command=='7161') { // We've asked for all sockets on the network, and smoeone has replied!
|
||||
echo date('H:i:s')." Discover reply from $macAddress\n";
|
||||
if (is_integer(strpos($message, '4952443030'))) { //from a known AllOne (because IR00 appears in the packet)
|
||||
$rec['TYPE']=1;
|
||||
if (!$rec['TITLE']) {
|
||||
$rec['TITLE']='AllOne - '.$macAddress;
|
||||
}
|
||||
} elseif (is_integer(strpos($message, '534f433030'))) { //socket
|
||||
$rec['TYPE']=0;
|
||||
if (!$rec['TITLE']) {
|
||||
$rec['TITLE']='Socket - '.$macAddress;
|
||||
}
|
||||
} else {
|
||||
$rec['TYPE']=1;
|
||||
if (!$rec['TITLE']) {
|
||||
$rec['TITLE']='Unknown - '.$macAddress;
|
||||
}
|
||||
}
|
||||
echo $rec['TITLE']."\n";
|
||||
$rec['MAC']=$macAddress;
|
||||
$rec['IP']=$remote_ip;
|
||||
$rec['UPDATED']=date('Y-m-d H:i:s');
|
||||
$rec['status']=substr($message,-1);
|
||||
// AQUI ACTUALIZAMOS
|
||||
$enchufe[$macAddress] = $rec['status'];
|
||||
|
||||
if ($rec['ID']) {
|
||||
// SQLUpdate('orvibodevices', $rec);
|
||||
} else {
|
||||
// SQLInsert('orvibodevices', $rec);
|
||||
}
|
||||
|
||||
//subscribe to it
|
||||
//if (!$this->subscribed[$rec['MAC']] || ((time()-$this->subscribed[$rec['MAC']])>30)) {
|
||||
$macReversed=array_reverse(HexStringToArray($rec['MAC']));
|
||||
$payload = makePayload(array(0x68, 0x64, 0x00, 0x1e, 0x63, 0x6c)).makePayload(HexStringToArray($rec['MAC'])).makePayload($twenties).makePayload($macReversed).makePayload($twenties);
|
||||
echo date('H:i:s')." Sending subscribe request: ".binaryToString($payload)."\n";
|
||||
socket_sendto($sock, $payload, strlen($payload), 0, $rec['IP'], $orvibo['port']);
|
||||
$subscribed[$rec['MAC']]=time();
|
||||
//query for name (optional)
|
||||
/*
|
||||
$payload = $this->makePayload(array(0x68, 0x64, 0x00, 0x1d, 0x72, 0x74));
|
||||
$payload.=$this->makePayload($this->HexStringToArray($rec['MAC']));
|
||||
$payload.=$this->makePayload($twenties);
|
||||
$payload.=$this->makePayload(array(0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
|
||||
echo date('H:i:s')." Sending name request: ".$this->binaryToString($payload)."\n";
|
||||
socket_sendto($sock, $payload, strlen($payload), 0, $rec['IP'], $this->port);
|
||||
*/
|
||||
//}
|
||||
|
||||
|
||||
} elseif ($command=='7274') { // We've queried the socket for the name, and we've got data coming back
|
||||
echo date('H:i:s')." Name reply from $macAddress\n";
|
||||
$tmp=explode('202020202020', $message);
|
||||
$strName=$tmp[4];
|
||||
if(strName == "ffffffffffffffffffffffffffffffff") {
|
||||
$strName='Orvibo '.$rec['MAC'];
|
||||
if ($rec['TYPE']==0) {
|
||||
$strName.=" (socket)";
|
||||
}
|
||||
} else {
|
||||
$strName=trim(HexStringToString($strName));
|
||||
}
|
||||
if (!$rec['TITLE']) {
|
||||
$rec['TITLE']=$strName;
|
||||
// SQLUpdate('orvibodevices', $rec);
|
||||
}
|
||||
|
||||
} elseif ($command=='636c') { // We've asked to subscribe to an AllOne, and this is confirmation.
|
||||
echo date('H:i:s')." Subscription reply from $macAddress\n";
|
||||
} elseif ($command=='6463') { // We've asked to change our state, and it's happened
|
||||
echo date('H:i:s')." State change reply from $macAddress\n";
|
||||
} elseif ($command=='6469') { // Possible button press, or just a ping thing?
|
||||
echo date('H:i:s')." Button pressed from $macAddress\n";
|
||||
if ($rec['LINKED_OBJECT'] && $rec['LINKED_METHOD_BUTTON']) {
|
||||
$params=array();
|
||||
$params['VALUE']=$rec['VALUE_IR'];
|
||||
callMethod($rec['LINKED_OBJECT'].'.'.$rec['LINKED_METHOD_BUTTON'], $params);
|
||||
}
|
||||
} elseif ($command=='7366') { // Something has changed our socket state externally
|
||||
echo date('H:i:s')." Socket state changed from $macAddress\n";
|
||||
if ($rec['TYPE']==0) {
|
||||
$state=substr($message, strlen($message)-1, 1);
|
||||
$this->statusChanged($rec['ID'], (int)$state);
|
||||
}
|
||||
} elseif ($command=='6963') { // We've asked to emit some IR and it's been done.
|
||||
echo date('H:i:s')." Emit IR done from $macAddress\n";
|
||||
} elseif ($command=='6c73') { // We're in learning mode, and we've got some IR back!
|
||||
echo date('H:i:s')." IR learning mode done from $macAddress\n";
|
||||
if (substr($message, 4, 4)!='0018') {
|
||||
$code=substr($message, 52);
|
||||
echo date('H:i:s')." Code: ".$code."\n";
|
||||
$rec['VALUE_IR']=$code;
|
||||
$rec['UPDATED']=date('Y-m-d H:i:s');
|
||||
SQLUpdate('orvibodevices', $rec);
|
||||
|
||||
if ($rec['LINKED_OBJECT'] && $rec['LINKED_PROPERTY']) {
|
||||
setGlobal($rec['LINKED_OBJECT'].'.'.$rec['LINKED_PROPERTY'], $rec['VALUE_IR'], array($this->name=>'0'));
|
||||
}
|
||||
|
||||
if ($rec['LINKED_OBJECT'] && $rec['LINKED_METHOD']) {
|
||||
$params=array();
|
||||
$params['VALUE']=$rec['VALUE_IR'];
|
||||
callMethod($rec['LINKED_OBJECT'].'.'.$rec['LINKED_METHOD'], $params);
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
echo date('H:i:s')." Ignoring result\n";
|
||||
}
|
||||
} else {
|
||||
echo date('H:i:s')." Unknown command: $command \n";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
158
S20/readme.txt
Normal file
158
S20/readme.txt
Normal file
@ -0,0 +1,158 @@
|
||||
[Documentación en inglés y español]
|
||||
|
||||
Orvibo S20 Domoticz
|
||||
======================
|
||||
|
||||
I made some slight modifications to adapt the PHP programming code from third parties to Domoticz.
|
||||
Thanks for the code to pcp135 / Orvibo https://github.com/pcp135/Orvibo (Orvibo Class for switching plugs) and
|
||||
sergejey / butler-orvibo https://github.com/sergejey/majordomo-orvibo (code to obtain the status)
|
||||
|
||||
The work I did was test the code of pcp135 to get it to work and create a small script to communicate with Domoticz.
|
||||
On the code of sergejey what I did was break a bit the code by removing a lot of to get communication with switchs to know the status and send it to Domoticz.
|
||||
Since I do not have time, I can not improve it, clean it and put everything in a nice class, but the important thing is that it works.
|
||||
|
||||
NEEDS
|
||||
---------
|
||||
Its need Apache + PHP on the same machine as Domoticz. In my case, it is a Windows Domoticz on port 8080. I installed XAMPP on port 80 and 444 (SSL 443 was busy and Apache did not start).
|
||||
Important: S20 Orvibo outlets must be previously configured for the network that will work.
|
||||
|
||||
|
||||
THE CODE
|
||||
--------
|
||||
The code consists of two parts:
|
||||
|
||||
1. Orvibo.php and s20.php
|
||||
With these codes, we can turn on and off Orvibo plugs S20.
|
||||
We must know the MAC address of the socket (you can use any network scanning as netscan program or command line program from Andrius Štikonas https://stikonas.eu/wordpress/2015/02/24/reverse-engineering-orvibo-s20 -socket / or perhaps even the other side of the module).
|
||||
Create the folder s20 in the root of our apache server and copy the files. It will use the URL:
|
||||
http://IP-DOMOTICZ/s20/s20.php?mac=accf23514ddd&accion=on
|
||||
http://IP-DOMOTICZ/s20/s20.php?mac=accf23514ddd&accion=off
|
||||
At this point you must turn on and off quickly any plug.
|
||||
|
||||
2. cron.php and cron.bat (windows)
|
||||
This code is responsible for launching a status request to the network by UDP port 10000 and listening for a response, it generates an array and sends the status of the plugs to Domoticz. This is needed if we turn on the plug or turn off manually and want to update their status to Domoticz (JSON).
|
||||
We can try to launch the URL and it should return debug code.
|
||||
http://IP-DOMOTICZ/s20/cron.php
|
||||
|
||||
|
||||
USE DOMOTICZ
|
||||
--------
|
||||
Step 1: Create a virtual switch through Setup > Hardware.
|
||||
Name: Orvibo
|
||||
Type: Dummy
|
||||
|
||||
Step 2: Create as many virtual switches and outlets Orvibo S20 we have (remember that plugs should already be pre-configured and running on your network), assign them a name and write down IDX (Setup > Devices).
|
||||
|
||||
We have our dummy plugs that do absolutely nothing.
|
||||
|
||||
Step 3: Include switches and assign a name through Setup > Devices > (Arrow). The switches will appear in Switches section but still does not work.
|
||||
|
||||
Step 4: Editing a plug and fill On Action and Off Action with:
|
||||
http://localhost/s20/s20.php?mac=MACADDRESS01&accion=on
|
||||
http://localhost/s20/s20.php?mac=MACADDRESS01&accion=off
|
||||
Where MACADDRESS01 is the MAC address of the plug we are configurin and have previously tried before throwing the URL and worked OK.
|
||||
Save it.
|
||||
|
||||
At this moment our plug works! Turns on and off quickly. We can make rules and play with it.
|
||||
|
||||
|
||||
Now you must run the CRON if the state outlet outside Domoticz changes manually. This requires modifying the PHP and configure it. I have set the CRON every 5 minutes (in my Windows machine).
|
||||
|
||||
|
||||
CONFIGURATION
|
||||
------
|
||||
cron.php must be configured manually to update the state correctly to Domoticz JSON.
|
||||
To do this, edit the cron.php and modify the lines regarding:
|
||||
$domoticz_server > the base URL of Domoticz, for example http://localhost:8080
|
||||
and several lines
|
||||
$mienchufe['accf23514ddd'] = '106'; // Description ddd
|
||||
$mienchufe['accf23514eee'] = '107'; // Description eee
|
||||
which it is an array with the MAC address and the IDX assigned to the virtual plug in Domoticz.
|
||||
|
||||
|
||||
Chim Pum!
|
||||
|
||||
Eduardo Pagán
|
||||
http://blog.eduardopagan.com/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Orvibo S20 a Domoticz
|
||||
======================
|
||||
|
||||
He hecho algunas modificaciones y pequeñas programaciones para adaptar el código PHP de terceros a Domoticz.
|
||||
Gracias por el código a pcp135 / Orvibo https://github.com/pcp135/Orvibo (Clase Orvibo para encender y apagar los enchufes) y
|
||||
sergejey / mayordomo-orvibo https://github.com/sergejey/majordomo-orvibo (código para conocer el estado de los mismos)
|
||||
|
||||
El trabajo que hice fue probar el código pcp135 hasta conseguir que funcionara y crear un pequeño script para la comunicación con Domoticz.
|
||||
Sobre el código de sergejey lo que hice fue romperlo un poco quitando mucho código hasta conseguir la comunicación con los enchufes para saber el estado y enviarlo a Domoticz.
|
||||
Como no dispongo de tiempo, no puedo mejorarlo, limpiarlo y meterlo todo en una bonita clase, pero lo importante es que funciona.
|
||||
|
||||
NECESIDADES
|
||||
---------
|
||||
Lo más cómodo es disponer de Apache+PHP en la misma máquina que Domoticz. En mi caso, es un Windows con Domoticz en el puerto 8080. He instalado XAMPP en el puerto 80 y 444 (ya que el 443 SSL estaba ocupado y no iniciaba).
|
||||
Importante: Los enchufes de Orvibo S20 previamente deben de estar configurados para la red en la que vamos a trabajar.
|
||||
|
||||
|
||||
EL CÓDIGO
|
||||
--------
|
||||
El código consta de dos partes:
|
||||
|
||||
1. Orvibo.php y s20.php
|
||||
Con estos códigos, podemos encender y apagar los enchufes Orvibo S20.
|
||||
Debemos conocer la dirección MAC del enchufe (podemos utilizar algún programa escaneo de red como netscan o el programa de línea de comandos de Andrius Štikonas https://stikonas.eu/wordpress/2015/02/24/reverse-engineering-orvibo-s20-socket/ o quizás incluso la otra parte del módulo).
|
||||
Crearemos la carpeta s20 dentro de la raíz de nuestro servidor apache y copiaremos los ficheros y emplearemos la URL:
|
||||
http://IP-DOMOTICZ/s20/s20.php?mac=accf23514ddd&accion=on
|
||||
http://IP-DOMOTICZ/s20/s20.php?mac=accf23514ddd&accion=off
|
||||
En este punto se debe encender y apagar con rapidez.
|
||||
|
||||
2. cron.php y cron.bat (windows)
|
||||
Este código se encarga de lanzar una petición de estado a la red por UDP puerto 10000 y se queda escuchando la respuesta, genera un array y envía el estado de los enchufes a Domoticz. Esto es por si encendemos el enchufe o lo apagamos de forma manual y queremos que se actualice su estado en Domoticz por JSON.
|
||||
Podemos probar a lanzar la URL y debería de retornar código debug.
|
||||
http://IP-DOMOTICZ/s20/cron.php
|
||||
|
||||
|
||||
USO EN DOMOTICZ
|
||||
--------
|
||||
Paso 1: Creamos un interruptor virtual a través de Setup > Hardware.
|
||||
Nombre: Orvibo
|
||||
Tipo: Dummy
|
||||
|
||||
Paso 2: Creamos tantos interruptores virtuales como enchufes Orvibo S20 tengamos (recuerdo que ya deben estar preconfigurados y funcionando en la red), les asignamos un nombre y nos anotamos el IDX (Setup > Devices).
|
||||
|
||||
Ya tenemos nuestros enchufes Dummy que no hacen absolutamente nada.
|
||||
|
||||
Paso 3: Los incluimos en nuestros Switches y les ponemos nombre. Nos aparecerán en Switches pero seguirán sin hacer nada.
|
||||
|
||||
Paso 4: Editamos un enchufe y rellenamos On Action y Off Action con:
|
||||
http://localhost/s20/s20.php?mac=MACADDRESS01&accion=on
|
||||
http://localhost/s20/s20.php?mac=MACADDRESS01&accion=off
|
||||
Donde MACADDRESS01 es la dirección MAC de ese enchufe y que previamente hemos probado antes lanzando a mano la URL y funcionaba OK.
|
||||
Guardamos.
|
||||
|
||||
¡Desde este momento nuestro enchufe ya funciona! Enciende y apaga rápidamente. Ya podemos hacer reglas y jugar con él.
|
||||
|
||||
|
||||
Ahora hay que ejecutar el CRON por si se modifica el estado del enchufe de forma física fuera de Domoticz. Para ello hay que modificar el PHP y configurarlo. Yo he puesto el CRON cada 5 minutos en Windows. Cada uno que lo haga como quiera (si no se sabe, buscar en Google).
|
||||
Si se utiliza el .bat habrá que modificar las rutas.
|
||||
|
||||
|
||||
CONFIGURACIÓN
|
||||
------
|
||||
En el caso del cron.php hay que configurarlo de forma manual para que actualice el estado correctamente a Domoticz por JSON.
|
||||
Para ello editamos el cron.php y modificamos las líneas referentes a:
|
||||
$domoticz_server donde indicaremos la URL base de Domoticz, por ejemplo http://localhost:8080
|
||||
y varias líneas de
|
||||
$mienchufe['accf23514ddd'] = '106'; // Description
|
||||
$mienchufe['accf23514eee'] = '107'; // Description
|
||||
que es un array donde se indica la MAC y la IDX asignada en Domoticz a ese enchufe virtual.
|
||||
|
||||
|
||||
Chim pum!
|
||||
|
||||
Eduardo Pagán
|
||||
http://blog.eduardopagan.com/
|
43
S20/s20.php
Normal file
43
S20/s20.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
// Plugin para DomoticZ - Orvibo S20
|
||||
// Eduardo Pagán http://blog.eduardopagan.com
|
||||
|
||||
require 'Orvibo.php';
|
||||
|
||||
$ip = $_REQUEST["ip"];
|
||||
$mac = $_REQUEST["mac"];
|
||||
$accion = $_REQUEST["accion"];
|
||||
|
||||
if ( !$mac ) {
|
||||
die('You must indicate MAC address');
|
||||
}
|
||||
|
||||
$mac = strtoupper($mac);
|
||||
$mac0 = (substr($mac,0,2));
|
||||
$mac1 = (substr($mac,2,2));
|
||||
$mac2 = (substr($mac,4,2));
|
||||
$mac3 = (substr($mac,6,2));
|
||||
$mac4 = (substr($mac,8,2));
|
||||
$mac5 = (substr($mac,10,2));
|
||||
|
||||
eval("\$mac0 = 0x$mac0;");
|
||||
eval("\$mac1 = 0x$mac1;");
|
||||
eval("\$mac2 = 0x$mac2;");
|
||||
eval("\$mac3 = 0x$mac3;");
|
||||
eval("\$mac4 = 0x$mac4;");
|
||||
eval("\$mac5 = 0x$mac5;");
|
||||
|
||||
if ( !$ip ) {
|
||||
$ip = '255.255.255.255';
|
||||
}
|
||||
|
||||
$orvibo = new Orvibo($ip,'10000',
|
||||
array(($mac0),($mac1),($mac2),($mac3),($mac4),($mac5)));
|
||||
if ( $accion == 'on' ) {
|
||||
$orvibo->on();
|
||||
} elseif ( $accion == 'off' ) {
|
||||
$orvibo->off();
|
||||
}
|
||||
|
||||
echo $accion;
|
Loading…
x
Reference in New Issue
Block a user