Skip to content

Instantly share code, notes, and snippets.

@LuisFDuarte
Created August 22, 2014 20:22
Show Gist options
  • Save LuisFDuarte/fdb973523a5879629754 to your computer and use it in GitHub Desktop.
Save LuisFDuarte/fdb973523a5879629754 to your computer and use it in GitHub Desktop.
device.on("temp", function(value) {
//server.log("Trying to post to Ubi the value:");
//server.log(value);
local headers = { "Content-Type": "application/json", "X-Auth-Token": "NBbF3PWPxWc2IaO40aXOKnhIu8tOv92rYN3ibiEc7Jh6GV3KZUUCHtXuNz7Y" }; // Replace the token with yours
local url = "http://things.ubidots.com/api/v1.6/variables/53d2beb37625424630223dac/values"; // Replace the Variable ID with yours
local string = {"value": value};
local request = http.post(url, headers, http.jsonencode(string));
local response = request.sendsync();
});
device.on("hum", function(value) {
//server.log("Trying to post to Ubi the value:hola");
//server.log(value);
local headers = { "Content-Type": "application/json", "X-Auth-Token": "NBbF3PWPxWc2IaO40aXOKnhIu8tOv92rYN3ibiEc7Jh6GV3KZUUCHtXuNz7Y" }; // Replace the token with yours
local url = "http://things.ubidots.com/api/v1.6/variables/53e541e57625422c7d900a1d/values"; // Replace the Variable ID with yours
local string = {"value": value};
local request = http.post(url, headers, http.jsonencode(string));
local response = request.sendsync();
});
function requestHandler(request, response) {
try {
// check if the user sent led as a query parameter
if ("relay" in request.query) {
server.log("entre");
// if they did, and led=1.. set our variable to 1
if (request.query.relay == "1" || request.query.relay == "0") {
// convert the led query parameter to an integer
local relayState = request.query.relay.tointeger();
// send "led" message to device, and send ledState as the data
device.send("relay", relayState);
}
}
// send a response back saying everything was OK.
response.send(200, "OK");
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}
// register the HTTP handler
http.onrequest(requestHandler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment