Skip to content

Instantly share code, notes, and snippets.

@cat-haines
Last active December 18, 2015 10:29
Show Gist options
  • Select an option

  • Save cat-haines/5769096 to your computer and use it in GitHub Desktop.

Select an option

Save cat-haines/5769096 to your computer and use it in GitHub Desktop.
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;
}
function set(value) {
led.write(value);
}
};
hardware.pin1.configure(DIGITAL_OUT);
ledController <- LedController("LED State", hardware.pin1);
imp.configure("HTTP In Example", [ledController], []);
http.onrequest(function(request, response) {
// do things
// send a response back..
response.send(200, "OK");
});
http.onrequest(function(request, response) {
// check if the ledStatus is in the query parameters (?ledStatus=VALUE)
if("ledStatus" in request.query)
{
// get the query parameter
local state = request.query["ledStatus"];
server.log(state); // log it
device.send("setLed", state); // pass it to the device
}
// send a response back..
response.send(200, "OK");
});
led <- hardware.pin1;
led.configure(DIGITAL_OUT);
imp.configure("Agent Example", [], []);
agent.on("setLed", function(data){
server.log("Setting LED to " + data);
led.write(data); // we're assuming data will be 1 or 0
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment