-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorDetect.agent.nut
30 lines (25 loc) · 1023 Bytes
/
ColorDetect.agent.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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 {
local ledState = 0;
// check if the user sent led as a query parameter
if ("led" in request.query) {
// if they did, and led=1.. set our variable to 1
if (request.query.led == "1" || request.query.led == "0") {
// convert the led query parameter to an integer
ledState = request.query.led.tointeger();
// send "led" message to device, and send ledState as the data
device.send("led", ledState);
}
}
// send a response back saying everything was OK.
if (ledState == 1) response.send(200, "I live!");
else if (ledState == 0) response.send(200, "I'm afraid I can't do that, or can I?");
} catch (ex) {
response.send(500, "Darnit, a server error occured: " + ex);
}
}
// register the HTTP handler
http.onrequest(requestHandler);