Andrew RoetkerJustin Holguin
Published © Apache-2.0

TMI

Platform for pebble and imp. Tells you bus time-to-arrival on your watchface, and flashes pretty leds when it's getting close.

IntermediateWork in progress1,601
TMI

Things used in this project

Story

Read more

Code

TMI-ELECTRIC-IMP-AGENT

JavaScript
local latestWeather = null;
local nextTime = -1;
// Define the response handler
 
function handleWeather(responseTable)
{
    // Expects the responseTable to be pre-validated.
    local data = http.jsondecode(responseTable.body);
    latestWeather = data.weather;
}

function handleTime(responseTable)
{
    // Expects the responseTable to be pre-validated.
    local data = http.jsondecode(responseTable.body);
    nextTime = data.arrivals[0];
}

function handleResponse(responseTable)
{
    // Called when the imp receives a response from the remote service
    
    if (responseTable.statuscode == 200)
    {
        // Remote service has responded with 'OK' so decode
        // the response's body 'responseTable.body' and headers 'responseTable.headers'
        // Code omitted for clarity 
        handleWeather(responseTable);
        handleTime(responseTable);
        server.log("latestWeather = " + latestWeather + ", nextTime = " + nextTime);
    }
    else
    {
        // Log an error
        server.log("Error response: " + responseTable.statuscode)
        device.send("updateColor", "orange");
    }
}

function update() {
    server.log("updating!")
    // Set up outgoing request object
    local request = http.get("http://hackthings-tmi.herokuapp.com/advice?stop=4016&bus=15", {});

    // Send the request asynchronously. This will not block the imp CPU
    request.sendasync(handleResponse);
}

function getColor() {
    if (nextTime <= 2) {
        return "red";
    } else if (nextTime <= 6) {
        return "yellow";
    } else if (nextTime <= 12) {
        return "green";
    } else {
        return "blue";
    }
}

function updateDevice(noop=null){
    device.send("updateColor", getColor());
}

function clockTick()
{
    // First, set a trigger to do the next tick in one minutes time
    imp.wakeup(60.0, clockTick);

    // if the next arrival is in negative time, refresh
    if (nextTime <= 0) {
        update();
    } else {
        // otherwise decrement the timer
        nextTime--;
    }
    updateDevice();
}

clockTick();
device.on("updateMe", updateDevice);

TMI-ELECTRIC-IMP-DEVICE

JavaScript
#require "ws2812.class.nut:1.0"

hardware.spi257.configure(MSB_FIRST, 7500);
pixels <- WS2812(hardware.spi257, 5);

function getColorCode(color){
    local blue = [0, 0, 255];
    local red = [255, 0, 0];
    local green = [0, 255, 0];
    local yellow = [255, 255, 0];
    local orange = [255, 165, 0];
    
    local newColor = blue;
    if (color == "red") {
        newColor = red;
    } else if (color == "yellow") {
        newColor = yellow;
    } else if (color == "green") {
        newColor = green;
    } else if (color == "orange") {
        newColor = orange;
    }
    return newColor;   
}

function setColor(colorName){
    local code = getColorCode(colorName);
    server.log("Setting color to " + colorName);
    for(local i = 0; i < 5; i++) {
        pixels.writePixel(i, code);
    }
    pixels.writeFrame();    
}

function startupColors(){
    setColor("red");
    imp.sleep(0.2);
    setColor("yellow");
    imp.sleep(0.2);
    setColor("green");
    imp.sleep(0.2);
    setColor("blue");
    imp.sleep(1.0);
}

startupColors();
agent.send("updateMe", null);
agent.on("updateColor", setColor)

TMI-PEBBLE

The Pebble watchface code.

TMI-API

The convenience endpoints API.

TMI-PEBBLE-CONFIG

Watchface configuration page for Pebble.

Credits

Andrew Roetker

Andrew Roetker

1 project • 0 followers
Justin Holguin

Justin Holguin

1 project • 0 followers

Comments