Telnet Server to switch led on/off

As referred to the subject line, I am looking at using telnet to turn pin 13 on and off.
I will use this to connect to a relay board that will be connected to my gate keyfob to open my gate.
So my goal is to send the command "open" via telnet and to open my gate.

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,1, 44);
IPAddress gateway(192,168,1, 254);
IPAddress subnet(255, 255, 0, 0);


// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients
  server.begin();
  pinMode(13, OUTPUT);
}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();
  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clead out the input buffer:
      client.flush();    
      alreadyConnected = true;
    } 

    if (client.available() > 0) {
      // read the bytes incoming from the client: ~ I dont think I want to read the bytes, but a 
      char thisChar = client.read();

// if open command sent then : ~ What command would I need here ?
//{
//  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
//  delay(500);              // wait for a second
//  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
//}

    }
  }
}

Here is my code, if someone could show me the missing part of my code I would be very appreciated.

Richard,

I started off using Telnet and it worked using the code below. Trouble is in my case it took too many keypresses to get it to work and the family did not like it.

I have since downloaded ARDUINO MANAGER and put it on my Iphone and life is easier as the set up and interface is much easier with excellent documentation

HOWEVER I do have a problem waking up the app. I quickly detect my wireless LAN as I drive up to my house, but sometimes the Arduino Manager start up is erratic but have yet to determine why

Anyway here is my Telnet code that worked for me .....

Peter

===================================================================

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,82);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255, 255, 255, 0);

EthernetServer server(23);// Telnet defaults to port 23
boolean alreadyConnected = false; // whether or not the client was connected previously

int ledPin = 13;

String commandString;

void setup() {

pinMode(ledPin, OUTPUT); // sets the digital pin as output
Ethernet.begin(mac, ip, gateway, subnet); // initialize the ethernet device
server.begin();// start listening for clients
Serial.begin(9600);// Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only

}

Serial.print("Chat server address:");
Serial.println(Ethernet.localIP());
}

void loop() {

// wait for a new client:
EthernetClient client = server.available();

// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clear out the input buffer:
client.flush();
commandString = ""; //clear the commandString variable

server.println("--> Please type your command and hit Return...");
alreadyConnected = true;
}

while (client.available()) {

char newChar = client.read(); // read the bytes incoming from the client:

if (newChar == 0x0D) //If a 0x0D is received, a Carriage Return, then evaluate the command
{
server.print("Received this command: ");
server.println(commandString);
processCommand(commandString);
} else
{
Serial.println(newChar);
commandString += newChar;
}

}
}
}

void processCommand(String command)
{
server.print("Processing command ");
server.println(command);

if (command.indexOf("pot") > -1){
Serial.println("Pot command received");
server.print("Reading from pot: " );
server.println(analogRead(A0)); //Print the integer returned by analogRead to the server object
commandString = "";
return;
}

if (command.indexOf("ledon") > -1){
server.println("LED On command received");
digitalWrite(ledPin, HIGH); // sets the LED on
server.println("LED was turned on");
commandString = "";
return;
}

if (command.indexOf("ledoff") > -1){
Serial.println("LED Off command received");
digitalWrite(ledPin, LOW); // sets the LED off
server.println("LED was turned off");
commandString = "";
return;
}

commandString = "";
instructions();
}

void instructions()
{
server.println("I don't understand");
server.println("Please use one of these commands:");
server.println("* pot, to get a reading from the potentiomet");
server.println("* ledon, to turn on the LED");
server.println("* ledoff, to turn off the LED");
}

Right ...
So you read the byte as they come in, store each letter in the string until a return command is sent.
I'm very rusty on programming, it has been about 3 years.

You program works perfect with my relay board, and even lets me connect a few other bits to the board as I could use the reading pin A1 (pot command) to reading current room temp.

I have a number of Arduino boards around me and would love to be able to replace them with just one.

I have a board that controls my TV from IP to IR (via http, very sloppy code).

A board that controls my lights using IP to RF (Home Easy, also via http, slow and sloppy code)

A board that reads room temp of my Lounge, Kitchen, Master bedroom, and 2nd bedroom, and sends it back via IP (via telnet, fast and just works!)

And I Wanted to be able to open my gate via IP too(this is why I asked for help)

I use iRule software on my iPad, iPhone and also my other half's Android.

Anyone fancy helping me code all the bits together ???

As referred to the subject line, I am looking at using telnet to turn pin 13 on and off.

You probably need to use another pin for on/off. With the w5100 shield on an arduino, pin 13 is a timing pin.