Skip to main content

Dweetio

Latest Version: 1.0.1

Dweet.io describes itself as a “ridiculously simple messaging (and alerts)” system for the Internet of Things, and it really is very simple to use — especially with the library. It wraps the dweet.io API for public Things.

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 "Dweetio.class.nut:1.0.1" to the top of your agent code

Class Usage

Constructor: DweetIO([baseURL])

Call the constructor to instantiate a new Dweet.IO client. It has a single, optional parameter: baseURL, the URL to which the data will be sent. The default is https://dweet.io.

#require "dweetio.class.nut:1.0.1"

client <- DweetIO();

Class Methods

Callbacks

All of the library’s methods except stream() have an optional callback function parameter. If a callback function is supplied, the request will be made asynchronously, and the callback will be triggered once the request is fulfilled. The callback function must include a single parameter of its own into which will be passed a table containing three fields: statuscode, headers and body. If a callback is not supplied, the request will be made synchronously and the method will return the same table outlined above.

dweet(thing, data[, callback])

The dweet() method can be used to send a dweet.

// Asynchronous dweet
client.dweet("myThing", {"field1" : 1, "field2" : "test"}, function(response) {
    server.log(response.statuscode + ": " + response.body);
})
// Synchronous dweet
local response = client.dweet("myThing", {"field1" : 1, "field2" : "test"});
server.log(response.statuscode + ": " + response.body);

getLatest(thing[, callback])

The getLatest() method returns the most recent dweet from the specified thing:

client.get("myThing", function(response) {
    if (response.statuscode != 200) {
        server.log("Error getting dweet: " + response.statuscode + " - " + response.body);
        return;
    }

    local data = http.jsondecode(response.body)["with"][0];
});

getHistory(thing[, callback])

The getHistory() method will return all of the most recent 24 hours’ dweets to the specified thing (up to a maximum of 500 dweets).

client.getHistory("myThing", function(response) {
    if (response.statuscode != 200) {
        server.log("Error getting dweets: " + response.statuscode + " - " + response.body);
        return;
    }

    local data = http.jsondecode(response.body)["with"][0];
});

stream(thing, callback)

The stream() method opens a stream to the Dweet service and will execute the callback whenever new information is available for the specified thing. Unlike the library’s other methods, stream() method must be supplied with a callback and the stream callback is triggered with the thing’s data not a response table:

client.stream("myThing", function(thing) {
    if ("thing" in thing) {
        device.send("status", thing.content);
    }
});

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
1.0.1 GitHub Bug fixes

License

The dweet.io library is licensed under the MIT License.