Skip to content

labitat/blipserver

Repository files navigation

blipserver

About

This is the code running on space.labitat.dk.

It reads data from the Arduino connected to the serial port, which monitors the power meter in Labitat, and serves the power graph and an API for retrieving past power meter data.

Each time the Arduino detects a new blink of the power meter it sends the amount of milliseconds passed since last blink as a decimal string followed by a newline ("\n").

Upon receiving such a value from the Arduino the server attaches a timestamp (unix timestamp in milliseconds) and stores this pair in a database. It also returns the point to any clients doing long-polling to update the live graph.

The server is written in Lua using the Lua Event Machine, along with the stream and PostgreSQL libraries for it.

API

The database stores pairs (stamp, ms) for each blink of the power meter. Here stamp is a unix timestamp in milliseconds describing roughly when the blink happened, and ms is the number of milliseconds which passed since the last blink detected. We'll refer to such a pair as a "point".

The power meter blinks once for each Wh of power used (1000 times for each kWh). Use the formula

3600000 / ms

to calculate the (mean) power usage in Watts during the time interval [stamp - ms, stamp].

The points can be fetched by doing HTTP requests to various URIs. So far JSON is the only output format supported and points will be returned in a JSON array [stamp, ms].

Clients should not assume that stamp1 + ms2 = stamp2 for every two consecutive points (stamp1, ms1) and (stamp2, ms2). There may be time drifts, rounding errors or both. Also the blip server may have been down for some period of time due to maintanence or other hacking and thus not been able to log some blinks.

  • /blip

    Use this URI to do long-polling. The server will not answer the request immediately, but instead wait until the next blink is detected and then return that point.

  • /last

    Immediately returns the last point read.

  • /last/<n>

    Returns a list points read during the last <n> milliseconds.

    If there are more than 2000 such points only the first 2000 will be returned.

  • /since/<n>

    Returns a list of points since <n>, which must be a unix timestamp in milliseconds.

    If there are more than 2000 such points only the first 2000 will be returned, so use 1 plus the timestamp of the last point in the list to request the next 2000 points (again using this URI).

  • /aggregate/<n>/<m>/<c>

    Returns aggreated usage over a time period. The period starts at <n>, which must be a unix timestamp in milliseconds.

    The aggregation interval is <m>, in milliseconds. Eg. 3600000 to get usage per hour. The number of points returned is <c>.

  • /hourly/<m>/<n>

    Returns usage per hour. The usage is returned in the interval <m> to <n>, in unix millisecond timestamps. 5 values are returned for each hour-interval: ** The unix millisecond timestamp of the start of the hour-interval ** The number of blips received during that hour-interval ** The average usage during that hour, in watts. ** The minimum blip length in that interval, in milliseconds. ** The maximum blip length in that interval, in milliseconds.

  • /labibus_blip/<dev>

    Long-polling of Labibus. The server waits for the next data point on Labibus device <dev> to be received, then returns it as the pair [stamp,value].

  • /labibus_last/<dev>/<n>

    Returns the Labibus data for device <dev> within the last <n> milliseconds (At most 20000 points will be returned though).

  • /labibus_since/<dev>/<stamp>

    Returns the Labibus data for the device <dev> since the given unix millisecond time stamp. At most 20000 points will be returned.

License

blipserver is free software. It is distributed under the terms of the GNU General Public License.

About

The server running on space.labitat.dk to log power usage and show a nice graph.

Resources

License

Stars

Watchers

Forks

Packages

No packages published