Example #1
0
<?php

include '../vendor/autoload.php';
use Att\M2X\MQTT\MQTTClient;
use Att\M2X\MQTT\Error\M2XException;
$apiKey = getenv("API_KEY");
$deviceId = getenv("DEVICE");
$m2x = new MQTTClient($apiKey);
$m2x->connect();
# Get the device
$device = $m2x->device($deviceId);
# Create the streams if they don't exist yet
$device->updateStream('load_1m');
$device->updateStream('load_5m');
$device->updateStream('load_15m');
while (true) {
    list($load_1m, $load_5m, $load_15m) = sys_getloadavg();
    $now = date('c');
    $values = array('load_1m' => array(array('value' => $load_1m, 'timestamp' => $now)), 'load_5m' => array(array('value' => $load_5m, 'timestamp' => $now)), 'load_15m' => array(array('value' => $load_15m, 'timestamp' => $now)));
    try {
        $device->postUpdates($values);
    } catch (M2XException $ex) {
        echo 'Error: ' . $ex->getMessage();
        echo $ex->response->raw;
        break;
    }
    sleep(10);
}
Example #2
0
        $reason = 'The "message" param is required in data';
        $command->reject(compact('reason'));
    }
}
function process_report_command($command)
{
    $public_ip = getHostByName(php_uname('n'));
    $pid = (string) getmypid();
    print sprintf("REPORT: public_ip: %s, pid: %s\n\r", $public_ip, $pid);
    $command->process(compact('public_ip', 'pid'));
}
try {
    $client = new MQTTClient($apiKey, array('clientId' => 'PHP Test Client', 'host' => gethostbyname($hostname)));
    $client->connect();
    echo "Connected to the broker\n\r";
    $device = $client->device($deviceId);
    //Check for unacknowledged commands
    $commands = $device->commands(array('status' => 'pending', 'limit' => 100));
    foreach ($commands as $command) {
        $command->refresh();
        process_command($command);
    }
    //Listen for incoming commands
    while ($command = $device->receiveCommand()) {
        process_command($command);
    }
    $client->disconnect();
} catch (Exception $ex) {
    echo sprintf('Exception Error: %s', $ex->getMessage());
    throw $ex;
}