Loading data to keen.io through json from array

Dear all,

I’m a newbie to Electricimp working on a IOT project. we have sensor data that we want to upload to electric imp.

There are about 256 integer values we wanted to upload every one minute(which may vary). This data is sent as a array from the device to the agent. What we wanted to do was upload to the keen.io dynamically, if there are 1 to n variables we will send a log data like this {p1 = 40, p2 = 30, p3 = 60…pn = 70}. The keen io example that are existing is loading only a fixed couple of values like shown below

keenData <- {
keen = {
timestamp = ts

temp = data.temp,
light = data.light,
buttonState = data.bs


what i would like to know is how can i convert my array in to a data that can be uploaded to keen.io ?

So far i have attempted to concatenate as a string and tried sending it but didn’t work.


`for (local i = 0 ; i < 10 ; i++)
                roll = 1.0 * math.rand() / RAND_MAX
                roll = (roll * 12) + 1
                arry.insert(i, 78)
                if (keyst)
                keyst = format("%s,P%i:%i", keyst, i, roll)
                keyst = format("P%i:%i", i, roll)
            keyst = format ("[{%s}]", keyst)
             keen.sendEvent("tempBugs", keyst, function(resp) {

Keen.IO data sent and response received:

015-03-07 18:39:23 UTC+5.5 [Agent] "[{P0:9,P1:3,P2:11,P3:8,P4:2,P5:1,P6:8,P7:2,P8:2,P9:5}]"
2015-03-07 18:39:23 UTC+5.5 [Agent] {“message”: “An event should be a JSON object of properties.”, “error_code”: “InvalidEventError”}
I get the following error

Have never used keen.io through imp but looking at error response I wondered why you are creating another array here: keyst = format ("[{%s}]", keyst)

did you try this:
keyst = format ("{%s}", keyst)

I haven’t used keen.io either, but if they want a JSON object you should send them valid JSON, which your code doesn’t.

Try something like (untested):

`local obj = {}
for (local i=0; i<10; ++i) {
  roll = math.rand();
  obj["P"+i] <- roll;
local keyst = http.jsonencode(obj);
keen.sendEvent("tempBugs", keyst, function(resp) {


Thanks Peter Your suggestion worked.


Also, note that if you’re using the Keen library you don’t need to json encode your data - you should be able to just pass the object:

local obj = {} for (local i=0; i<10; ++i) { roll = math.rand(); obj["P"+i] <- roll; } keen.sendEvent("tempBugs", obj, function(resp) { server.log(resp.body);