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 include this library in your project, add
#require "Phant.class.nut:1.0.0"
at the top of your agent code
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);
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/"
.
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.
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:
function poll() {
imp.wakeup(15, poll);
agent.send("temp", getTemp());
}
poll();
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;
}
});
});
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);
});
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;
}
});
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 |
The Phant library and example code are licensed under the MIT License.