Created
June 24, 2020 11:41
-
-
Save ElectricImpSampleCode/58da5869fe9b2172d11c313032a04809 to your computer and use it in GitHub Desktop.
imp006 Breakout Board Getting Started #2 — Agent Code
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"); | |
// Define the function called when a request is sent to the agent | |
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 = anything or 0, set our state variable | |
// Convert the led query parameter to a Boolean | |
local ledState = request.query.led == "0" ? false : true; | |
// Send a "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) { | |
// Something went wrong -- tell the caller | |
response.send(500, "Internal Server Error: " + ex); | |
} | |
} | |
// Register the function that will handle HTTP requests from your browser | |
http.onrequest(requestHandler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment