Skip to content

Instantly share code, notes, and snippets.

View cat-haines's full-sized avatar

Cat Haines cat-haines

View GitHub Profile
@cat-haines
cat-haines / agent.nut
Created November 15, 2014 15:54
LED API
http.onrequest(function(req, resp) {
if(req.path=="/led/on") {
device.send("led", 1);
}
if(req.path=="/led/off") {
device.send("led", 0);
}
resp.send(200, "OK")
});
@cat-haines
cat-haines / ble112_tracker_ex.device.nut
Created November 5, 2014 21:13
BLE112 Tracker Example: Agent Code
// -----------------------------------------------------------------------------
class Firebase {
// General
db = null; // the name of your firebase
auth = null; // Auth key (if auth is enabled)
baseUrl = null; // Firebase base url
prefixUrl = ""; // Prefix added to all url paths (after the baseUrl and before the Path)
// For REST calls:
defaultHeaders = { "Content-Type": "application/json" };
@cat-haines
cat-haines / ble112_tracker_ex.device.nut
Created November 5, 2014 21:10
BLE112 Tracker Example: Device Code
// Copyright (c) 2014 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
/*
BGLib for Squirrel
==================
This implements the BGLib library for Bluegiga's BLE112 Bluetooth Smart module.
It assumes you have connected from the Imp to the BLE112:
@cat-haines
cat-haines / yo.agent.nut
Created September 6, 2014 23:36
Super simple Electric Imp + Yo integration.
http.onrequest(function(req, resp) {
if (req.query != null && "username" in req.query) server.log("Got a YO from " + req.query.username);
device.send("toggleLed", null);
resp.send(200, "OK");
});
class Stepper {
pinArray = null;
currentPosition = null; // 0
stepsPerFullRotation = null; // 200
pin = null; // id for pinArray
delayTime = null; // seconds
constructor(_firstPin, _secondPin, _thirdPin, _fourthPin, _stepsPerFullRotation, _delayTime)
{
pinArray = [_firstPin, _secondPin, _thirdPin, _fourthPin];
isHacking <- null;
message <- "";
device.on("hacking", function(data) {
isHacking = data.state;
message = format("%s (as of %i) - via an @electricimp agent", (data.state == 0) ? "NOT HACKING" : "HACKING", time())
if (!data.boot) {
twitter.tweet(message);
}
});
curl -X POST -d "type=drone&color=black&owner=bc7e4f6c-a0a7-4ab7-98b7-ec688fd36156" http://skynet.im/devices
{"type":"drone","color":"black","owner":"bc7e4f6c-a0a7-4ab7-98b7-ec688fd36156","ipAddress":"173.8.157.38","uuid":"58402a41-f25c-11e3-ba2e-0b078550b2fe","timestamp":"2014-06-12T18:06:58.276Z","token":"b56jgbmi8xyo80k97rj9fx4p274d9529","channel":"main","online":false}
verify that uuid exists by running:
curl -X GET http://skynet.im/devices/58402a41-f25c-11e3-ba2e-0b078550b2fe --header "skynet_auth_uuid: bc7e4f6c-a0a7-4ab7-98b7-ec688fd36156" --header "skynet_auth_token: b56jgbmi8xyo80k97rj9fx4p274d9529"
{"error":"unauthorized"}%
@cat-haines
cat-haines / create-device.agent.nut
Last active August 29, 2015 13:59
Code for the SkyNet blog post
data <- {
platform = "electric imp",
online = true,
token = "4e64e622-f456-44a6-af00-b996b6bea7a1",
type = "temp",
lat = 37.39674,
long = -122.10473
}
http.post(SKYNET_BASE + "devices", {}, http.urlencode(data)).sendasync(function(resp) {
@cat-haines
cat-haines / 1-SendTemp.device.nut
Last active January 2, 2016 20:49
Code for head First into AT&T M2X blog post.
/***** Include Hannah class here *****/
hannah <- Hannah();
function poll() {
agent.send("temp", { temp = hannah.temp.get() });
imp.onidle(function() { server.sleepfor(30.0); });
}
poll();
@cat-haines
cat-haines / stringify.nut
Last active December 30, 2015 01:59
Recursively traverses a Squirrel table (or object) and logs the results.
function stringify(t, i = 0) {
local indentString = "";
for(local x = 0; x < i; x++) indentString += ".";
if (typeof(t) != "table" && typeof(t) != "array") {
server.log(indentString + t)
} else {
foreach(k, v in t) {
if (typeof(v) == "table" || typeof(v) == "array") {
local par = "[]";