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.
#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.