Example #1
0
<?php

require 'SAM/php_sam.php';
define("SAM_HOST", "SAM_HOST");
define("SAM_PORT", "SAM_PORT");
define("SAM_WAIT", "SAM_WAIT");
define("SAM_MQTT_QOS", "SAM_MQTT_QOS");
//create a new connection object
$conn = new SAMConnection();
//start initialise the connection
$conn->connect(SAM_MQTT, array(SAM_HOST => "test.mosquitto.org", SAM_PORT => 1883));
//subscribe to topic cpu
$subUp = $conn->subscribe('topic://cpupc1-asitha', array(SAM_WAIT => 1, SAM_MQTT_QOS => 0)) or die('could not subscribe');
//print confirmation to terminal
echo "subscribed";
$i = 0;
$prev = "";
while ($conn) {
    //receive latest message on topic $subUp
    $msgUp = $conn->receive($subUp);
    //if($msgUp==true)
    /* echo $conn->errno;
    		 echo $conn->error;
    		 echo $conn->debug; */
    //if there is a message
    if ($msgUp) {
        if (!$msgUp->body == "") {
            //echo message to terminal
            // echo $msgUp->body;
            //if ($msgUp->body!=$prev)
            //  {
    $msg = $conn->receive('invalid sub name', array(SAM_WAIT => 5000));
    if (!$msg) {
        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 subscribe -<br>
<?php 
if ($conn) {
    $sub = $conn->subscribe($queuename);
    if ($sub) {
        echo '<font color="green"><b>OK</b></font> subscribe suceeded sub id=' . $sub . '<br>';
    } else {
        echo '<font color="red"><b>FAILED</b></font> Subscribe failed  with errno=' . $conn->errno . ' ' . $conn->error . '<br>';
    }
} else {
    echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!';
}
?>
<p/>
<hr/>
Attempting receive via subscription -<br>
<?php 
if ($conn) {
    $msg = $conn->receive($sub, array(SAM_WAIT => 5000));
Example #3
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'));
$subUp = $conn->subscribe('topic://noderx') or die('could not subscribe');
while ($conn) {
    $msgUp = $conn->receive($subUp);
    if ($msgUp) {
        $body = $msgUp->body . "\n";
        print $body;
    }
}
Example #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'));
$subUp = $conn->subscribe('topic://serialread') or die('could not subscribe');
while ($conn) {
    $msgUp = $conn->receive($subUp);
    $body = $msgUp->body;
    echo $body;
}
Example #5
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));
$subUp = $conn->subscribe('topic://state') or die('could not subscribe');
$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";
}
$serial = fopen($filename, "r+", false, $c);
stream_set_timeout($f, 0, 1000);
while ($conn && $serial) {
    $msgUp = $conn->receive($subUp);
    if ($msgUp) {
        $val = $msgUp->body;
        if ($val) {
            echo $val . "\n";
            $val = (int) $val;
            // Convert to csv byte values
            $p2 = $val >> 8;
            $p1 = $val - ($p2 << 8);
            $str = $p1 . "," . $p2 . ",";
            // Send to rfm12pi running RFM12Demo
            fprintf($serial, $str . "s");
            usleep(100);
Example #6
0
$redis = new Redis();
$redis->connect("127.0.0.1");
require "Modules/user/user_model.php";
$user = new User($mysqli, $redis, null);
include "Modules/feed/feed_model.php";
$feed = new Feed($mysqli, $redis, $feed_settings);
require "Modules/input/input_model.php";
// 295
$input = new Input($mysqli, $redis, $feed);
require "Modules/input/process_model.php";
// 886
$process = new Process($mysqli, $input, $feed);
require "SAM/php_sam.php";
$conn = new SAMConnection();
$conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => '1883'));
$subUp = $conn->subscribe('topic://emoncms/input') or die('could not subscribe');
while ($conn) {
    $msgUp = $conn->receive($subUp);
    if ($msgUp && $msgUp->body) {
        $m = json_decode($msgUp->body);
        print $msgUp->body;
        $time = $m->time;
        $nodeid = $m->node;
        $data = $m->csv;
        $dbinputs = $input->get_inputs($userid);
        $tmp = array();
        foreach ($data as $name => $value) {
            if ($input->check_node_id_valid($nodeid)) {
                if (!isset($dbinputs[$nodeid][$name])) {
                    $inputid = $input->create_input($userid, $nodeid, $name);
                    $dbinputs[$nodeid][$name] = true;