[Solved] Xively will stop updating or Agent will stop sending data

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)
});`

You should probably print the return code from your client.Put():

local ret = client.Put(TempFeed); server.log("Put returned code "+ret.statuscode);

…I suspect you can’t post to Xively that fast - as in, xively don’t let you.

The temperature will only update once when I build the code. (I’ve tried 3 more times with whole new project and it still shows the same result. I even made it to update every 30 seconds…)

Is there any other way to graph my room temperature data and access it via the internet?

start with an interval of 1 minute, and be patient, because you might be “punished” for exeeding their rate limit

I’ve tried 60 seconds and 300 seconds… Still the same…

Looks like I am going to have to look for another way of logging temperature. Any suggestions?

Before you give up it might pay to look at what your sending xively.
You can use requestb.in to inspect your put.
replace local url = "https://api.xively.com/v2/feeds/ with http://requestb.in/xxx where xxx is your request.in then refresh http://requestb.in/xxx?inspect and it should show what your putting.

@Tom just updated his TempBug instructables - it’s a guide to logging temperatures from an Electric Imp card to Xively with agents…

http://www.instructables.com/id/TempBug-internet-connected-thermometer/

Also just noticed that you have two device.on(“temp”) handlers. The second one will take priority. I presume you’re sending a reading from the device every n seconds?

If you post your device code, we can see what is going on there.

@jwehr Device code as you requested.

Device code:
`hardware.pin1.configure(ANALOG_IN);

// LED pin config
hardware.pin2.configure(DIGITAL_OUT_OD_PULLUP);
// initially set to off
hardware.pin2.write(0);

/*
If you don’t have a Hannah board but want to try the
Cosm.com temperature logging exercise, here it is!

temperature sensor used is tmp36 from adafruit.com for $2.00
http://learn.adafruit.com/tmp36-temperature-sensor
temp range -40°C to 150°C / -40°F to 302°F
*/

local postInit = false;
local oldTemp = null;
local reading = null;
local ratio = null;
local voltage = null;
local temperatureC = null;

local temp = null;

function update_temp() {
// keep the imp awake
imp.wakeup(300, update_temp);

// get the raw voltage value from temp sensor btw 0-65535
// in this case that needs mapping to the range 0-3.3v
reading = hardware.pin1.read();

// get the ratio
ratio = 65535.0 / reading;

// make units milivolts and get voltage we can work with
//voltage = (hardware.voltage() * 1000) / divider;
voltage = 3300 / ratio;

// get temperature in degrees Celsius
temperatureC = (voltage - 500) / 10.0;

server.log(postInit);

server.log("temp: " + temperatureC + “°C”);

// set our output to desired temperature unit
temp = temperatureC;

// after function’s first run set flag to true
postInit = true;

}

update_temp();
agent.send(“temp”, temp);`

Move agent.send(“temp”, temp); up to the bottom of the update_temp() function. It is only being called once when you run the code, even thought update_temp() is being called over and over.

It works now. Thank you very much!