I’ve been working on a room temperature logger for a while and now I have finally managed to get it working.
It started updating itself when I fixed the errors and built the code for the first time for about 10 minutes. After then it stopped working and it will only update whenever I build the code again. I’m not sure why it will not update every 10 seconds.
I’m not sure if its Xively or Agent that is having an issue. (I can see the temperature being updated in my log without any problems)
Here is the code:
`API_Key <- “"; //Type your Xively API Key
Feed_ID <- "” //Type your Feed ID
Channel_ID <- “Thermometer”; //Type your Channel ID
device.on(“temp”, function(temp) {
// create the channel
local tempChannel = Xively.Channel(Channel_ID);
// set the channel
tempChannel.Set(temp);
// create the feed:
local tempFeed = Xively.Feed(Feed_ID, [tempChannel]);
// create the client
local client = Xively.Client(API_Key)
// push the data
client.Put(tempFeed);
});
Xively <- {}; // this makes a 'namespace’
class Xively.Client {
ApiKey = null;
triggers = [];
constructor(apiKey) {
this.ApiKey = apiKey;
}
/*****************************************
* method: PUT
* IN:
* feed: a XivelyFeed we are pushing to
* ApiKey: Your Xively API Key
* OUT:
* HttpResponse object from Xively
* 200 and no body is success
*****************************************/
function Put(feed){
local url = "https://api.xively.com/v2/feeds/" + feed.FeedID + ".json";
local headers = { "X-ApiKey" : ApiKey, "Content-Type":"application/json", "User-Agent" : "Xively-Imp-Lib/1.0" };
local request = http.put(url, headers, feed.ToJson());
return request.sendsync();
}
/*****************************************
* method: GET
* IN:
* feed: a XivelyFeed we fulling from
* ApiKey: Your Xively API Key
* OUT:
* An updated XivelyFeed object on success
* null on failure
*****************************************/
function Get(feed){
local url = "https://api.xively.com/v2/feeds/" + feed.FeedID + ".json";
local headers = { "X-ApiKey" : ApiKey, "User-Agent" : "xively-Imp-Lib/1.0" };
local request = http.get(url, headers);
local response = request.sendsync();
if(response.statuscode != 200) {
server.log("error sending message: " + response.body);
return null;
}
local channel = http.jsondecode(response.body);
for (local i = 0; i < channel.datastreams.len(); i++)
{
for (local j = 0; j < feed.Channels.len(); j++)
{
if (channel.datastreams[i].id == feed.Channels[j].id)
{
feed.Channels[j].current_value = channel.datastreams[i].current_value;
break;
}
}
}
return feed;
}
}
class Xively.Feed{
FeedID = null;
Channels = null;
constructor(feedID, channels)
{
this.FeedID = feedID;
this.Channels = channels;
}
function GetFeedID() { return FeedID; }
function ToJson()
{
local json = "{ \"datastreams\": [";
for (local i = 0; i < this.Channels.len(); i++)
{
json += this.Channels[i].ToJson();
if (i < this.Channels.len() - 1) json += ",";
}
json += "] }";
return json;
}
}
class Xively.Channel {
id = null;
current_value = null;
constructor(_id)
{
this.id = _id;
}
function Set(value) {
this.current_value = value;
}
function Get() {
return this.current_value;
}
function ToJson() {
local json = http.jsonencode({id = this.id, current_value = this.current_value });
server.log(json);
return json;
}
}
TempChannel <- Xively.Channel(Channel_ID)
TempFeed <- Xively.Feed(Feed_ID, [TempChannel]);
client <- Xively.Client(API_Key);
device.on(“temp”, function(temp) {
TempChannel.Set(temp)
client.Put(TempFeed)
});`