Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
145 lines (96 sloc) 4.44 KB

AT&T's M2X Ruby (MQTT) Client

AT&T’s M2X is a cloud-based fully managed data storage service for network connected machine-to-machine (M2M) devices. From trucks and turbines to vending machines and freight containers, M2X enables the devices that power your business to connect and share valuable data.

This library aims to provide a simple wrapper to interact with the AT&T M2X API for Ruby using 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.


$ gem install m2x-mqtt


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

m2x =<YOUR-API-KEY>)

This provides an interface to your data in M2X

  • Distribution

    distribution = m2x.distribution("<DISTRIBUTION-ID>")
  • Device

    device = m2x.device("<DEVICE-ID>")
  • Stream

    stream ="<DEVICE-ID>", "<STREAM-NAME>")
    stream = m2x.device("<DEVICE-ID>").stream("<STREAM-NAME>")

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


For devices that do not have a Real Time Clock, M2X provides a set of endpoints that returns the server's time.

=> {"seconds"=>1435970368, "millis"=>1435970368451, "iso8601"=>"2015-07-04T00:39:28.451Z"}


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:

#! /usr/bin/env ruby

# for instructions

require "time"
require "m2x/mqtt"

API_KEY = ENV.fetch("API_KEY")

puts "M2X::MQTT/#{M2X::MQTT::VERSION} example"

@run = true

stop = { @run = false }

trap(:INT,  &stop)
trap(:TERM, &stop)

# Match `uptime` load averages output for both Linux and OSX
UPTIME_RE = /(\d+\.\d+),? (\d+\.\d+),? (\d+\.\d+)$/

def load_avg

m2x =

# Get the device
device = m2x.device(DEVICE)

# Create the streams if they don't exist"load_1m").update!"load_5m").update!"load_15m").update!

while @run
  load_1m, load_5m, load_15m = load_avg

  # Write the different values into AT&T M2X
  now =

  values = {
    load_1m:  [ { value: load_1m,  timestamp: now } ],
    load_5m:  [ { value: load_5m,  timestamp: now } ],
    load_15m: [ { value: load_15m, timestamp: now } ]

  res = device.post_updates(values: values)

  sleep 1


You can find this script in examples/m2x-uptime.rb.


This gem 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 gem is provided under the MIT license. See LICENSE for applicable terms.