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
// Basic wrapper to create an execute an HTTP GET | |
function HttpGetWrapper (url, headers) { | |
local request = http.get(url, headers); | |
local response = request.sendsync(); | |
return response; | |
} |
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
// Basic wrapper to create an execute an HTTP DELETE | |
function HttpDeleteWrapper (url, headers) { | |
local request = http.httpdelete(url, headers); | |
local response = request.sendsync(); | |
return response; | |
} |
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
// Basic wrapper to create an execute an HTTP PUT | |
function HttpPutWrapper (url, headers, body) { | |
local request = http.put(url, headers, body); | |
local response = request.sendsync(); | |
return response; | |
} |
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
// Basic wrapper to create an execute an HTTP POST | |
function HttpPostWrapper (url, headers, string) { | |
local request = http.post(url, headers, string); | |
local response = request.sendsync(); | |
return response; | |
} |
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
device.on("senddata", function(data) { | |
// Set URL to your web service | |
local url = "https://www.mywebservice/object"; | |
// Set Content-Type header to json | |
local headers = { "Content-Type": "application/json" }; | |
// encode data and log | |
local body = http.jsonencode(data); | |
server.log(body); |
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
// create a request handler | |
http.onrequest(function(request, response){ | |
try | |
{ | |
// decode the json - if it's invalid json an error will be thrown | |
local data = http.jsondecode(request.body); | |
// check if particular keys are in the json body | |
if ("pin1" in data) { | |
// send message to device |
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
server.log(http.urlencode({ foo="bar" })); | |
server.log(http.urlencode({ foo="wot?" })); | |
server.log(http.urlencode({ bar="bar", wurdle=[3,4] })); | |
server.log(http.urlencode(1234)); // this will throw an error |
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
hardware.pin1.configure(ANALOG_OUT); | |
hardware.pin2.configure(DIGITAL_OUT); | |
// When we get a pin1 message | |
agent.on("pin1", function(data) { | |
// Write value to pin1 | |
hardware.pin1.write(data); | |
} | |
// When we get a pin2 message |
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 to turn LED on/off when agent starts | |
server.log("Turn the LED on by browsing to " + http.agenturl() + "?led=1"); | |
server.log("Turn the LED off by browsing to " + http.agenturl() + "?led=0"); | |
// HTTP Request handlers expect two parameters: | |
// request: the incoming request | |
// response: the response we send back to whoever made the request | |
function requestHandler(request, response) { | |
// Check if the variable led was passed into the query | |
if ("led" in request.query) { |
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
imp.configure("Hello World", [], []); |