Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
158 lines (113 sloc) 5.25 KB

Latest Stable Version Build Status License

AT&T's M2X PHP (MQTT) Client

AT&T M2X is a cloud-based fully managed time-series data storage service for network connected machine-to-machine (M2M) devices and the Internet of Things (IoT).

The AT&T M2X API provides all the needed operations and methods to connect your devices to AT&T's M2X service. This library aims to provide a simple wrapper to interact with the AT&T M2X API for PHP over the MQTT protocol. Refer to the Glossary of Terms to understand the nomenclature used throughout this documentation.

Getting Started

  1. Signup for an M2X Account.
  2. Obtain your Master Key from the Master Keys tab of your Account Settings screen.
  3. Create your first Device and copy its Device ID.
  4. Review the M2X API Documentation.


Simply add a dependency on attm2x/m2x-php-mqtt to your project's composer.json file if you use Composer to manage the dependencies of your project.

  "require": {
    "attm2x/m2x-php-mqtt": "~2.0"


In order to communicate with the M2X API, you need an instance of MQTTClient. You need to pass the host and your API key in the constructor to access your data.

use Att\M2X\MQTT\MQTTClient;

$client = new MQTTClient('<YOUR API KEY>');

// Placeholder for next examples.


This provides an interface to the following endpoints for the M2X API:

  • Creating devices

    $data = array(
    	'name' => '<DEVICE-NAME>',
    	'visibility' => 'private'
    $device = $client->createDevice($data)
    //Or for a distribution
    $distribution = $client->distribution("<DISTRIBUTION-ID>");
  • Stream creation

  • Posting values

    $device = $client->device('<DEVICE-ID>');
    $data = array(
      array('value' => '1005', 'timestamp' => '2015-03-01T10:00:00Z'),
      array('value' => '2002', 'timestamp' => '2015-03-05T10:00:00Z')
  • Updating the device locations

    $device = $client->device('<DEVICE-ID>');
    $data = array(
      'name' => 'Storage Room A',
      'latitude' => '-37.9788423562422',
      'longitude', '-57.5478776916862'

Refer to the documentation on each class for further usage instructions.


In order to run this example, you will need a Device ID and API Key. If you don't have any, access your M2X account, create a new Device, and copy the Device ID and API Key values. The following script will send your CPU load average to three different streams named load_1m, load_5m and load_15. Check that there's no need to create a stream in order to write values into it.

In order to execute this script, run:

> API_KEY=<YOUR-API-KEY> DEVICE=<YOUR-DEVICE-ID> php examples/uptime.php
use Att\M2X\MQTT\MQTTClient;
use Att\M2X\MQTT\Error\M2XException;

$apiKey = getenv("API_KEY");
$deviceId  = getenv("DEVICE");

$m2x = new MQTTClient($apiKey);

# Get the device
$device = $m2x->device($deviceId);

# Create the streams if they don't exist yet

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 {
  } catch (M2XException $ex) {
    echo 'Error: ' . $ex->getMessage();
    echo $ex->response->raw;



This lib aims to adhere to Semantic Versioning 2.0.0. As a summary, given a version number MAJOR.MINOR.PATCH:

  1. MAJOR will increment when backwards-incompatible changes are introduced to the client.
  2. MINOR will increment when backwards-compatible functionality is added.
  3. PATCH will increment with backwards-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

Note: the client version does not necessarily reflect the version used in the AT&T M2X API.


This lib is provided under the MIT license. See LICENSE for applicable terms.