Updating 2 Thingspeak Fields at the same time

I am new to the Imp, Squirrel, Thingspeak and electronics in general, but seem to be learning quicker than I thought.  Hopefully someone with more know-how can help me with my first Device-to-Agent-to-Thingspeak adventure.

I have created a two temperature sensor device that I plan to use Thingspeak to remotely monitor the two temperatures.  Currently I am able to poll the temperatures at the same time, log them etc.

When I get the data to the Agent and I attempt to post the values to Thingspeak it appears I am forced to split my posting into staggered posts due to the Thingspeak's posting limit (1-post per Thingspeak channel every 15 seconds).  That said, I got to playing with the Agent code, trying to create code that would re-combine my values into a single Thingspeak post.

So far, I have my two "device.on" functions as follows:

device.on("updateTemp2", function(Update_Temp2) {

    server.log("Update_Temp2 Value: " + Update_Temp2 + "*");


device.on("updateTemp5", function(Update_Temp5) {

    server.log("Update_Temp5 Value: " + Update_Temp5 + "*");



The server logs for these come out with the data I want, so I know things are working up to this point.  Now I need the code to combine the Update_Temp2 and the Update_Temp5 values for my Thingspeak post.  What I have so far is a 3rd “device.on function that I was hoping would receive the output from the two above device.on functions.

device.on("updateTemp", function (combined) {

    local request = http.post(thingspeakUrl, headers, ("field1"+"="+Update_Temp2+"&"+"field2"+"="+Update_Temp5));

    local response = request.sendsync();

    return response;




It seems I am struggling with the function scoping because my current error is as follows: ERROR: the index 'Update_Temp2' does not exist.  I have tried a handful of things but, I cannot seem to get the information to pass to my combined function.  I am sure there is a rookie mistake here, I just need someone to point it out.


If anyone else is having this problem, I was able to find my solution. I needed to convert this all to a single “agent.send” function on the device.nut and put the exact call information into the “object” location.

Device.nut code:
agent.send(“updateTemp”, (“field1”+"="+UpdateTemp2+"&"+“field2”+"="+UpdateTemp5));

Then in the Agent.nut:
device.on(“updateTemp”, function (post) {
local request = http.post(thingspeakUrl, headers, post);
local response = request.sendsync();
return response;

Hope this may help someone else out there.

I am no Thingspeak expert but thought to comment to test my own knowledge.

I have managed to get data from agent to 2 Thingspeak channels before and to do this you need to set up and use 2 write API keys (1 per channel).

I believe the alternative, which I could not get to work correctly, is to set up 2 fields within a single channel. According to the Thingspeak Channel Settings page you can set up to 8 fields.

I too attempted to create a string as you have shown for “field1” and “field2” but only field1 data was shown on my chart. So you not alone in trying to solve this…

If you are updating two fields on the same ThingSpeak Channel. You need to enable each field on the Channel Settings screen. By default, field1 is checked / enabled.

I’m doing it, `local s = “field1=”+value1;+"&field2="+ value2+"&field3="+ value3+"&field4="+ value4 ;
server.log("what was sent: "+ s);
local response = httpPostToThingSpeak(s);

    imp.wakeup(60, SendtoThingSpeak); `

each minute, and it work for 24 to 48 h, and the thingspeak look to be receiving two post each time i send one, so the “response” of thingspeak to each of my subsequent post is like this

-0 // when thingspeak response 0, it mean there is an issue
-7611 // thingspeak response with the number of entry i’ve sent on that channel
-0 // thingspeak réponse is 0 again, but i’ve sent the same post than before
-7613 // strange, thingspeak response with 2 entry more than the last successful

anybody can help me?

G’day Johnygab.

I worked out how to do this on the weekend.

This code helped me get the IMP talk to ThingSpeak using a single data stream from the IMP and one field on ThingSpeak:

Using the Channel Settings in ThingSpeak I added fields 2 and 3. I created an array of data on the IMP and sent it to the Agent, the Device code was as follows (not all the code but hopefully you get the gist):

local temp = sensor.getTemperature();
local rssi = imp.rssi();
local voltage = sendAnalogInput();
responsefromimp <- [temp, rssi, voltage];
agent.send("updateCharts", responsefromimp); 

It is handled on the Agent as follows:

local field = “field1=”;
local field2 = “&field2=”;
local field3 = “&field3=”;
device.on(“updateCharts”, function(responsefromimp) {
local response = httpPostToThingspeak(
field + responsefromimp[0]
+ field2 + responsefromimp[1]
+ field3 + responsefromimp[2]);


your doing the same thing as me, and it will work, but after a few days, it won’t :stuck_out_tongue:

Hmmm, I haven’t had any trouble so far…