Skip to main content

SparkFun Phant IO

Latest Version: 1.0.0

The Phant class wraps the PhantIO/data.sparkfun.com API. It is a very simple data store to get up and running with.

You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.

To add this library to your project, add #require "Phant.class.nut:1.0.0" to the top of your agent code

Class Usage

Constructor: Phant(publicKey, privateKey[, baseURL])

The Phant constructor takes three parameters: your Phant public key and private key, and an optional URL. If no URL is specified, the class will use Sparkfun’s https://data.sparkfun.com as the base.

#require "Phant.class.nut:1.0.0"

const PUBLIC_KEY = "YOUR PUBLIC KEY";
const PRIVATE_KEY = "YOUR PRIVATE KEY";
stream <- Phant(PUBLIC_KEY, PRIVATE_KEY);

Current Limitations

We are currently recommending that you do not use the default URL in your constructor. Sparkfun is limiting traffic connecting via HTTPS more aggressively than traffic connecting via HTTP. If you are sending data to data.sparkfun.com, please set your constructor’s baseURL to "http://data.sparkfun.com/".

Class Methods

Optional Callbacks

All methods in the Phant class take an optional callback function parameter. The callback expects two parameters: error and data. If the request was successful, error will be null, and data will contain the response. If the request was unsucessful, error will contain the error information.

When the optional callback is passed, the requests will be made asynchronously. If a callback is not supplied, the request will be made synchronously, and a table containing err and data will be returned.

push(data[, callback])

The push() method pushes a new event into your Phant instance. The first parameter, data, must be a table containing the data to be pushed. For example:

Device Code

function poll() {
    imp.wakeup(15, poll);
    agent.send("temp", getTemp());
}

poll();

Agent Code

device.on("temp", function (tempData) {
    // Asyncronous Push
    stream.push({ temp = tempData }, function(error, data) {
        if (error != null) {
            // If it failed, log the reason
            server.error(http.jsonencode(err));
            return;
        }
    });
});

get([callback])

The get() method retreives the current contents of your Phant instance.

stream.get(function(error, data) {
    if (err != null) {
        server.error(http.jsonencode(error));
        return;
    }

    // Calculate average temperature
    local avgTemp = 0.0;
    foreach (datapoint in data) {
        avgTemp += datapoint.temp;
    }

    avgTemp = avgTemp / data.len();

    // Log average temperature
    server.log("Average Temperature: " + avgTemp);
});

clear([callback])

The clear() method clears all information stored in the Phant instance.

stream.clear(function(error, data) {
    if (error != null) {
        server.error(http.jsonencode(err));
        return;
    }
});

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release

License

The Phant library and example code are licensed under the MIT License.