示例#1
1
<?php

require 'SAM/php_sam.php';
//create a new connection object
$conn = new SAMConnection();
//start initialise the connection
$conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => 1883));
//create a new MQTT message with the output of the shell command as the body
$msgCpu = new SAMMessage($_REQUEST['message']);
//send the message on the topic cpu
$conn->send('topic://' . $_REQUEST['target'], $msgCpu);
$conn->disconnect();
echo 'MQTT Message to ' . $_REQUEST['target'] . ' sent: ' . $_REQUEST['message'];
示例#2
0
<?php

// Saved state of control packet
$packet = array('setpoint' => array('type' => 'int', 'value' => 16.5));
$packet['setpoint']['value'] = (double) $_GET['val'];
echo "State set to: " . $packet['setpoint']['value'];
require 'SAM/php_sam.php';
$conn = new SAMConnection();
$conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => 1883));
$msg_state = new SAMMessage($packet['setpoint']['value']);
$conn->send('topic://state', $msg_state);
$conn->disconnect();
示例#3
0
if ($conn) {
    $msg = new SAMMessage('This is a test message');
    $msg->header->SAM_REPLY_TO = 'queue://test/replyQueue';
    $msg->header->SAM_TYPE = SAM_TEXT;
    $msg->header->myStringProp1 = array('a string property', SAM_STRING);
    $msg->header->myStringProp2 = 'another string property';
    $msg->header->contentType = 'text/xml: charset=utf-8';
    $msg->header->transportVersion = 1;
    $msg->header->myBoolProp = array(FALSE, SAM_BOOL);
    $msg->header->myIntProp = array(32768, SAM_INT);
    $msg->header->myLongProp = array(9876543, SAM_LONG);
    $msg->header->myByteProp1 = array(123, SAM_BYTE);
    $msg->header->myByteProp2 = array('12', SAM_BYTE);
    $msg->header->myFloatProp = array(3.141592653589793, SAM_FLOAT);
    $msg->header->myDoubleProp = array(3.141592653589793, SAM_DOUBLE);
    $correlid = $conn->send($queuename, $msg);
    if (!$correlid) {
        echo '<font color="red"><b>FAILED</b></font>' . " ({$conn->errno}) {$conn->error}";
    } else {
        echo '<font color="green"><b>OK</b></font> correlID=' . $correlid;
    }
} else {
    echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!';
}
?>
<p/>
<hr/>
Attempting 2nd send -
<?php 
if ($conn) {
    $msg = new SAMMessage('This is a 2nd test message');
示例#4
0
<?php

error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
require 'SAM/php_sam.php';
$conn = new SAMConnection();
$conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => 1883));
$i = 0;
while (true) {
    if (time() - $ltime > 1) {
        $ltime = time();
        print $i . "\n";
        $i = 0;
    }
    $msg_rawserial = new SAMMessage("hello");
    $conn->send('topic://myqueue', $msg_rawserial);
    $i++;
}
<?php

/*
@info
Αρχείο για την ειδοποίηση των οδηγών ότι τον επέλεξε ο πελάτης και είναι έτοιμος για παραλαβή
*/
header('Content-Type: text/html; charset=utf-8');
require 'SAM/php_sam.php';
require 'config.php';
$conn = new SAMConnection();
//type:1 = notifyDrivers
//type:2 = message for acception
if (isset($_REQUEST["customerlat"]) && isset($_REQUEST["customerlon"]) && isset($_REQUEST["driverDevIdtoSend"]) && isset($_REQUEST["selectedOrderid"])) {
    $message = "2" . ":" . "Πελάτης για παραλαβή" . ":" . $_REQUEST["customerlat"] . ":" . $_REQUEST["customerlon"] . ":" . $_REQUEST["driverDevIdtoSend"] . ":" . $_REQUEST["selectedOrderid"];
    $target = $_REQUEST["driverDevIdtoSend"];
} else {
    $message = "";
    $target = "";
}
$conn->connect(SAM_MQTT, $SAM_MQTTCONF);
$conn->send('topic://' . $target, $message);
$conn->disconnect();
//for debug
function write2file($string)
{
    $file = fopen("test.txt", "a");
    fwrite($file, $string);
    fclose($file);
}
示例#6
0
<?php 
$conn = new SAMConnection();
if ($conn->connect(SAM_WPM, array(SAM_ENDPOINTS => $endpoints, SAM_BUS => $bus, SAM_TARGETCHAIN => $target, SAM_TRANSACTIONS => SAM_MANUAL))) {
    echo '<font color="green"><b>OK</b></font>';
} else {
    echo '<font color="red"><b>FAILED</b></font>';
}
?>
<p/>
<hr/>
Sending test message -
<?php 
if ($conn) {
    $msg = new SAMMessage('This is a test message');
    $msg->header->SAM_TYPE = SAM_TEXT;
    $correlid = $conn->send($queuename, $msg);
    if (!$correlid) {
        echo '<font color="red"><b>FAILED</b></font>' . " ({$conn->errno}) {$conn->error}";
    } else {
        echo '<font color="green"><b>OK</b></font> correlID=' . $correlid;
    }
} else {
    echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!';
}
?>
<p/>
<hr/>
Attempting 2nd send -
<?php 
if ($conn) {
    $msg = new SAMMessage('This is a 2nd test message');
        echo '<font color="green"><b>OK</b></font> receive failed with errno=' . $conn->errno . ' ' . $conn->error . '<br>';
    } else {
        echo '<font color="red"><b>FAILED</b></font> Should not have received a message!';
    }
} else {
    echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!';
}
?>
<p/>
<hr/>
Attempting send to topic -<br>
<?php 
if ($conn1) {
    $msg = new SAMMessage('This is a test message on topic test/x/y');
    $msg->header->SAM_TYPE = SAM_TEXT;
    $correlid = $conn1->send($queuename, $msg);
    if (!$correlid) {
        echo '<font color="red"><b>FAILED</b></font>' . " ({$conn->errno}) {$conn->error}";
    } else {
        echo '<font color="green"><b>OK</b></font> correlID=' . $correlid;
    }
} else {
    echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!';
}
?>
<p/>
<hr/>
Attempting receive via subscription again -<br>
<?php 
if ($conn) {
    $msg = $conn->receive($sub, array(SAM_WAIT => 10000));
示例#8
0
$conn = new SAMConnection();
$conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => 1883));
$c = stream_context_create(array('dio' => array('data_rate' => 9600, 'data_bits' => 8, 'stop_bits' => 1, 'parity' => 0, 'flow_control' => 0, 'is_canonical' => 1)));
if (PATH_SEPARATOR != ";") {
    $filename = "dio.serial:///dev/ttyUSB0";
} else {
    $filename = "dio.serial://dev/ttyUSB0";
}
$serial = fopen($filename, "r+", false, $c);
stream_set_timeout($f, 0, 1000);
while ($conn && $serial) {
    $data = fgets($serial);
    if ($data && $data != "\n") {
        if ($data[0] == 'O' && $data[1] == 'K') {
            echo "DATA RX:" . $data;
            $parts = explode(' ', $data);
            $nodeid = (int) $parts[1];
            // Pack individual bytes into a binary string
            $bin = "";
            for ($i = 2; $i < count($parts); $i++) {
                $bin .= pack("C", intval($parts[$i]));
            }
            // Unpack the binary string into signed shorts
            $values = unpack("s*", $bin);
            echo "  Decoded Node: {$nodeid}:[" . implode($values, ',') . "]\n";
            $msg_state = new SAMMessage(implode($values, ','));
            $conn->send('topic://serialread', $msg_state);
        }
    }
}
fclose($serial);
示例#9
0
<?php

// Read from serial with data coming from RFM12PI with RFM12_Demo sketch
// All Emoncms code is released under the GNU Affero General Public License.
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
require 'SAM/php_sam.php';
$conn = new SAMConnection();
$conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => 1883));
$c = stream_context_create(array('dio' => array('data_rate' => 9600, 'data_bits' => 8, 'stop_bits' => 1, 'parity' => 0, 'flow_control' => 0, 'is_canonical' => 1)));
// ttyAMA0 is the standard serial port used on the raspberrypi
// Depending on your machine you may need to change this to i.e: /dev/ttyUSB0
if (PATH_SEPARATOR != ";") {
    $filename = "dio.serial:///dev/ttyUSB0";
} else {
    $filename = "dio.serial://dev/ttyUSB0";
}
$f = fopen($filename, "r+", false, $c);
stream_set_timeout($f, 0, 1000);
while (true) {
    $data = fgets($f);
    if ($data && $data != "\n") {
        echo "DATA RX:" . $data;
        $msg_rawserial = new SAMMessage($data);
        $conn->send('topic://rawserial', $msg_rawserial);
    }
}
fclose($f);