Go Down

Topic: Problem with Comparing String (Read 682 times) previous topic - next topic

Billwaa

Hi, I am having problem with comparing String for some odd reason. The codes works with Char, but just not String. I am not very experienced with programming, so hopefully someone can help me out here.

Code: [Select]


#include "WiFly.h"
#include "Credentials.h"

//Command Library
#include <Servo.h>

/*
May 27th, 2011
Yu Hin Hau

Script to establish wifi connection between Laptop computer and Space Hopper, using Telnet to prompt for input command and arduino to execute it
Please make sure the commanding computer is connected to HopperNet and is able to acess Telnet

Library taken from experimental WiFly library alpha 2 from sparkfun,  http://forum.sparkfun.com/viewtopic.php?f=32&t=25216

*/




//define variables
Server server(80);
boolean firstRun = false;
char input = -1;
boolean command_recieved = false;

//Array Command of 50 Character
char command[50];
String command_line;

//Testing Function variable
boolean led = false;
Servo servo1;
double servo1_position = 0;
boolean servo1_update = false;

void setup() {

 //Testing Function
 pinMode(8,OUTPUT);
 servo1.attach(9);
 
 
 //Setup
 Serial.begin(9600);
 
 Serial.println("Lehigh Space Hopper");
 Serial.println("");  

Serial.print("initializing WiFly...........");
 WiFly.begin();
 Serial.println("done");

Serial.print("connecting to HopperNet...........");
 if (!WiFly.join("HopperNet", "HopperNet")) {
   while (1) {
   }
 }
 Serial.println("done");
 
    Serial.print("starting server...........");
  server.begin();
  Serial.println("done");
 
 Serial.println("");
 
 Serial.print("IP: ");
 Serial.println(WiFly.ip());
 Serial.println("");
 Serial.println("1) Enable Telnet by going to Control Panel / Program / Windows Feature...");
 Serial.println("2) Start Command Prompt...");
 Serial.println("3) Type 'Telnet' and press enter...");
 Serial.print("4) Type 'open "); Serial.print(WiFly.ip()); Serial.println(" 80'...");
 Serial.println("5) Transmit command to Space Hopper...");
 Serial.println("6) Press Control + ] to exit...");
 Serial.println("");
 
 
}


void loop() {
 
 Client client = server.available();
 
 
 if (client) {
   if (!firstRun) {
     Serial.println("Controller Connected");
     client.println("---------------------------------------------------------------");
     client.println("---------------------------------------------------------------");
     client.println(" Lehigh University");
     client.println(" Space Hopper Control Interface");
     client.println(" status - online");
     client.println("---------------------------------------------------------------");
     client.println("---------------------------------------------------------------");
     client.println("");
     client.println(" Command List:");
     client.println(" LED_ON \t LED_OFF \t SERVO_1_0 \t SERVO_1_180 ");
     client.println("");
     client.println("");
     firstRun = true;
   }
   
   //Starting Array Command Input
   int i = 0;
   
   while(true)
     {
   input = client.read();
   
   if (input != -1)
   {
   if (input == '\n')
       break;
   else
   {
     command[i] = input;
     i++;
   }
   }
     }
     
     command_line = "";
     
     
     for(int d = 0; d < i; d++)
       command_line += command[d];
     
     
      Serial.println(command_line);
      client.println("\t echo: "+command_line);
     
             
      ///////////////////////////////////////
     
     
     
     ////////////////
   
   
 
/* if(input != -1)
 {
   client.println(" ~");
   Serial.println(input);
 }*/
 
 //Testing Function/////////////////////////////////////////////////////////////////////////
 if (command_line.equals("LED_ON") == 1)
 {
 led = true;
 client.println("\t LED = On");
 client.println("");
 command_recieved = true;
 }
 
 if(command_line.equals("LED_OFF") == 1)
 {
   led = false;
   client.println("\t LED = Off");
   client.println("");
   command_recieved = true;
 }
 
 if(led)
 digitalWrite(8, HIGH);
 else
 digitalWrite(8, LOW);
 /////////////////////////////////////////////////////////////////////////////////////////
 
 //Servo/////////////////////////////////////////////////
 
   if(command_line.equals("SERVO_1_0") == 1)
 {
   servo1_position = 0;
   client.println("\t Servo Position = 0");
   client.println("");
   servo1_update = true;
   command_recieved = true;
   
 }
 
     if(command_line.equals("SERVO_1_180") == 1)
 {
   servo1_position = 180;
   client.println("\t Servo Position = 180");
   client.println("");
   servo1_update = true;
   command_recieved = true;
 }
 
 if (servo1_update)
 {
  servo1.write(servo1_position);  
  servo1_update = false;
  }
  ////////////////////////////////////////////////////////////////////
 
 if(!command_recieved)
       client.println("\t\t invalid command...");
       else
       client.println("\t\t ~");
       
       client.println("");
 
 command_recieved = false;
 
 
 
 
 
 
 
 }
 
 
}


So long story short, I am a MechE working on a space project with my school. I know some programming, in Java, but not so good in C++. With the Arduino, I am just typing and hoping to get it working. Basically, I created a script to connect Arduino board to the Wifi network and communicate with the PC using Telnet. This is important because we want to be able to do most heavy processing in the PC and also attach joysticks to it, using it to control the spacecraft prototype. It will be a lot easier if we can just wire it, but unfortunately, that is not an option.

So, since I am very very new to Arduino and also network and stuffs, I am not sure if I understand things correctly. I got the assumption that through Telnet and client.read(), I can only get 1 character transfered at a time, which is not good enough for our purpose.  

So I changed the original script from the Arduino reacting to a single char command (which worked), to it reacting to a String command that was assembled from an array of char received.

Somehow, the Arduino was not able to correctly compare the String for some reason. It must be something stupid I did, since it is such a simple function that is screwing up, but I am not seeing it after trying various method of comparing String. So I am hoping for some help, thanks.

zoomkat

You might try the string functions like below.

Code: [Select]

    if(readString.indexOf("woohoo") >=0) {
      Serial.println("I found woohoo!");
    }
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Billwaa


You might try the string functions like below.

Code: [Select]

    if(readString.indexOf("woohoo") >=0) {
      Serial.println("I found woohoo!");
    }



thanks, that solved the problem! :D

now I have another problem with String...

I am trying extract an integer value from the input command. normally in java, I could do substring, then do Integer.parseString or something like that.

So in Arduino, I looked it up, and people say use atoi(str);

But it gave me an error when I try to do that.

Code: [Select]

if(command_line.indexOf("SERVO_1") >= 0)
  {
    int loc = command_line.indexOf("@");
   
    Serial.println(command_line.substring(loc+1));
       
        char * temp = command_line.substring(loc+1);
       
    servo1_position = atoi(temp);
   
    client.println("\t Servo Position = "+servo1_position);
    client.println("");
    servo1_update = true;
    command_recieved = true;
  }


Code: [Select]

WirelessNetworkServo__Array_Command_.cpp: In function 'void loop()':
WirelessNetworkServo__Array_Command_:199: error: cannot convert 'String' to 'char*' in initialization
WirelessNetworkServo__Array_Command_:203: error: invalid operands of types 'const char [20]' and 'double' to binary 'operator+'


what is 'char*' ? and why don't it work? thanks.

AWOL

Quote
what is 'char*' ? and why don't it work? thanks.

atoi works on C strings, which are char arrays terminatedwith a zero character.
A way of representing a char array is with a char pointer, written "char*".
A String is an object, not a simple array, so if you want to  use "atoi", you chave to provide a char array and convert your String into it, then call "atoi".
The String documentation explains all of this.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

zoomkat

Below is a way to get a string number into an integer number to control a servo. This uses the serial monitor for testing servos.

Code: [Select]

// zoomkat 11-27-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readstring; //string to be captured from serial port
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
 Serial.begin(9600);
 myservo.attach(7);  //the pin for the servo control
 Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

 while (Serial.available()) {
   delay(10);  
   if (Serial.available() >0) {
     char c = Serial.read();  //gets one byte from serial buffer
     readstring += c; //makes readstring from the single bytes
   }
 }

 if (readstring.length() >0) {
   Serial.println(readstring);  //so you can see the captured string
   char carray[readstring.length() + 1]; //determine size of the array
   readstring.toCharArray(carray, sizeof(carray)); //put readstringinto an array
   int n = atoi(carray); //convert the array into an Integer
   myservo.writeMicroseconds(n); // for microseconds
   //myservo.write(n); //for degees 0-180
   readstring="";
 }
}
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

liudr

I'm using totally C library to do this without the string stuff.

sscanf scans input string and finds numbers.

Say if you type in "80 50 30"

Then sscanf("80 50 30", "%d%d%d), &x, &y, &z); will capture the numbers and transfers them to variables x, y and z.

http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/


BTW, your serial.print commands are too long. The strings you print reside in SRAM and can run you out of SRAM. Don't do it. Use short strings or PROGMEM. You're not running this code on your main PC.

Billwaa

thanks a lot, guys. I got a lot done today :D

liudr: sorry, I am not used to working with micro-controllers. Do you mean that it is bad for me to do all the outputs to the Serial because I will run out of RAM? Should I worry about how long each String is or so I worry more about how many words in the duration of the program I am outputing to Serial? thx

liudr


thanks a lot, guys. I got a lot done today :D

liudr: sorry, I am not used to working with micro-controllers. Do you mean that it is bad for me to do all the outputs to the Serial because I will run out of RAM? Should I worry about how long each String is or so I worry more about how many words in the duration of the program I am outputing to Serial? thx


If you  use an UNO, there's only 2048 bytes of ram. The strings all use this ram so yes you need to worry about those long lines. Either cut them short or use PROGMEM.

Go Up