Last active
December 17, 2015 06:59
-
-
Save xively-gists/5569436 to your computer and use it in GitHub Desktop.
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
const FEED_ID = "FEED_ID_HERE"; | |
const API_KEY = "API_KEY_HERE"; | |
function get_xively() { | |
//wakeup | |
imp.wakeup(15.0, get_xively); //schedule when to wakeup again | |
local xively_url = "https://api.xively.com/v2/feeds/" + FEED_ID + ".json"; //format xively url | |
server.log(xively_url); | |
local getreq = http.get(xively_url, {"X-ApiKey":API_KEY, "User-Agent":"Xively-Imp-Lib/1.0"}); //add headers | |
local res = getreq.sendsync(); //actuallty send request | |
if(res.statuscode != 200) { | |
server.log("error sending message: " + res.body); | |
} | |
server.log("#####RESPONSE#####"); | |
server.log(res.statuscode + " OK"); //status | |
//server.log(res.body); //body | |
//format data into table - uncomment to use | |
local resTable = http.jsondecode(res.body); | |
server.log("table:"); | |
server.log(resTable.title); | |
server.log(resTable.id); | |
//print ds0 | |
server.log(resTable.datastreams[0].id); | |
server.log(resTable.datastreams[0].current_value + " at " + resTable.datastreams[0].at); | |
//send to device | |
device.send("value", resTable.datastreams[0].id + "," + resTable.datastreams[0].current_value); //send datastream 0 info | |
//device.send("json", res.body); //send json | |
} | |
function send_xively(body) { //take in csv value | |
local xively_url = "https://api.xively.com/v2/feeds/" + FEED_ID + ".csv"; //setup url for csv | |
server.log(xively_url); | |
server.log(body); //pring body for testing | |
local req = http.put(xively_url, {"X-ApiKey":API_KEY, "Content-Type":"text/csv", "User-Agent":"Xively-Imp-Lib/1.0"}, body); //add headers | |
local res = req.sendsync(); //send request | |
if(res.statuscode != 200) { | |
server.log("error sending message: "+res.body); | |
}else device.send("status", (res.statuscode + " OK")); //sends status to uart. this can be removed if not desired | |
} | |
get_xively(); //initialize first get request. will continue to call itself. | |
device.on("data", function(feedCSV) { //take csv body in from device | |
server.log("device on"); | |
//send preformatted multi-ds csv | |
send_xively(feedCSV); //send to function to call xively | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment