I'm still working on this and all ears. Focusing on just 2 temps in one message for now. What I did get working was this (again, where 'datapoint' is an array of 7 temperatures)
Device
//Thingspeak temp0 - Field 1
agent.send("thingtemp0", datapoint);
imp.sleep(20);
//Thingspeak temp1 - Field 2
agent.send("thingtemp1", datapoint);
Agent
local channel = "XA6D1INCQFWNDMK5"
local thingSpeakUrl = "http://api.thingspeak.com/update";
local headers = {
"Content-Type" : "application/x-www-form-urlencoded",
"X-THINGSPEAKAPIKEY" : "XA6D1INCQFWNDMK5"
};
local field1 = "field1";
function httpPostToThingSpeak (data) {
local request = http.post(thingSpeakUrl, headers, data);
local response = request.sendsync();
return response;
}
device.on("thingtemp0", function(datapoint) {
local response = httpPostToThingSpeak(field1+"="+datapoint.temp[0]);
server.log("Thingspeak Channel "+ channel + " feed 1 " + response.body);
});
// -
local field2 = "field2";
function httpPostToThingSpeak (data) {
local request = http.post(thingSpeakUrl, headers, data);
local response = request.sendsync();
return response;
}
device.on("thingtemp1", function(datapoint) {
local response = httpPostToThingSpeak(field2+"="+datapoint.temp[1]);
server.log("Thingspeak Channel entries: " + response.body);
});
What I thought would work is something like below, but the "&" operator isn't working. The code checks out ok in the Electric Imp IDE and uploads to the Imp, but the agent side returns an error:
Error: bitwise op between 'string' and 'string'
Device
//Thingspeak code all temps at once
agent.send("thingtemp", datapoint);
Agent
local channel = "XA6D1INCQFWNDMK5"
local thingSpeakUrl = "http://api.thingspeak.com/update";
local headers = {
"Content-Type" : "application/x-www-form-urlencoded",
"X-THINGSPEAKAPIKEY" : "XA6D1INCQFWNDMK5"
};
local field1 = "field1";
local field2 = "field2";
local field3 = "field3";
local field4 = "field4";
local field5 = "field5";
local field6 = "field6";
local field7 = "field7";
function httpPostToThingSpeak (data) {
local request = http.post(thingSpeakUrl, headers, data);
local response = request.sendsync();
return response;
}
device.on("thingtemp", function(datapoint) {
local response = httpPostToThingSpeak(field1+"="+datapoint.temp[0]&field2+"="+datapoint.temp[1]);
});