Things used in this project

Code

TMI-ELECTRIC-IMP-AGENTJavaScript
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-DEVICEJavaScript
#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

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Life Saver
Intermediate
  • 3,877
  • 72

Work in progress

Detect road accident, raise SOS, share location, share & monitor vital health stats during accident using Pebble. #SaveLife

Life Saver

Team Enigma

Pebble controlled DIY SmartGarage with the Sparkcore
Intermediate
  • 7,408
  • 97

Full instructions

Build it yourself and control your garage door with the Pebble Smartwatch and the Sparkcore

Smartwatch car remote
Intermediate
  • 11,661
  • 73

Full instructions

Controlling a car using a Pebble watch, an Android phone and an Arduino

Particle Sumo Bot
Intermediate
  • 3,437
  • 59

Full instructions

3D Printed Particle.io powered Sumo Bot with Pololu motor controller

RunTracker One
Intermediate
  • 963
  • 17

Work in progress

Go running with only one device - your smartwatch - and still enjoy all your music, GPS tracking, and more!

RunTracker One

Team Wesley Crushers

PebbleCannon
Intermediate
  • 972
  • 17

Vanquish your foes, control a nerf gun with your pebble watch.

Add projectSign up / Login