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