Receiving an array of strings via http

I’m trying to wrap my head around this problem and have gotten stuck. I’m trying to link my imp up to a 64 pixel long string of WS2801 chips/leds. I’m attempting to post an array of values via an html form and have the imp update the strip with these colors over spi. I have a little explaining to do and my questions are at the bottom of this. Thank you in advance for your help.

Basically I have a html form posting an array of html hex colors. I have 64 inputs similar to this:
<input type="color" name="value[1]" value="#636F57"> <input type="color" name="value[2]" value="#636F57"> ...

I’m trying to figure out the structure of the input I will be receiving so I can parse and write it out to spi. Here’s my sample code trying to figure this out:

`

class LedInput extends InputPort
{
numPixels = 64;

currentPixel = 0;

constructor()
{
    // Call the base constructor first
    base.constructor("array", "array");
}

function set(value)
{ 
    //output point A
    server.log(typeof value);
    
    foreach (val in value)
    {
        //output point B
        server.log(typeof value.a);
    }    
}

}

// Register with the server
imp.configure(“Web Color”, [ LedInput() ], []);

// End of code.
`

According to http://devwiki.electricimp.com/doku.php?id=httpapi:

If you send url-encoded fields e.g. value[a] = 1, you'll get a table on your imp like {a = 1}.

If you’ve made it this far, thank you :slight_smile:

Here are my questions/problems:

  1. At output point A, the type is reported as array. Shouldn't this be a table?
  2. At output point B, the type is reported as null. Am I iterating through this the right way?
  3. Any suggestions, or examples I missed in my searches? Would you recommend any changes?

After I get this sorted out, I plan to start following the advice posted here to write this data via spi:
http://forums.electricimp.com/discussion/197/electric-imp-out-of-memory-crashing/p1

Pretty sure at A it would be an array, but that’s based on having sent multiple things encoded in the URL previously, eg value=[1,2,3,4]. I’ve not tried it with posts myself.

Output point B should be server.log(typeof val) shouldn’t it? val is the element from the iterator.

Thank you so much. You were right, I had an error in my code. Thanks again.

I ran into another issue that I believe is related, so I hope someone is still watching this thread :slight_smile:

Extending on my code above(without an error :wink: ), I am either using the wrong string functions, or it’s causing another error I’m stumped on. Point Q is baffling me.

`class LedInput extends InputPort
{
numPixels = 64;

currentPixel = 0;

constructor()
{
    // Call the base constructor first
    base.constructor("array", "array");
}

function set(value)
{ 
    //output point A
    server.log(typeof value);
    local out = blob(3*numPixels);   
       
    
    foreach (val in value)
    {
        //output point B
        server.log(typeof val);
        server.log(val);
        
        //Point Q
       // server.log(val.len());
    }
}

}

// Register with the server
imp.configure(“Web Color”, [ LedInput() ], []);

// End of code.`

As is, Output B is saying it’s a string, and it is what I expect it to be, an rgb color in hex format. When you uncomment below Point Q, I receive the following output:

Saturday, January 05, 2013 10:31:01: array Saturday, January 05, 2013 10:31:01: null Saturday, January 05, 2013 10:31:01: (null : 0x0x0) Saturday, January 05, 2013 10:31:01: ERROR: the index 'len' does not exist Saturday, January 05, 2013 10:31:01: ERROR: at set:27

According to the following example provided:

http://devwiki.electricimp.com/doku.php?id=webcolor

Particularly, the second line of the “hexToInteger” function, I was under the assumption this would work. Did I screw up again?

I also tried replacing point Q with the following:

//Point Q server.log(val.slice(1,3));

That produced a similar error.

I have been unable to find anything in you documentation about string functions, From my perspective, based on examples provided, what I have should work, but this is not the first time I’ve been wrong.

I tried going through the squirrel language docs and found this listed.

http://www.squirrel-lang.org/doc/squirrel3.html#d0e2819

Anyone have a suggestion?

Thank you again for any help. I’ve really been enjoying playing with the imp so far, and I hope to overcome this hurdle.

That’s strange; if val is a string (ie the typeof is reporting string - you’ve not posted that log) then .len() should be valid on it…

My fault again, index 0 of the array, which I was not passing in, was being set to null on the imp side. I had assumed the foreach loop, or the array it’s going over wouldn’t have position 0 filled if I didn’t pass it in. I wasn’t aware that the value would be filled in for me anyway with null.

By changing my form to start from array position 0, not 1, my issue went away. Didn’t think it would matter when I originally added it. Thanks again for your help.

Ah, that would explain it! I’m sure your pain will be someone else’s gain upon reading this :slight_smile: