Code

file_3648.txtJavaScript
See the entire code base here: http://jsfiddle.net/brockleej/zbfjp4bc/
var api = pinoccioAPI();

//Usage:
//1. Put your main api.token in the approprate spot.
//2. Put the appropriate troopId, scoutId, pinType 'digital' or 'analog' an pinNum
//3. put the name of the command you wish your troop to run when the button is pushed.
// to make a toggle command in HQ: function toggle {d2=!d2;} //toggles pin d2 the pin. 
api.token = "YourTokenHere";
var troopId = 2;
var scoutId = 1;
var pinType = 'digital';
var pinNum = 0;// d2=0, d3=1, d4=2 etc.
var command = "toggle";

// when a button is clicked run the funtcion tog() 
$("button").click(function () {
    tog();
})

 
// change the pin state in HQ and check to see if it is updated on this page! 
// when the button is clicked the scout is given instructions to run the "toggle" command.
function tog() {

    //toggle command on scout--- function toggle {d2=!d2;} //toggles pin "d2"
    api.rest({
        url: "/v1/" + troopId + "/" + scoutId + "/command",
        data: {
            command: command
        }
    }, function (error, result) {
        // this is where you find out of the command was successful if(!error)
        // and read the result. for commands that print results.
        if (error) return $("#out").text('ERROR: ' + (error.message || error + ''));
        // setting the led does not print a result.
        $("#out").html("success!");
    })

    $("#out").text("sending command " + command + " to troop " + troopId + " and scout " + scoutId);
}



// make a sync stream so we know the real state of pin.
api.sync({stale: 1}).on('data', function (data) {
    // is this is an event for the troop and scout im interested in?
    // the sync returns a lot of data... you could choose nearly any variable to look at.
    if (data.type == pinType && data.troop == troopId && data.scout == scoutId) {
        // read the pin values for the particular scout. 
        var pins = data.value.state;
        var status;
        //The following checks the status on a pin 
        //in the "pinType"  data.type / data.value.state. d2=0, d3=1, d4=2, etc
        //then runs logical test and changes status based on pin state.
        if (pins[pinNum] == 1) {
            //change the status message
            status = "AWESOME";
            //or change the status image
            document.getElementById("sign").src = "http://brockclan.com/on.jpg";
            } else {
                status = "BUMMER";
                document.getElementById("sign").src = "http://brockclan.com/off.jpg";
            }
            // set text based on "stale" events are stale when they have occured before the last online time.
            // its the last recorded value but it may not be the current state.
            var text = status + "!";
            if (data.stale) text = "last known" + status + "!";
            $("#coffee").text(text);
            $(signPic).slideToggle( "slow", function(){
 
  });


        }
    }).on('error', function (error) {
    $("#error").text('sync error. ' + (error.message || error + ''));
})

Credits

Lee Brock

Are you Lee Brock? Claim this project and add it to your profile.

This is mine
Posted by Tmp image 0?auto=compress%2cformat&w=40&h=40&fit=min Alex Glow

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

DHT22 Sensor and Pinoccio
  • 1,623
  • 5

Full instructions

Using DHT22 Temperature and Humidity sensor with Pinoccio

Mesh Your Brain
  • 3,549
  • 22

Full instructions

Build your own Internet of Brains with this wireless EEG hack!

Human Presence Detection & Visualization
  • 1,070
  • 9

Full instructions

Team Cogitate's social radar displays people nearby as a set of glowing orbs

Hideous Arc Reactor Sweater (WIP)
  • 845
  • 7

Work in progress

Power through the holiday slump with this festive, EEG-reactive sweater!

Pinoccio Craft
  • 378
  • 5

Full instructions

Send Bitlash Pinoccio Commands through Minecraft.

Solar Powered Pinoccio Scout
  • 1,104
  • 9

Run your wireless mesh forever with Adafruit's solar LiPo charger

Add projectSign up / Login