Last active
July 1, 2017 00:12
-
-
Save SwitchScienceCode/35520c2db15e52170fd7f9546e5e8cbf to your computer and use it in GitHub Desktop.
Electric Imp Explorer Kit Sample Code: Agents
This file contains 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
// Log the URLs we need | |
server.log("Turn LED On: " + http.agenturl() + "?led=1"); | |
server.log("Turn LED Off: " + http.agenturl() + "?led=0"); | |
function requestHandler(request, response) { | |
try { | |
// Check if the user sent led as a query parameter | |
if ("led" in request.query) { | |
// If they did, and led = 1 or 0, set our variable to 1 | |
if (request.query.led == "1" || request.query.led == "0") { | |
// Convert the led query parameter to a Boolean | |
local ledState = (request.query.led == "0") ? false : true; | |
// Send "set.led" message to device, and send ledState as the data | |
device.send("set.led", ledState); | |
} | |
} | |
// Send a response back to the browser saying everything was OK. | |
response.send(200, "OK"); | |
} catch (ex) { | |
response.send(500, "Internal Server Error: " + ex); | |
} | |
} | |
// Register the HTTP handler to begin watching for HTTP requests from your browser | |
http.onrequest(requestHandler); |
This file contains 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
// Import Electric Imp’s WS2812 library | |
#require "WS2812.class.nut:3.0.0" | |
// Set up global variables | |
spi <- null; | |
led <- null; | |
// Define the loop flash function | |
function setLedState(state) { | |
local color = state ? [255,0,0] : [0,0,0]; | |
led.set(0, color).draw(); | |
} | |
// Set up the SPI bus the RGB LED connects to | |
spi = hardware.spiAHSR; | |
spi.configure(MSB_FIRST, 6000); | |
// Set up the RGB LED | |
led = WS2812(spi, 1); | |
hardware.pinH.configure(DIGITAL_OUT, 1); | |
// Register a handler function for incoming "set.led" messages from the agent | |
agent.on("set.led", setLedState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment