Back

How do I send an event from PHP

There are two ways to do this. Either you connect to the TCP port or send a VSCP broadcast.

VSCP Broadcast

This code snippte send a CLASS1.INFORMATION TYPE=3 ON Event using an UDP datagram.

<?
  $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  socket_set_option( $socket,
                       SOL_SOCKET,  // socket level
                       SO_BROADCAST,
                       1 );
 
  // create the request packet
  $head = 0;
 
  $class = 20;   // LEVEL I Information
  $type = 4;     // ON Event
 
  // We use GUID for LAB usage
  $GUID[ 0 ] = 0;
  $GUID[ 1 ] = 0;
  $GUID[ 2 ] = 0;
  $GUID[ 3 ] = 0;
  $GUID[ 4 ] = 0;
  $GUID[ 5 ] = 0;
  $GUID[ 6 ] = 0;
  $GUID[ 7 ] = 0;
  $GUID[ 8 ] = 0;
  $GUID[ 9 ] = 0;
  $GUID[ 10 ] = 0;
  $GUID[ 11 ] = 0;
  $GUID[ 12 ] = 0;
  $GUID[ 13 ] = 0;
  $GUID[ 14 ] = 0;
  $GUID[ 15 ] = 0;
 
  $size = 3;
  $data[ 0 ] = 0;
  $data[ 1 ] = 1;   // Zone 1
  $data[ 2 ] = 80;  // Sub Zone 80
 
  $crc = 0;
 
  $packet = chr( $head ) .
                chr( $class / 256 ) .
                chr ( $class & 255 ) .
                chr( $type / 256 ) .
                chr ( $type & 255 );
  for ( $i=0; $i<16; $i++ ) {
    $packet = $packet . chr( $GUID[ $i ] );
  }
 
  $packet = $packet . chr( $size / 256 ) . chr( $size & 255 );
 
  for ( $i=0; $i<$size; $i++ ) {
    $packet = $packet . chr( $data[ $i ] );
  }
 
  $crc = calculate_common_crc16c( $packet );
 
  $packet = $packet . chr( $crc / 256 ) . chr( $crc & 255 );
 
  // UDP is connectionless, so we just send on it.
  socket_sendto($socket, $packet, ( 25 + $size ), 0x100, "255.255.255.255", 9598 );
 
 
// this function is used to calculate the CCITT crc16c for an entire buffer
function calculate_common_crc16c( $buffer )
{
   $crc16c = 0xffff;  // the crc initial value
 
   $buffer_length = strlen( $buffer );
 
   for ($i = 0; $i < $buffer_length; $i++)
   {
       $ch = ord($buffer[$i]);
 
       $crc16c = update_common_crc16c($ch, $crc16c);
   }
 
   return $crc16c;
}
?>

VSCP TCP Connection

The following code does the same thing as above but use the TCP interface of the daemon

<?php
 
  printf("<h1><h2>TCP/IP Connection</h2></h1>");
 
  $service_port = getservbyname('www', 'tcp');
  $service_port = 9598;
 
  $address = gethostbyname('192.168.1.6');
 
  $socket = socket_create (AF_INET, SOCK_STREAM, 0);
  if ($socket < 0) {
    echo "socket_create() failed: reason: " . socket_strerror ($socket) . "\n" . "<br>";
  } else {
    echo "OK.\n" . "<br>";
  }
 
  echo "Attempting to connect to '$address' on port '$service_port'...";
  $result = socket_connect ($socket, $address, $service_port);
  if ($result < 0) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n" . "<br>";
  } else {
    echo "OK.\n" . "<br>";
  }
 
  // Read response
  $out = socket_read ($socket, 2048);
  echo $out . "<br>";
 
  $in = "USER admin\n";
  echo "Sending username...";
  socket_write ($socket, $in, strlen ($in));
  echo "OK.\n" . "<br>";
 
  // Read response
  $out = socket_read ($socket, 2048);
  echo $out . "<br>";
 
  $in = "PASS secret\n";
  echo "Sending password...";
  socket_write ($socket, $in, strlen ($in));
  echo "OK.\n" . "<br>";
 
  // Read response
  $out = socket_read ($socket, 2048);
  echo $out . "<br>";
 
  // send head,class,type,obid,timestamp,GUID,data1,data2,data3....
  // class=20 - Information
  // Type=4 - OFF
  // Zone=1
  // SubZone=80
  $in = "SEND 0,20,4,0,0,0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,0,1,80\n";
  echo "Sending CLASS1.INFORMATION TYPE=ON...";
  socket_write ($socket, $in, strlen ($in));
  echo "OK.\n" . "<br>";
 
  echo "Closing socket...";
  socket_close ($socket);
  echo "OK.\n\n";
 
?>
 
howto/how_do_i_send_an_event_from_php.txt · Last modified: 2014/03/27 06:22 (external edit)
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki