Relaying blob data from agent to imp

Hi,

I am trying to receive various parameters from a HTTP request (from app eventually) to change the values of a blob to be written to UART. Is there any easier way than this?

`// Agent Code
// https://agent.electricimp.com/myimpid/?ch1=255&ch2=125&ch3=125&ch4=125&ch5=125&ch6=125&ch7=125&ch8=125&ch9=125&ch10=125&ch11=125&ch12=125 --> example http request

http.onrequest(function(req, res) {
res.send(200, “OK”);

outBlob <- blob(12);
for (local i = 0; i < 12; i++) { // populate the blob with null data
outBlob.writen(0x00, ‘b’);
}
local newch1 = req.query.ch1;
local newch2 = req.query.ch2;
local newch3 = req.query.ch3;
local newch4 = req.query.ch4;
local newch5 = req.query.ch5;
local newch6 = req.query.ch6;
local newch7 = req.query.ch7;
local newch8 = req.query.ch8;
local newch9 = req.query.ch9;
local newch10 = req.query.ch10;
local newch11 = req.query.ch11;
local newch12 = req.query.ch12;
outBlob.seek(1);
outBlob.writen(newch1, ‘b’);
outBlob.seek(2);
outBlob.writen(newch2, ‘b’);
outBlob.seek(3);
outBlob.writen(newch3, ‘b’);
outBlob.seek(4);
outBlob.writen(newch4, ‘b’);
outBlob.seek(5);
outBlob.writen(newch5, ‘b’);
outBlob.seek(6);
outBlob.writen(newch6, ‘b’);
outBlob.seek(7);
outBlob.writen(newch7, ‘b’);
outBlob.seek(8);
outBlob.writen(newch8, ‘b’);
outBlob.seek(9);
outBlob.writen(newch9, ‘b’);
outBlob.seek(10);
outBlob.writen(newch10, ‘b’);
outBlob.seek(11);
outBlob.writen(newch11, ‘b’);
outBlob.seek(12);
outBlob.writen(newch12, ‘b’);
server.log(outBlob);
device.send(“ping”, outBlob);
})`

//Device code agent.on("ping", function(value) { outBlob = value; server.log(outBlob); })

Is there any easier way to do this? I tried to make a for loop like this:

for (local i = 1; i < 12; i++) { // populate the blob with null data local "newch"+i = "req.query.ch"+i; outBlob.seek(i); outBlob.writen("newch"+i, 'b'); }

But obviously I realize this is actually writing “newch1” instead of the value of newch1. Also is it possible to send a blob like this from the agent to the imp? From what I read it is, however when I server.log(outBlob), I get “(instance : 0x0x2000e47c)”.

Any help would be nice,
Thanks

The pointer auto-increments, you don’t need to seek unless you want to seek somewhere the pointer isn’t. As you’re creating a blob (ie, pointer will be 0 at first) then writing bytes into it, each byte write will increment the pointer by 1 automatically.

Tables in Squirrel (like objects in Javascript, but unlike anything in C/C++) are just dictionaries really. So if t is a table, t.foo is the same thing as t[“foo”]. That means you can write your loop as:
for (local i=1; i<12; i++) { local val = req.query["ch"+i]; outBlob.writen(val, 'b'); }
though note that the code seems a little confused as to whether the blob is 12 or 13 bytes long – if you seek to position 12, then write another byte, the blob will be 13 bytes long altogether.

As for your other question, blobs are instances of type Blob, and they can’t be logged directly (until the next release, anyway).

Peter

Thank you Peter! Still adjusting to Squirrel… Ok so a few more questions:

This is the agent code now:

http.onrequest(function(req, res) { res.send(200, "OK"); local outBlob = blob(12); for (local i=0; i<12; i++) { local val = req.query["ch"+(i+1)]; outBlob.writen(val, 'b'); } device.send("ping", outBlob); })

And device code:

`dmxoutput <- hardware.uart57;
andgate <- hardware.pin2;
dmxoutput.configure(250000, 8, PARITY_NONE, 2, NO_CTSRTS);
andgate.configure(DIGITAL_OUT);
outBlob <- blob(12);

for (local i = 0; i < 12; i++) { // populate the blob with null data
outBlob.writen(0x00, ‘b’);
}

function refresh() {
andgate.write(0);
andgate.write(1);
dmxoutput.write(outBlob);
imp.wakeup(0.001, refresh);
}

agent.on(“ping”, function(value) {
outBlob = value;
refresh();
})

imp.configure(“DMX Controller”,[],[]);
refresh();`

My question is, and I sending/translating the blob on the device side correctly? The new channel data (ch1=255,ie) is not being rewritten to the outBlob on the imp side. Is this not the correct way:
agent.on("ping", function(value) { outBlob = value; refresh(); })

Also when using outBlob.writen for blobs in a loop, you do not have to use outBlob.seek, to arrange the pointer? The Squirrel API handbook says this -I think (http://www.squirrel-lang.org/doc/sqstdlib3.html#d0e841).

Thanks again Peter!

I thought maybe this might work:

agent.on("ping", function(value) { for (local i=0; i<12; i++) { value.seek(i); local val = value.readn('b'); outBlob.seek(i); outBlob.writen(val, 'b'); server.log(val); } refresh(); })

But I only get 0’s returned to the server log…

Thanks Hugo! Now my problem is I cant get the blob data being sent from the agent to be transferred to the blob on the imp.

Agent code:
`outBlob <- blob(12);
http.onrequest(function(req, res) {
res.send(200, “OK”);
for (local i=0; i<12; i++) {
local val = req.query[“ch”+(i+1)];
outBlob.writen(val, ‘b’);
server.log(val);
}

device.send(“newDMX”, outBlob);
})`

Device code:
agent.on("newDMX", function(value) { for (local i=0; i<12; i++) { local val = value.readn('b'); outBlob.writen(val, 'b'); server.log(val); } refresh(); })

The server log on the agent side shows the correct values being written to the blob, however, when I try to read the values on the imp side all I get are 0’s. This is a terrible annoyance, been stuck here for almost two days…

Can I not send a blob from the agent? I tried http.base64encode(outBlob), but this doesn’t help…

You can send blobs back and forth between devices and agents - we do this rather a lot.

I slightly modified your code, and tested this out (on the release you have):

agent: (I’ve put a delay in here to ensure the imp is ready for the transmission, because I’m not doing this on an http request)

`imp.wakeup(1, function() {
outBlob <- blob(12);
for (local i=0; i<12; i++) {
local val = (128+i);
outBlob.writen(val, ‘b’);
server.log(val);
}

device.send("newDMX", outBlob);

});
`

device:

`
imp.configure(“test”, [], []);

agent.on(“newDMX”, function(v) {
foreach(a in v) server.log(format("%02x",a));
});
`

…this logs correctly:
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 80
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 81
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 82
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 83
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 84
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 85
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 86
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 87
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 88
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 89
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 8a
Mon Jun 03 2013 10:52:04 GMT-0700 (PDT): 8b

Also: you don’t need to do all the writen type stuff if you’re writing bytes either. You can just index with [] (for read and write), eg blob[5] = 0x23.

Also tried it with readn(), also worked fine:

agent.on("newDMX", function(v) { for(local a=0;a<12;a++) { local b = v.readn('b'); server.log(format("%02x",b)); } });

So I got it to work. The problem wasn’t with the blob transfer from agent to imp, but rather I needed to change the req.query.ch[i] to float before writing the blob. Thanks alot Hugo and Peter, I like the outBlob[i] = val indexing feature. Sleek.

Agent:
`outBlob <- blob(12);
http.onrequest(function(req, res) {
res.send(200, “OK”);
server.log(“New Blob”);
for (local i=0; i<12; i++) {
local val = req.query[“ch”+(i+1)];
local v = val.tofloat();
outBlob[i] = v;
}

device.send(“newDMX”, outBlob);
})`

Device:
agent.on("newDMX", function(value) { outBlob = value refresh(); })

One quick note:

When I write a value (255 ie) to ch 1 in the url, all the channels come on. But channels from 2 up all control as they should. I think this is an indexing issue. Does the index 0 in a blob mean the first byte? I think the issue is with the loop, i value, and the outblob[i]. Should I start the loop at 1? This makes a 13 byte blob so I don’t think so…

Thanks again for your time.

ch 2 (in url) controls ch 1 (on controller), ch 3 controls ch2, etc.

Errrrr, float should not have made things better here. When writing to outblob[i], it’s taking an integer byte. What are the values you’re sending? (ie , what is “val” each time through the loop)

So…

`outBlob <- blob(12);

http.onrequest(function(req, res) {
res.send(200, “DMX Has been Sent”);
for (local i=0; i<12; i++) {
local val = req.query[“ch”+(i+1)];
outBlob[i] = val;
server.log(val);
}

for (local i=0; i<12; i++) {
local f = outBlob.readn(‘b’);
server.log(f);
}`

returns:

Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 255
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229
Wed Jun 05 2013 08:15:48 GMT-0400 (Eastern Standard Time): 229

The val equals 255 or 125, but returns 229. When I cast to float, it works.

`outBlob <- blob(12);

http.onrequest(function(req, res) {
res.send(200, “DMX Has been Sent”);
for (local i=0; i<12; i++) {
local val = req.query[“ch”+(i+1)];
outBlob.writen(val, ‘b’);
server.log(val);
}

for (local i=0; i<12; i++) {
local f = outBlob.readn(‘b’);
server.log(f);
}
`

Returns:

Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 255
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): 125
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): io error
Wed Jun 05 2013 08:18:57 GMT-0400 (Eastern Standard Time): at unknown:17

I think you need a seek(0) between populating the outblob and reading it. Try that first?