Skip to content

Instantly share code, notes, and snippets.

@harrisonhjones
Last active August 29, 2015 14:23
Show Gist options
  • Save harrisonhjones/1258e235362c110f9830 to your computer and use it in GitHub Desktop.
Save harrisonhjones/1258e235362c110f9830 to your computer and use it in GitHub Desktop.
Particle - Pebble App
// Project: Door Controller
// Description: A simple Pebble app for controlling my Particle-powered door lock
// Author: Harrison Jones ([email protected])
// Date: 6/7/2015
// -------- Includes -------- ///
var UI = require('ui');
var ajax = require('ajax');
//var Vector2 = require('vector2');
//var Accel = require('ui/accel');
var Vibe = require('ui/vibe');
//var Settings = require('settings');
// -------- Global Variables -------- ///
var particleAPIURL = 'https://api.particle.io/v1/';
var particleDeviceID = 'YOUR_DEVICE_ID_HERE';
var particleAPIKey = 'YOUR_ACCESS_TOKEN_HERE';
// -------- Program Start -------- ///
// Show splash screen while we check to determine if the front door controller is online
var cardSplashScreen = new UI.Card();
cardSplashScreen.title('Door Controller');
cardSplashScreen.subtitle('Loading...');
cardSplashScreen.body('');
cardSplashScreen.show();
// Load the API information
ajax(
{
url: particleAPIURL + 'devices/' + particleDeviceID + '/?access_token=' + particleAPIKey,
type:'json'
},
function(data) {
// Notify the user
Vibe.vibrate('short');
console.log("Got data: " + JSON.stringify(data));
// Check if the device is online
if(data.connected === true)
{
// Construct a Menu to show to the user
console.log("Device is online. Show the menu");
var menuControl = new UI.Menu({
title: 'Door Controller',
sections: [{
title: 'Actions',
items: [{
title: 'Lock',
subtitle: 'Lock the door'
},{
title: 'Unlock',
subtitle: 'Unlock the door'
}]
}]
});
// Add an action for the 'select' button (middle button)
menuControl.on('select', function(e) {
console.log('Item number ' + e.itemIndex + ' was pressed!!');
var arg = '';
if(e.itemIndex === 0)
{
arg = 'lock';
}
else
{
arg = 'unlock';
}
ajax(
{
url: particleAPIURL + 'devices/' + particleDeviceID + '/door',
method: 'post',
data: {access_token: particleAPIKey, args: arg}
},
function(data) {
console.log("Got data: " + data);
console.log("JSON - Got Data: " + JSON.stringify(data));
console.log("Return Value: " + data.return_value);
var jsonData = JSON.parse(data);
if(jsonData.return_value == 1)
{
// Success
Vibe.vibrate('short');
cardSplashScreen.subtitle('Success!');
cardSplashScreen.show();
}
else
{
// Failure (bad response from the device)
Vibe.vibrate('double');
cardSplashScreen.subtitle('');
cardSplashScreen.body('Bad return value: ' + jsonData.return_value);
cardSplashScreen.show();
}
},
function(error) {
// Failure (bad response from the API)
Vibe.vibrate('double');
console.log('Download failed: ' + JSON.stringify(error));
cardSplashScreen.subtitle('');
cardSplashScreen.body('Unable to communicate with the Particle API');
cardSplashScreen.show();
});
});
menuControl.show();
cardSplashScreen.hide();
}
else
{
// Failure (device is offline)
Vibe.vibrate('double');
console.log("Device is offline. Show an error message");
cardSplashScreen.subtitle('');
cardSplashScreen.body('Device is offline.');
}
},
function(error) {
// Failure (bad response from the API)
Vibe.vibrate('double');
console.log('Download failed: ' + JSON.stringify(error));
cardSplashScreen.subtitle('');
cardSplashScreen.body('Unable to communicate with the Particle API');
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment