Skip to content

Instantly share code, notes, and snippets.

@JoshuaJB JoshuaJB/app.js

Last active Aug 29, 2017
Embed
What would you like to do?
Basic Pebble App with WebSockets
/**
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*/
var UI = require('ui');
// WARNING: Does not work on emulator
var ws = new WebSocket('ws://152.2.31.240:9000/');
ws.onopen = function(evt){ console.log("connected!"); };
ws.onmessage = function(evt) { console.log("message: " + JSON.stringify(evt.data)); };
ws.onerror = function(evt) { console.log("error"); };
var main = new UI.Card({
title: 'RaspPI GPIO',
subtitle: 'Welcome!',
body: 'Press the middle button.'
});
main.show();
main.on('click', 'select', function(e) {
var menu = new UI.Menu({
sections: [{
items: [{
title: 'On',
subtitle: 'Turn on LED'
}, {
title: 'Off',
subtitle: 'Turn off LED'
}]
}]
});
menu.on('select', function(e) {
switch (e.itemIndex) {
case 0:
console.log("0");
ws.send(JSON.stringify({type: "broadcast", label: "ctrl", payload: 0}));
break;
case 1:
console.log("1");
ws.send(JSON.stringify({type: "broadcast", label: "ctrl", payload: 1}));
break;
default:
console.log('Unknown item selected: ' + e.itemIndex);
}
});
menu.show();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.