Xively agent code - NOVICE - can Adafruit tutorial agent code be adapted to send 2 channels?

I’m a complete novice and have followed @jwehr excellent Adafruit tutorial, based on the work of @beardedinventor & @mjkuwp94 excellent work that allows temp readings to be sent along to both Xively and Twitter.


I’ve got the imp reporting the temperature of 2 x i2c thermometers and they return their temps in 2 x variables:


at the end of the DEVICE code, I can send 1 of these variables to the Xively AGENT code by:

agent.send("Xively", celcius_12);

The Xively AGENT code sends this as Channel 1 but I’d like to see if anyone has adapted that AGENT code to send 2 Channels and if so would they be willing to share it with me. I imagine there’s some special way of sending both variables to the AGENT code as well.

Thanks in advance,

I don’t know which Xively class you use, but the ones I know are quite similar and they create the channels automaticly.

So you could try (for a start):
agent.send("celcius_12", celcius_12); agent.send("celcius_89", celcius_89);

Thanks @DolfTraanberg,
I’m going to have to apologise upfront, I’m really dumb with this stuff.

If I understand you correctly, your suggested DEVICE code amendments look to me to be sensible in so much as I have 2 x agents (called celcius_12 and another called celcius_89) is that correct?

I don’t seem to be able to attach the AGENT code that I’ve borrowed from @jwehr Adafruit tutorial which I understand was largely written by @beardedinventor & @mjkuwp94 as the “ATTACH A FILE” routine won’t let me attach either a RTF of ODT text file (I can’t find any other simple format available on my iMac Text Editor).

So I guess that’s a bummer!

The best I can come up with is a link to the Adafruit tutorial ZIP file for the code:

I’ve tried a few ways to modify it to accept 2 channels but can’t seem to make it work owing to my complete lack of understanding.

Thanks again,

I send multiple temperatures to Xively like this:

Make a table in the device code and send it to the agent:
local probetemps = {"probe1temp" : probe1temp , "probe1reftemp" : probe1reftemp, "probe2temp" : probe2temp, "probe2reftemp" : probe2reftemp}; agent.send("Xively", probetemps)

And then to Xively from the agent:
probe1 <- 0; probe2 <- 0; device.on("Xively", function(v) { channel1 <- Xively.Channel("Grill_Temperature"); channel1.Set(v.probe1temp); probe1 = v.probe1temp; channel2 <- Xively.Channel("Food_Probe_Temperature"); channel2.Set(v.probe2temp); probe2 = v.probe2temp; channel3 <- Xively.Channel("Grill_Temp_Reference"); channel3.Set(v.probe1reftemp); channel4 <- Xively.Channel("Food_Probe_Temp_Reference") channel4.Set(v.probe2reftemp); feed <- Xively.Feed("555555555555", [channel1, channel2, channel3, channel4]); client.Put(feed); });

Does that make sense? You make a table at the device and put your values in it, and then pass the table up to the agent. At the agent, when you want to get the values, you pull them back out of the table and pass them to Xively.

I have not helped with the tutorials so I am bit humbled and embarrassed that I have been getting mentioned.

I have on my long-term todo list to do something similar (multiple data series to Xively or TempDB ) so I will be able to help in the long run but in the short run I don’t have the time , sorry.

In general:

  • there is one Agent with each one Device.
  • I suggest JSON language to transfer the data. Here is a link to Xively which appears to show the format required.

Write a Single Datapoint to Each Datastream

JSON can be created and decoded from Arrays and Tables in squirrel using http.jsonencode and http.jsondecode.

this tool may help

json editor online

I suggest practicing using the Agent code only to encode and decode to get practice and print out the JSON in the log.

Thanks @jwehr,
if I understand you correctly; based on my variable in the DEVICE code, I should be able to make the following table:

local thermotemps = {"thermo1temp" : celcius_89, "thermo2temp" : celcius_12};

and if I understand that correctly, “thermo1temp” will hold the value of my variable “celcius_89” (which is the temperature read on i2c89)

and by natural consequence, “thermo2temp” will hold the value of my variable “celcius_12” (which is the temperature read on i2c12)

then I send the table to the Xively AGENT code:

agent.send("Xively", thermotemps);

Is that correct?

And if that’s correct, then the relevant AGENT code amendments would be:

thermo1 <- 0;
thermo2 <- 0;

device.on(“Xively”, function(v) {
channel1 <- Xively.Channel(“IR_Temp_Sensor_i2c89”);
thermo1 = v.thermo1temp;
channel2 <- Xively.Channel(“IR_Temp_Sensor_i2c12”);
thermo2 = v.thermo2temp;

feed <- Xively.Feed(Feed_ID, [channel1, channel2,]);

Does that sound right to you?
Or have I missed the point?

Thank you ( and everyone else) for helping me.

Yep, you got it. You could make thermo1 and thermo2 local variables if you aren’t using them in any other functions… but that is a minor point. I use them in some other functions, which is why they are globals in my code… but both will work.

Thanks @mjkuwp94
I really appreciate the time & effort you’ve taken to point me in the right direction.

I’m going to take the time to study the advice you’ve given me here to get a better understanding of what I’m trying to achieve in the end.

Thanks heaps,

Hi again @jwehr

I’ve hit the “Build and Run” command button & it’s worked as expected!!!

That is AWESOME; I’m so excited.

I do have one other question regarding this kind of thing if you don’t mind?

As I’ve got 2 x impees doing the same thing but reporting to 2 different Xively “devices”, I’ve created 2 different Imp “Models”:


in each of these “models”, I’ve just updated the Xively AGENT code with the relevant “API_Key” and “Feed_ID” parameters.

The downside of this is that as I get more and more Imps using this same “Model” “prototype”, I have to keep producing more and more individual “Models” which becomes a nightmare to make “firmware” updates across the same prototype.

I assume there’s a way of doing the same thing from a single “model”?

That would be easier as I get more & more Impees but I don’t really see any way of achieving that with the Xively AGENT code the way it appears to need to work (although I rely on my NOVICE status when making such statements).

In my naive mind, I would think the only way I could achieve using a SINGLE IMP “MODEL” would be to use the same API_Key for all Xively “Devices” and then use the Impee’s unique ID # as the “Feed_ID” but that doesn’t appear to be at all possible given that Xively auto-assign the “Feed_ID” and there doesn’t appear to be any way of changing that (again; I rely on my NOVICE status when making such statements).

Thanks again,

Yeah, there is nothing you can do about how Xively assigns Feed and Key values. And yes, you will want to make a separate model for every device you build where the code isn’t exactly the same. In fact, I’ve found that naming models uniquely is very helpful for keeping things organized. I believe the reason that things are set up the way they are is that someone selling a commercial Imp product would want to quickly assign a single model to many devices at once, with identical code. Commercial Imp products are definitely the priority of Electric Imp, since they pay the bills. We devs get to ride along on the service for free, which is pretty awesome.

Thanks Joel,
I just read a little of the the Xively blurb about “Provisioning”, I’m tipping that could be a way to get this done but; as you say, it’s a “Commercial” type product, so it’ll cost to go down that road just to see where it takes me.

Yeah, I totally agree with your about the AWESOME free service that Electric Imp provide to people like me who just want to muck around for hobbyist type pursuits. For $150, I’ve got everything I need to tinker with the Electric Imp till my heart’s content (that is until my wife gets sick of me sitting in front of a computer & cuts me off; then I’m in trouble).

Thanks for your (and everyone else’s) help with my learning; I greatly appreciate the time you guys & gals take for me.


:slight_smile: I’m fairly obsessed with Imping things, and my wife has been quite understanding. I’ve learned a ton over the past year while dev’ing with the Imp. It is such a powerful device.