Skip to content

Instantly share code, notes, and snippets.

View cat-haines's full-sized avatar

Cat Haines cat-haines

View GitHub Profile
html <- "<html><head><title>Hello World</title></head><body><h1>Hello World</h1><div>I'm being server by an angert!! Hurray</div></body></html>";
http.onrequest(function(req, res) {
res.send(200, html);
});
@cat-haines
cat-haines / agent.nut
Last active December 20, 2015 23:58
Here's some code I wrote in my mail editor to demonstrate how you can use ping-pong messages to only respond to an HTTP request after receiving confirmation that your imp got the message. It also uses a watched function to return timeouts if a specific timeout is exceeded.
responseQueue <- [];
timeout <- 5; // timeout (in seconds) for roundtrip from agent -> device -> agent.
// function that triggers responses if timeout expires
function checkResponses() {
local newQueue = [];
// loop through http response queue
local t = time();
for (local i = 0; i < responseQueue.len(); i++) {
// if it's been sitting for more than allowed timeout
@cat-haines
cat-haines / agent.button.js
Last active December 20, 2015 19:59
This GIST has four files and demonstrates how data can be passed from one imp to another using HTTP requests, and HTTP request handlers.
const otherAgentUrl = "http://agent.electricimp.com/YourOtherAgentID";
// create a handler for the "button" message from the device
device.on("button", function(state) {
local url = otherAgentUrl + "?led=" + state;
server.log("sending request to " + url);
// create the request
local request = http.get(url);
// send the request
request.sendsync();
@cat-haines
cat-haines / 1HttpIn.js
Last active December 18, 2015 10:29
This is a collection of code snipper for the blog post at: beardedinventor.com/2013/06/12/Migrating-from-HTTP-Nodes-to-Agents/ These files are actually .nut files (Squirrel) - but we store them as .js for formatting and syntax highlighting.
// Create a class to control an LED from an input port
class LedController extends InputPort {
led = null;
constructor(name, _led) {
base(name);
this.led = _led;
}
@cat-haines
cat-haines / pulse.js
Last active December 16, 2015 20:19
A simple Squirrel class for handeling pulses as a type of digital input and a simple example using it. NOTE: Accuracy has NOT been tested.
/*******************************************************************************
* Reads a pulse (either HIGH or LOW) on a pin. If the pulseValue is HIGH,
* the object will start timing when the pin goes HIGH, then waits for the pin
* to go LOW, and executes the callback (callback expects a function with one
* parameter, the length of the pulse in microseconds).
*
* The accuracy of this function has not been tested, and will have some
* variance because of how callbacks work.
******************************************************************************/
class Pulse {
@cat-haines
cat-haines / agent.nut
Created April 20, 2013 00:02
Electric Imp example API
local greenState = 0;
local redState = 0;
device.on("setRed", function(data) {
redState = data;
});
device.on("setGreen", function(data) {
greenState = data;
});
@cat-haines
cat-haines / HttpRequestResponse.nut
Created April 19, 2013 21:10
Electric Imp example code that processes an incoming HTTP Request (through an HttpIn node hooked up to an InputPort) and sends a "response" (which is actually just an HTTP Post to a predefined location).
// This is the function that actually handles the incoming HTTP Request
function SimpleRequestHandler(data) {
server.log("Got a request with value='" + data + "'");
return 200;
}
// handles incoming HttpIn requests, and sends "responses" to an HttpRequest node through
// an output port. The ResponsePort sends a url-encoded message where "value" is the response code.
class HttpInPortWithResponse extends InputPort {
RequestHandler = null; // function that will be called whenever a request comes into the input port. Should return a status code.
@cat-haines
cat-haines / gist:5014004
Created February 22, 2013 14:57
Electric Imp code for polling an analog input every half second.
imp.configure("Analog In", [], []);
function readPots()
{
local p = hardware.pin5.read();
server.show(p);
imp.wakeup(0.5, readPots);
}
hardware.pin5.configure(ANALOG_IN);
@cat-haines
cat-haines / RGB Imp
Last active December 14, 2015 02:29
Electric Imp code to go with Electric_Imp_Example repository. Passes RGB json object to Electric_Imp_Example API.
local redPot = hardware.pin5;
local greenPot = hardware.pin7;
local bluePot = hardware.pin8;
local red = 0;
local green = 0;
local blue = 0;
local redPort = OutputPort("red");
local greenPort = OutputPort("green");
@cat-haines
cat-haines / SimpleInputPort
Last active December 14, 2015 00:49
ElectricImp code for setting an LED based on value from an InputPort
class simpleInputPort extends InputPort
{
function set(value)
{
if (value == 1)
{
server.show("on");
hardware.pin2.write(0);
}
else if (value == 0)