Go Down

Topic: processing incoming text string from GET (Read 2361 times) previous topic - next topic

Jeremy-arduino

Hi, I am trying to get something going where I can enter a string in an input box on a browser and through the ethernet shield the arduino prints it to the lcd after formatting it correctly. This is what I have but it does not work and causes the arduino to reset or crash.

Quote

            }else if(newLine.indexOf("GET /Message=") != -1){
               newLine = newLine.replace("GET /Message=", "");
               newLine = newLine.replace("%20", " ");
               lcd.setCursor(0, 1);
               lcd.print(newLine);
               messageSet = 1;
            }


What am I doing wrong? Thanks.

Jeremy


johnwasser

When the Arduino has crashed on me it has always been because I ran out of memory.  If you have some Serial.print() debug output or other string constants you don't need, try commenting them out.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Jeremy-arduino

#2
Mar 16, 2011, 06:09 pm Last Edit: Mar 16, 2011, 06:12 pm by Jeremy-arduino Reason: 1
Hi John,
So I took out everything that did not have to do with what I was trying to accomplish and it worked... Well, sorta. It seems to display random Chinese characters in front of my text string, and after my text string it displays " HTTP/1.1" and some more Chinese characters. Any idea how to make this work? I looked at the trim function but that only looks like it takes out "white spaces".

Jeremy

WillR

The Ethernet shield is FLAKY -- or maybe it's the software libraries.

Sometimes it works -- sometimes it doesn't. Sometimes I get garbage data -- sometimes none at all.

It definitely needs work.

Jeremy-arduino

@ Will, Not sure either but I am making some progress. This is what I have to do. Before "newLine" gets the new line from the client, I have to "zero" it out.
newLine = "";

Then, When I run my IF bit of code, this what I have to do:
Quote
if(newLine.indexOf("GET /Message=") != -1){
               newLine = newLine.replace("GET /Message=", ""); // Get rid of the stuff before the message
               newLine = newLine.replace("%20", " "); // Make the spaces real spaces
               newLine = newLine.replace("HTTP/1.1", ""); // Get rid of this from the end of the message
               newLine = newLine.replace("\n", ""); // Doing this gets rid of one of the  weird characters
               newLine = newLine.replace("\r", ""); // Doing this gets rid of the other  weird characters
               lcd.setCursor(0, 1); // Set cursor to column 0 on line 1, which is the second line
               lcd.print("                "); // Erase anything that is on this line
               lcd.setCursor(0, 1); // Set the cursor back to the beginning of this line to print the message
               lcd.print(newLine); // Print the final edited version of "newLine"
            }

So, this works as a stand-alone application, but before, I might have been running out of memory... How should I go about fixing that? Thanks!

Jeremy

zoomkat

I use the below server code to test my routerbot that has two servos. I use a url like below in a web page to send the info to the arduino server, and have the serial monitor open to see what the server does.

http://192.168.1.102:84/?-1000-2000

Code: [Select]

//zoomkat 10-22-10
//routerbot code
//for use with IDE 0021
//open serial monitor to see what the arduino receives
//

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
  192, 168, 1, 102 }; // ip in lan
byte gateway[] = {
  192, 168, 1, 1 }; // internet access via router
byte subnet[] = {
  255, 255, 255, 0 }; //subnet mask
Server server(84); //server port

String readString, servo1, servo2;

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
//////////////////////

void setup(){

  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  myservo1.attach(7);
  myservo2.attach(6);
  Serial.println("bot21"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString);

          //readString looks like "GET /?-1500-1500 HTTP/1.1"

          if (readString.length() >0) {
            Serial.println(readString);

            servo1 = readString.substring(7, 11);
            servo2 = readString.substring(12, 16);

            Serial.println(servo1);
            Serial.println(servo2);

            int n1;
            int n2;

            char carray1[6];
            servo1.toCharArray(carray1, sizeof(carray1));
            n1 = atoi(carray1);

            char carray2[6];
            servo2.toCharArray(carray2, sizeof(carray2));
            n2 = atoi(carray2);

            myservo1.writeMicroseconds(n1);
            myservo2.writeMicroseconds(n2);

            //myservo.write(n);
            readString="";
          }
          ///////////////////

          //now output HTML data header
          client.println("HTTP/1.1 204 Zoomkat");
          client.println();
          client.println();
          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Jeremy-arduino

@zoomkat That was a good read. I was not aware of what "substring" did. But, that would mean that I would have to send the same set of variables and value length every time. I don't think that I can do that.

I found something in another thread here (http://arduino.cc/forum/index.php/topic,43764.msg317064.html#msg317064) from a while back that seems a little bit interesting. It uses ajax to send the info without refreshing the page.

I got a nasty-gram in my email from a reply to this current thread in regards to the amount of information that I was posting. The post however was deleted, but I will try my best to conform to the requests of its author.

What my intent in this project to do is this. Have a web page that is hosted on the arduino, accessed through the ethernet shield. The web page should have a text box on it and a button next to it. The goal so far is to type something into the box and hit the button. When the button is pressed, ajax sends the text to the arduino in a GET line that looks like this:
Code: [Select]
GET /Message=Hello%20World!\r\n
Now what the arduino is supposed to do is check if that line of text has this phrase in it:
Code: [Select]
GET /Message=
If it does then the arduino executes this bit of code:
Quote
if(newLine.indexOf("GET /Message=") != -1){
               newLine = newLine.replace("GET /Message=", ""); // Get rid of the stuff before the message
               newLine = newLine.replace("%20", " "); // Make the spaces real spaces
               newLine = newLine.replace("HTTP/1.1", ""); // Get rid of this from the end of the message
               newLine = newLine.replace("\n", ""); // Doing this gets rid of one of the  weird characters
               newLine = newLine.replace("\r", ""); // Doing this gets rid of the other  weird characters
               lcd.setCursor(0, 1); // Set cursor to column 0 on line 1, which is the second line
               lcd.print("                "); // Erase anything that is on this line
               lcd.setCursor(0, 1); // Set the cursor back to the beginning of this line to print the message
               lcd.print(newLine); // Print the final edited version of "newLine"
            }

I don't need any response back from the arduino at this point. My next hurdle is making the button send whatever is in the textbox on the web page, through ajax to the arduino in the above formated string. I am testing this on my own web server but cannot seem to figure out how to get it to send it. This is what I have right now:
Code: [Select]
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

ajaxRequest = new XMLHttpRequest();
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "serverTime.php", true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Name: <input type='text' onChange="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>

I got this from tizag.com but the tutorial didn't seem to tailor to my needs. I have changed it a little so that the overall size might still work on the arduino and not crash from memory maxing out.
What I would like is for the form section of the above code to look like this:
Code: [Select]
<form name='myForm'>
Name: <input type='text' name='message' /> <br />
Send: <input type='button' name='sendButton' onClick="ajaxFunction(message);" />
</form>

I just can't figure out what I need to change in the function to make it send the string that I need. I know that:
Code: [Select]
function ajaxFunction(){
should be something like:
Code: [Select]
function ajaxFunction(message){
and:
Code: [Select]
ajaxRequest.open("GET", "serverTime.php", true);
should look something like:
Code: [Select]
ajaxRequest.open("GET", message, true);
but I can't figure it out. Any ideas, help or direction would be greatly appreciated.

I hope that I have enough information in here now so that a certain someone will be appeased. Just kidding. I got a kick out of your post even if nobody else saw it. :)

Jeremy

Go Up