示例#1
1
    $host = 'localhost';
}
if ($mqtt->config['MQTT_PORT']) {
    $port = $mqtt->config['MQTT_PORT'];
} else {
    $port = 1883;
}
if ($mqtt->config['MQTT_QUERY']) {
    $query = $mqtt->config['MQTT_QUERY'];
} else {
    $query = '/var/now/#';
}
$mqtt_client = new phpMQTT($host, $port, "MajorDoMo MQTT Client");
if (!$mqtt_client->connect()) {
    exit(1);
}
//$topics['/dev/Node1/Ai6'] = array("qos"=>0, "function"=>"procmsg");
//$topics['/dev/#'] = array("qos"=>0, "function"=>"procmsg");
$topics[$query] = array("qos" => 0, "function" => "procmsg");
$mqtt_client->subscribe($topics, 0);
while ($mqtt_client->proc()) {
}
$mqtt_client->close();
function procmsg($topic, $msg)
{
    global $mqtt;
    $mqtt->processMessage($topic, $msg);
    echo date("Y-m-d H:i:s") . " Topic:{$topic} {$msg}\n";
}
$db->Disconnect();
// closing database connection
<?php

require "Lib/phpMQTT.php";
$mqtt = new phpMQTT("127.0.0.1", 1883, "Emoncms feed subscriber");
if (!$mqtt->connect()) {
    exit(1);
}
$topics["emoncms/#"] = array("qos" => 0, "function" => "procmsg");
$mqtt->subscribe($topics, 0);
while ($mqtt->proc()) {
}
$mqtt->close();
function procmsg($topic, $value)
{
    $time = time();
    print $topic . " " . $value . "\n";
}
示例#3
0
$config = load_config();
// ----------------------------------------------------------------
// MQTT Client
// ----------------------------------------------------------------
require "Lib/phpMQTT.php";
$mqtt = new phpMQTT("127.0.0.1", 1883, "Emoncms input subscriber");
if (!$mqtt->connect()) {
    exit(1);
}
$topics = array();
$topics[$topic] = array("qos" => 0, "function" => "procmsg");
$log->info("subscribing to {$topic}");
$mqtt->subscribe($topics, 0);
$last = time();
while (true) {
    $mqtt->proc(0);
    usleep(100000);
    // Reload config every 5 seconds
    $now = time();
    if ($now - $last > 5.0) {
        $log->info("Reloading config");
        $last = $now;
        $config = load_config();
    }
}
$mqtt->close();
// ----------------------------------------------------------------
function procmsg($topic, $input)
{
    global $log, $redis, $config, $emoncms_config_file, $emonhub_config_file, $process, $feed;
    $log->info("Received mqtt message: {$topic} {$input}");
$config['client_id'] = 'a:' . $config['org_id'] . ':' . $config['app_id'];
$location = array();
// initialize client
$mqtt = new phpMQTT($config['server'], $config['port'], $config['client_id']);
$mqtt->debug = false;
// connect to broker
if (!$mqtt->connect(true, null, $config['iotf_api_key'], $config['iotf_api_secret'])) {
    echo 'ERROR: Could not connect to IoT cloud';
    exit;
}
// subscribe to topics
$topics['iot-2/type/+/id/' . $config['device_id'] . '/evt/accel/fmt/json'] = array('qos' => $config['qos'], 'function' => 'getLocation');
$mqtt->subscribe($topics, $config['qos']);
// process messages
$elapsedSeconds = 0;
while ($mqtt->proc(true)) {
    if (count($location) == 2) {
        $latitude = $location[0];
        $longitude = $location[1];
        $mapsApiUrl = 'https://maps.googleapis.com/maps/api/staticmap?key=' . $config['maps_api_key'] . '&size=640x480&maptype=roadmap&scale=2&markers=color:green|' . sprintf('%f,%f', $latitude, $longitude);
        break;
    }
    if ($elapsedSeconds == 5) {
        break;
    }
    sleep(1);
    $elapsedSeconds++;
}
// disconnect
$mqtt->close();
function getLocation($topic, $msg)
示例#5
0
<?php

include "resources/phpMQTT.php";
error_reporting(0);
$results = [];
$topics = "WEATHER/a,AHMS/pir,AHMS/move,AHMS/door";
if (isset($_GET["topic"])) {
    $topics = $_GET["topic"];
}
$mqtt = new phpMQTT("192.168.1.20", 1883, "PHP MQTT Client", "ahmsclient", "ahms2013");
if ($mqtt->connect()) {
    foreach (explode(",", $topics) as $topic) {
        $myTopics = [];
        $myTopics[$topic] = array("qos" => 0, "function" => "procmsg");
        $mqtt->subscribe($myTopics);
        if ($mqtt->proc() == 0) {
            array_push($results, array("status" => "no message", "topic" => $topic, "message" => ""));
        }
    }
    $mqtt->close();
} else {
    array_push($results, array("status" => "ok", "no connection" => $topic, "message" => ""));
}
echo json_encode($results);
function procmsg($topic, $message)
{
    global $results;
    array_push($results, array("status" => "ok", "topic" => $topic, "message" => $message));
}