finder.find and finder.getValue() using FORMS with HEX input problem [SOLVED]

I know how the get data out of a submitted FORM like this one:

http://192.168.1.1?DT1=192&DT2=168&DT3=2&DT4=1

As you can see the data is 192, 168, 2, 1

but now I have this as FORM submitted:

http://192.168.1.1?DT1=DE&DT2=AD&DT3=BE&DT4=EF&DT5=FE&DT6=ED

As you can see, this time the data is: AD, BE, EF, FE, ED

My problem is finder.getValue() doesn't give these. And actually they are meant to be HEX and not text.

if the users fill out 0xAD, 0xBE, 0xEF, 0xFE, 0xED finder.getValue() are all 0.

Do you have a link to the library you are using? I'd guess that the getValue() method is converting the value in the string to an int, and DE, AD, etc. are not numbers.

if the users fill out 0xAD, 0xBE, 0xEF, 0xFE, 0xED finder.getValue() are all 0.

http://192.168.1.1?DT1=DE&DT2=AD&DT3=BE&DT4=EF&DT5=FE&DT6=ED

DE != 0xDE AD != 0xAD etc.

Hi PaulS,

This is a link to the library: http://www.arduino.cc/playground/Code/TextFinder

  long TextFinder::getValue(char skipChar)
  {
      boolean isNegative = false;
      long value = 0;   
      char c;

      while( (c = myRead()) != '-' && (c < '0' || c > '9') )
         ;// ignore non numeric leading characters  
      do{ 
        if(c == skipChar)
           ; // ignore this charactor        
        else if(c == '-') 
           isNegative = true;          
        else if(c >= '0' && c <= '9')        // is c a digit?  
           value = value * 10 + c - '0';         
        c = myRead();        
      }
      while( (c >= '0' && c <= '9')  || c == skipChar );
      if(isNegative)
        value = -value;   
     return value;
  }

So, a value like AD (or even 0xAD) is not considered valid input.

So, a value like AD (or even 0xAD) is not considered valid input.

Oh ohw, that means I have a big problem. Now I'll have to find an other way to get data out of the URL, because I want to use the FORMS to submit text like "hello world" too

welcome back JO3RI! If you are certain you will alway receive hex, you can just modify the code to accept hex, just 10->16, and add a thru f values.

Now I'll have to find an other way to get data out of the URL, because I want to use the FORMS to submit text like "hello world" too

There are methods in that library to get floats and strings, as well as ints.

As liudr points out, modifing that code to handle other notations (binary, octal, hexadecimal, etc.) is not all that difficult.

Mmmmm. For now I’m going to concentrate on getting those MAC values out of the URL. I made a more simple version to show what it is doing:

first time the sketched is ran:

when you press the submit button, as you can see things go wrong when MAC address is submitted

and here you have the code:

#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
#include <EEPROM.h>

//seting up the EthernetShield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192,168,1,2};
byte subnet[] = {255,255,255,0};
byte gateway[] = {192,168,1,1};
Server server(80);

const byte ID = 0x91; //used to identify if valid data in EEPROM the "know" bit, 
// if this is written in EEPROM the sketch has ran before
// We use this, so that the very first time you'll run this sketch it will use
// the values written above. 

void setup()
{
  ShieldSetup (); //Setup the Ethernet shield
  server.begin(); //starting the server
}

void ShieldSetup()
{
  int idcheck = EEPROM.read(0);
  if (idcheck != ID){
    //ifcheck id is not the value as const byte ID,
    //it means this sketch has NOT been used to setup the shield before
    //just use the values written in the beginning of the sketch
    Ethernet.begin(mac, ip, gateway, subnet);
  }
  if (idcheck == ID){
    //if id is the same value as const byte ID,
    //it means this sketch has been used to setup the shield.
    //So we will read the values out of EERPOM ans use them
    //to setup the shield.
    for (int i = 0; i < 6; i++)
    {
      mac[i] = EEPROM.read(i+1);
    }
    for (int i = 0; i < 4; i++)
    {
      ip[i] = EEPROM.read(i+7);
    }
  }
  Ethernet.begin(mac, ip, gateway, subnet);
}

void loop()
{
  Client client = server.available();
  if (client) {
    TextFinder  finder(client );
    while (client.connected()) {      
      if (client.available()) {          
        if( finder.find("GET /") ) {
          if (finder.findUntil("SBM", "\n\r")){ //when "SBM" is found a form has been submitted.
            byte SET = finder.getValue();
              while(finder.findUntil("DT", "\n\r")){ //look for "DT" and get all the values
                int val = finder.getValue(); // get all the numbers behind DT => DT1, DT2, ...
                if(val >= 1 && val <= 6) { // now for DT1 till DT6 means value for MAC
                  mac[val - 1] = finder.getValue(); // get the value behind the "=" => DT1=..
                }
                if(val >= 7 && val <= 10) { // now for DT7 till DT10 means value for IP
                  ip[val - 7] = finder.getValue(); // get the value behind the "=" => DT7=..
                }
              }
            for (int i = 0 ; i < 6; i++)
            {
              EEPROM.write(i + 1,mac[i]); //write those MAC values to EEPROM
            }
            for (int i = 0 ; i < 4; i++)
            {
              EEPROM.write(i + 7, ip[i]); //write those IP values to EEPROM
            }
            //set ID to the known bit, so when you reset the Arduino
            //from now on it will use the EEPROM values
            EEPROM.write(0, 0x91);
            // if al the data has been written to EEPROM we should reset the arduino
            // for now you'll have to use the hardware reset button
          }
          }           
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.print("<html><body><table><form><input type=\"hidden\" name=\"SBM\" value=\"1\">");
          client.print("<tr><td>MAC: <input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT1\" value=\"");
          client.print(mac[0],HEX);
          client.print("\">.<input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT2\" value=\"");
          client.print(mac[1],HEX);
          client.print("\">.<input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT3\" value=\"");
          client.print(mac[2],HEX);
          client.print("\">.<input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT4\" value=\"");
          client.print(mac[3],HEX);
          client.print("\">.<input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT5\" value=\"");
          client.print(mac[4],HEX);
          client.print("\">.<input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT6\" value=\"");
          client.print(mac[5],HEX);
          client.print("\"></td></tr><tr><td>IP: <input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT7\" value=\"");
          client.print(ip[0],DEC);
          client.print("\">.<input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT8\" value=\"");
          client.print(ip[1],DEC);
          client.print("\">.<input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT9\" value=\"");
          client.print(ip[2],DEC);
          client.print("\">.<input type=\"text\" size=\"3\" maxlength=\"3\" name=\"DT10\" value=\"");
          client.print(ip[3],DEC);
          client.print("\"></td></tr><tr><td><input type=\"submit\" value=\"SUBMIT\"></td></tr>");
          client.print("</form></table></body></html>");
          break;
        }
      }
    delay(1);
    client.stop();
  }
}

when you press the submit button, as you can see things go wrong when MAC address is submitted

Add some Serial.print()s to see what is happening. What value gets assigned to val by

                int val = finder.getValue();

?

What changes have you made to the finder class to support getting hex values as integers? What value gets assigned to val by

                if(val >= 1 && val <= 6) { // now for DT1 till DT6 means value for MAC
                  mac[val - 1] = finder.getValue();

?

Hi PaulS,
@liudr, thanks man. I’m back with great ideas :smiley:

I didn’t change a thing. I was nearly giving the code as is for now,

BUT … I found a way around the problem:

<body>

<script>
function hex2num (s_hex) {eval("var n_num=0X" + s_hex);return n_num;}
</script>

<html>
<form>
<input id="Text1" type="text" size="2" value="DE"/>:
<input id="Text3" type="text" size="2" value="AD"/>:
<input id="Text5" type="text" size="2" value="BE"/>:
<input id="Text7" type="text" size="2" value="EF"/>:
<input id="Text9" type="text" size="2" value="FE"/>:
<input id="Text11" type="text" size="2" value="ED"/>
<input id="Text2" type="hidden" name="DT1"/>
<input id="Text4" type="hidden" name="DT2"/>
<input id="Text6" type="hidden" name="DT3"/>
<input id="Text8" type="hidden" name="DT4"/>
<input id="Text10" type="hidden" name="DT5"/>
<input id="Text12" type="hidden" name="DT6"/>
<input id="Button1" type="submit" value="SUBMIT" onclick="document.getElementById('Text2').value = hex2num(document.getElementById('Text1').value);document.getElementById

('Text4').value = hex2num(document.getElementById('Text3').value);document.getElementById('Text6').value = hex2num(document.getElementById

('Text5').value);document.getElementById('Text8').value = hex2num(document.getElementById('Text7').value);document.getElementById('Text10').value = hex2num

(document.getElementById('Text9').value);document.getElementById('Text12').value = hex2num(document.getElementById('Text11').value);" /> 
</form>
</html>
</body>

When you save this piece of HTML to a test.html file you’ll see that when you enter HEX values, the submit form converts them into DEC values and then submits them: PROBLEM SOLVED.

1 Like

PROBLEM SOLVED.

Great.

So you let the script (or whatever processor that is running the script) do the conversion, good solution.

JO3RI, I've recently made a serial lcd keypad terminal panel. Since you use Ethernet you will be able to simplify your display setup with this panel. It handles line breaks wrapping and scrolling and a lot more. Tons of functions for only two IO lines. I'm open for trade (old computer processors will be best:))

http://liudr.wordpress.com/phi-panel/

Hi liudr,

it is just javascript, so that the browser client (firefox, safari, IE) will do the conversion before it is submitted. Clever solution, if I may say :D

about

I'm open for trade

I'll send you an email.