Last active
August 29, 2015 14:05
-
-
Save erjjones/e11a46a36c42d5869d79 to your computer and use it in GitHub Desktop.
Home Monitor Garage Door Electric Imp Device
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ODATAHQ_URL = "https://query.odatahq.com/v3/{accountkeyhere}/homemonitor/garagedoor?api-key={apikeyhere}"; | |
// Print light reading trigger URL | |
server.log("Sensor Agent URL: " + http.agenturl()); | |
// Define funtions | |
function requestHandler(request, response) | |
{ | |
// Handle an incoming web request for a reading | |
try | |
{ | |
response.send(200, "OK"); | |
} | |
catch (ex) | |
{ | |
response.send(500, "Internal Server Error: " + ex); | |
} | |
} | |
// Register the HTTP handler | |
http.onrequest(requestHandler); | |
function processResponse(resp) { | |
server.log(resp); | |
} | |
// Make API Call | |
function postData(value) { | |
local key = value[0]; | |
local d = date(); | |
local datestring = format("%04d-%02d-%02dT%02d:%02d:%02d", d.year, d.month+1, d.day, d.hour, d.min, d.sec); | |
local body = http.jsonencode({ DocumentID = datestring, Status = key, StatusDateTime = datestring }); | |
local headers = { "Content-Type": "application/json" }; | |
local request = http.post(ODATAHQ_URL, headers, body); | |
request.sendasync(processResponse); | |
server.log(body); | |
server.log(datestring); | |
} | |
device.on("ping", function(value) { | |
postData(value); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***************************************************************** | |
Electric Imp Breakout (Device) | |
Post data to an ODataHQ OData service using | |
an Electric Imp https://www.sparkfun.com/products/11400 | |
Eric Jones @erjjones | |
Original Creation Date: August 27, 2014 | |
Description | |
Hardware Hookup: | |
* Pin 2 - Active-low momentary button (pulled high internally) | |
Distributed as-is; no warranty is given. | |
*****************************************************************/ | |
///////////////////////// | |
// Garage Door Monitor // | |
///////////////////////// | |
///////////////////// | |
// Pin Definitions // | |
///////////////////// | |
buttonPin <- hardware.pin2; | |
// This name will be sent to the stream each update: | |
local impName = "Imp%20" + imp.getmacaddress(); | |
//////////////////////////// | |
// Button Press Interrupt // | |
//////////////////////////// | |
// This function will be called each time the button (connected to pin 2) is | |
// pressed or released. | |
function sendData() | |
{ | |
// Only do this if the pin is low (pressed) | |
if (buttonPin.read() == 0) | |
{ | |
// garage door open | |
server.log("Garage Door Open"); | |
// talk to agent | |
agent.send("ping", ["Open"]); | |
} | |
if (buttonPin.read() == 1) | |
{ | |
// garage door closed | |
server.log("Garage Door Closed"); | |
// talk to agent | |
agent.send("ping", ["Closed"]); | |
} | |
} | |
/////////////// | |
// Pin Setup // | |
/////////////// | |
buttonPin.configure(DIGITAL_IN_PULLUP, sendData); // Button -- input w/ pull-up | |
server.log("Garage Door Monitor has started"); // Hello, monitoring. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment