Pages: [1]   Go Down
Author Topic: string manipulation  (Read 1686 times)
0 Members and 1 Guest are viewing this topic.
HOLLAND
Offline Offline
Full Member
***
Karma: 0
Posts: 178
HOLLAND!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everybody I wanted my wifi robot to display a msg from my web controlling page.

So I'm using a 16x2 lcd with my ethernetshield and since i made a shiel for it taht uses the 11 and 12 pin i had to change them to 8 and 9  because ethernet shield uses 10..13.
I do have almost complete code but i can't get the lcd to display the msg i'll have to catch the msg from a GET response...

Code:
#include <WString.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 150 };
Server server(80);
String readString = String(30);

void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  lcd.begin(16, 2);
  lcd.print("setup running");
}

void loop(){
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 30) {
          readString.append(c);
        }
        if (c == '\n') {
          if(readString.contains("msg")) {
            lcd.clear();
            // magic
            // characters between:
            // 'GET /?msg=' and  'HTTP/1.1'
            // are important!
            lcd.print("blabla");
          }

          // Now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html><head><title>arduino ethernet msg board</title></head><body><center>");
          client.println("<form method=get>");
          client.println("<input type=text name=msg>");
          client.println("<input type=submit value=send>");
          client.println("</form>");
// debug
client.println(readString);
//
          client.println("</center></body></html>");
          readString="";
          client.stop();
        }
      }
    }
  }
}
// EOF *******************************

simple hello msg
Code:
GET /?msg=hello HTTP/1.1

first 10 can be discarted (i don't know how)
and last 9 can be discarted
problem is the msg is not always the same length.
and i don't know how to manipulate a string.
« Last Edit: June 15, 2010, 04:15:04 am by sasaa » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49073
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use the indexOf function to get the position of the =. Then, you can use the length function to get the string length. Then, use the substring function (with two args) to extract the portion of interest.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9542
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use the below to manipulate a GET request to produce a servo control string which is sent out the serial port.

Code:
// 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.append(c);
}

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

///////////////
//Serial.println(readString);
//readString looks like "GET /?-0p1555-1p500t1000 HTTP/1.1"

  if(readString.contains("-")) { //test for servo control sring
  readString.replace('-', '#');
  pos = readString.length(); //capture string length
  //find start of servo command string (#)
  ind1 = readString.indexOf('#');
  //capture front part of command string
  teststring = readString.substring(ind1, pos);
  //locate the end of the command string
  ind2 = teststring.indexOf(' ');
  //capturing the servo command string from readString
  finalstring = readString.substring(ind1, ind2+ind1);
  //print "finalstring" to com port;
  Serial.println(finalstring); //print string with CR
    }
  ////////////////////////
  //readString looks like "GET /?Slidervalue0=1800&Submit=Sub+0 HTTP/1.1"
  if(readString.contains("Slidervalue")) {
  ind1 = readString.indexOf('u');
  ind2 = readString.indexOf('&');
  finalstring = readString.substring(ind1+1, ind2);
  finalstring.replace('e', '#');
  finalstring.replace('=', 'p');
  Serial.println(finalstring);
  }
  ///////////////////
    
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

HOLLAND
Offline Offline
Full Member
***
Karma: 0
Posts: 178
HOLLAND!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@zoomkat

could you post your complete code?
because i missing some pieces...

thank you smiley
Logged

HOLLAND
Offline Offline
Full Member
***
Karma: 0
Posts: 178
HOLLAND!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what i have for now:

Code:
#include <WString.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 150 };
Server server(80);
String readString = String(50);

void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  lcd.begin(16, 2);
  lcd.print("setup running");
}

void loop(){
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 50){
          readString.append(c);
        }
        if (c == '\n') {
          if(readString.contains("msg")){
            lcd.clear();
            // get msg form get command!
            // really need help here
            lcd.print("blabla");
          }

          // Now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html><head><title>arduino msg board</title></head><body><center>");
          client.println("<form method=get>");
          client.println("<input type=text name=msg>");
          client.println("<input type=submit value=verstuur>");
          client.println("</form>");
          client.println(readString);
          client.println("</center></body></html>");
          readString="";
          client.stop();
        }
      }
    }
  }
}
// EOF *******************************

I can't get the msg out of the string.

I tried using this:
char end_value = readString.length();
String msg1 = readString.substring(10, end_value - 11);
but it returns nothing...

very annoying...

have confirmed the lcd and ethernet shield working smiley-grin so that aint the problem...
« Last Edit: June 19, 2010, 05:02:03 am by sasaa » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49073
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The length of the string is the number of characters in the string. Why is that value stored in a char type? It should be an int type.

I'd start with sending the entire readString to the LCD to confirm that the data is being read correctly, when readString contains msg.

Then, use substring (with one argument) to extract the first (or last, whichever it returns) n characters, and send them to the LCD, instead.

Then, use substring (with two arguments) to lop off both ends.
Logged

HOLLAND
Offline Offline
Full Member
***
Karma: 0
Posts: 178
HOLLAND!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

just a quick reply the hole readstring is good and can be displayed on the lcd

just the substring doen'st seem to like me smiley-razz going to experiment a little bit more later today because i'm doing a laptop to DPF thing smiley-grin
« Last Edit: June 19, 2010, 07:21:53 am by sasaa » Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9542
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is the code I'm currently using to send string commands to an ssc-32 servo controller. It does not send back a web page. The examples are given for the strings received from GET (query_string) request. Redownload  the WString library from the below site, as there was a problem fixed ~ a week ago that made the "substring" manipulation fail. After each string manipulation "client.println();" the result to the serial port so you can see in the serial monitor if you got what was intended from that manipulation. This makes it easy to see where a problem starts to occurr. I've cleaned out some stuff and added comment's which I hope doesn't break the code.


http://www.arduino.cc/en/Tutorial/TextString

Code:
//zoomkat 6-19-10

#include <WString.h>
#include <Ethernet.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 = String(100); //string for fetching data from address
 String teststring = String(100);
 String finalstring = String(100);
 
 int ind1 = 0;
 int ind2 = 0;
 int pos = 0;
 //////////////////////

void setup(){

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

//enable serial datada print
Serial.begin(9600); }

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.append(c);
}

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

///////////////
//Serial.println(readString);  // see what was received
//readString looks like "GET /?-0p1555-1p500t1000 HTTP/1.1"

  if(readString.contains("-")) { //test for "-" in servo control sring
  readString.replace('-', '#');  //replace the "-" with "#"
  pos = readString.length(); //capture string length
  //find start of servo command string (#)
  ind1 = readString.indexOf('#');
  //capture front part of command string into "teststring"
  teststring = readString.substring(ind1, pos);
  //locate the end of the command string (a space) in "teststring"
  ind2 = teststring.indexOf(' ');
  //capturing the servo command string from readString
  finalstring = readString.substring(ind1, ind2+ind1);
  //print "finalstring" to com port and also see the final result
  Serial.println(finalstring); //print string with CR
    }
  
////////////////////////
  //GET /?Slidervalue0=1800&Submit=Sub+0 HTTP/1.1
  //for using web page sliders for servo control
  if(readString.contains("Slidervalue")) {
  ind1 = readString.indexOf('u');
  ind2 = readString.indexOf('&');
  finalstring = readString.substring(ind1+1, ind2);
  finalstring.replace('e', '#');
  finalstring.replace('=', 'p');
  Serial.println(finalstring);
  }
  ///////////////////
  
  //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="";
teststring="";
finalstring="";
  
}}}}}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

HOLLAND
Offline Offline
Full Member
***
Karma: 0
Posts: 178
HOLLAND!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

mmm crap made that was it then gonna try it again smiley-razz
Thank you all!
Logged

HOLLAND
Offline Offline
Full Member
***
Karma: 0
Posts: 178
HOLLAND!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah it works...

this is sort of what I had come up with myself but it din't work because of wstring being corrupt.

Thank you all

For intrested people here's the code.

Code:
#include <WString.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 5, 4, 3, 2); // MY LCD change to your needs
byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 150 }; // MY IP change to your needs
Server server(80);
String readString = String(100);
String finalstring = String(100);
int startpos = 10;
int endpos;


void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  lcd.begin(16, 2);
  lcd.print("setup running");
}

void loop(){
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100){
          readString.append(c);
        }
        if (c == '\n') {
          if(readString.contains("msg")){
            // Magic :P
            lcd.clear();
            // last 11 have to be taken away.
            endpos = readString.length() - 11;
            // fisrt ten have to taken away.
            finalstring = readString.substring(startpos, endpos);
            // finish the line by replacing te + for a space.
            // since /GET commands translate spaces into +'s i chage it back :P
            finalstring.replace('+', ' ');
            lcd.print(finalstring);
          }

          // Now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html><head><title>arduino msg board</title></head><body><center>");
          client.println("<form method=get>");
          client.println("<input type=text name=msg>");
          client.println("<input type=submit value=verstuur>");
          client.println("</form>");
// debug
// client.println(readString);
          client.println("</center></body></html>");
          readString="";
          client.stop();
        }
      }
    }
  }
}
// EOF *******************************

code for 16x2 LCD's
Code:
#include <WString.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 5, 4, 3, 2); // MY LCD change to your needs
byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 150 }; // MY IP change to your needs
Server server(80);
String readString = String(100);
String finalString = String(35);
String line1 = String(16);
String line2 = String(16);
int startpos = 10;
int endpos;
int lenght;


void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  lcd.begin(16, 2);
  lcd.print("setup DONE!");
}

void loop(){
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100){
          readString.append(c);
        }
        if (c == '\n') {
          if(readString.contains("msg")){
            lcd.clear();
            endpos = readString.length() - 11;
            finalString = readString.substring(startpos, endpos);
            finalString.replace('+', ' ');
                lenght = finalString.length();
                if (lenght > 16){
              line1 = finalString.substring(0, 16);
              line2 = finalString.substring(16, finalString.length());
                  lcd.setCursor(0, 0);
                  lcd.print(line1);
                  lcd.setCursor(0, 1);
                  lcd.print(line2);
                }else{
                  lcd.setCursor(0, 0);
                  lcd.print(finalString);
                }
          }

          // Now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html><head><title>arduino msg board</title></head><body><center>");
          client.println("<form method=get>");
          client.println("<input type=text SIZE=32 MAXLENGTH=32 name=msg>");
          client.println("<input type=submit value=verstuur>");
          client.println("</form>");
          client.println("</center></body></html>");
          readString="";
          client.stop();
        }
      }
    }
  }
}

only problem now is displaying special characters but if you try hard you can get them out/replaced to smiley-grin.
« Last Edit: June 19, 2010, 05:10:37 pm by sasaa » Logged

Pages: [1]   Go Up
Jump to: