Implement Existing Code with Ethernet Shield

I am building a web controlled tank that utilizes the Arduino Ethernet Controller. Prior to using the Ethernet controller, I would drive my netbook around on the tank. (Original Code posted below). Is there a way to easily implement this code to make it work on a web server? The original code uses incomingByte to listen for data sent over serial. Can the ethernet shield just send serial data to the arduino?

Original Code:

/*
  */
 int ledPin13 = 13; //
 int ledPin12 = 12; //
 int ledPin11 = 11; //
 int ledPin10 = 10; //
 int ledPin9 = 9; // 
 int ledPin8 = 8; // 
 
 
 int incomingByte;      // a variable to read incoming serial data into
 void setup() {
   
   Serial.begin(9600); // initialize serial communication
  
   pinMode(ledPin13, OUTPUT);  
   pinMode(ledPin12, OUTPUT);  
   pinMode(ledPin11, OUTPUT);  
   pinMode(ledPin10, OUTPUT);  
 }
 void loop() {
   // see if there's incoming serial data:
   if (Serial.available() > 0) {
     
     incomingByte = Serial.read(); // read the oldest byte in the serial buffer
//Preform the code to switch on or off the leds
    if (incomingByte == '0') {
    digitalWrite(ledPin13, HIGH); 
  } 
   if (incomingByte == '1') {
   digitalWrite(ledPin13, LOW); 
 }
 
     if (incomingByte == '2') {
    digitalWrite(ledPin12, HIGH); 
} 
   if (incomingByte == '3') {
   digitalWrite(ledPin12, LOW);
 }
 
   } 
   if (incomingByte == '4') {
   digitalWrite(ledPin11, HIGH); 
 }
 
 
    
   if (incomingByte == '5') {
   digitalWrite(ledPin11, LOW); 
 }


   if (incomingByte == '7') {
digitalWrite(ledPin12, LOW); 
digitalWrite(ledPin11, LOW);

 
 }
 
 
    if (incomingByte == '6') {
digitalWrite(ledPin12, HIGH); 
digitalWrite(ledPin11, HIGH); 

 
 }


    if (incomingByte == '8') {
   digitalWrite(ledPin10, HIGH); 

 }
 
     if (incomingByte == '9') {
   digitalWrite(ledPin10, LOW);

 }
 
      if (incomingByte == 'l') {
   digitalWrite(ledPin13, HIGH); 
   digitalWrite(ledPin10, HIGH); 

 }
 
       if (incomingByte == 'o') {
   digitalWrite(ledPin13, LOW); 
   digitalWrite(ledPin10, LOW);
   digitalWrite(ledPin8, LOW); 

 }
 
        if (incomingByte == 'b') {
   digitalWrite(ledPin8, HIGH); 

 }



   }

Sorry but I've just got to ask.

Comments at the top of your sources indicate Motors, Lights and Ground effects yet everywhere else the comment indicate (often conflictingly as well) various color LED's. Which is correct?

I apologize for the discrepancies in the code. I seem to have done a poor job at commenting it. The pin assignments don't matter as much as being able to control them via Ethernet. Just know that I am using pins 13,12,11,10,9 and 8. However, i realize that the Ethernet shield may conflict with these IO pins. I think my shield uses IO pins 11,10,6,5,3.

Thanks

The original code uses incomingByte to listen for data sent over serial.

Send by what? Ascii data or binary data?

What code are you running on the Arduino now, with the ethernet shield installed?

Sorry but I'm sorry too.

Wrong comments are WORSE than no comments at all as it wastes time by misdirecting the reader.

No offense meant.

Agreed. I do apologize. i have removed all comments. I recently bought the ethernet shield. Before I would send serial data to the arduino from the netbook through the COM port monitor.

Presumably, you'll be having the Arduino act as a server, responding to GET requests, but actually moving.

Before, you were receiving ascii data from the serial port, using Serial.read(). Now, you'll be receiving ascii data from the client, using client.read().

Does this mean that I will have to setup the Ethernet Shield to act as a server and a client? If so how would I go about doing that? Im very new to the Ethernet shield, although I have familiarized myself with the basics, I believe this is just out of my expertise.

Does this mean that I will have to setup the Ethernet Shield to act as a server and a client?

It should be either a client (makes requests of a server) or a server (services requests by clients), not both.

Picture your robot as www.robot.com. Your PC will make a request for information/action/etc. The server, the robot, will process the request (in the form of a GET request with additional data), and react accordingly.

The GET request might look like "GET /go?dir=forward&speed=200". The job of the robot is to parse the command (go), and the arguments (dir=forward, speed=200), and take the necessary action.

So the shield will act as a server. Here is where i struggle. How do I get the shield to take the appropriate action and send serial data to the Arduino. Are there any examples you can point me toward?

How do I get the shield to take the appropriate action

First, you have to collect the client data into an appropriate collection - either a char array or a String. Then, you parse the character data to figure out what to do.

and send serial data to the Arduino.

The shield does not connect to the serial pins, so it can't send serial data to the Arduino.

Im not asking for you to hand me the code on a a silver platter, but I would like to learn how to accomplish this. Like I said, im pretty new to this stuff, so any additional help is greatly appreciated.

There are examples provided with the ethernet library, to have the Arduino with ethernet shield act as server or as client.

Knowing that you want the Arduino to act as a server, upload the server sample, and get that to work. Once you can print the client request to the Serial Monitor, it will be trivial to change what is sent to the Arduino by the browser (because you type that part) and to change what the Arduino does with the received data (which is exactly what your original code did with Serial.read() data (now client.read()).

You might want to check out the below post that have info similar to what you want to do.

http://www.lynxmotion.net/viewtopic.php?f=20&t=6343

Thanks! I re-wrote the code, but I am still receiving errors. In theory, if I point my browser to http://192.168.1.102:84/1 the shield should receive a 1 and take the appropriate action. Am I missing something? (Ignore comments in the code, I copy, paste and move a lot around so dont let them confuse you)

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


/*
  Physical Pixel
  
  created 2006
  by David A. Mellis
  modified 14 Apr 2009
  by Tom Igoe and Scott Fitzgerald
  http://www.arduino.cc/en/Tutorial/PhysicalPixel
  
  CODE CHANGED AND UPDATED FOR PERSONAL USE BY JEREMIAH M DUKE 7-19-2009
  YOU ARE FREE TO USE THIS SOFTWARE AS LONG AS YOU KEEP ALL OF THE ABOVE INTACT.
  I AM NOT THE ORIGINAL CREATOR OF THIS SOFTWARE.
  
  */
 int ledPin13 = 13; // the pin that the RED LED is attached to
 int ledPin12 = 12; // the pin that the YELLOW LED is attached to
 int ledPin11 = 11; // the pin that the GREEN LED is attached to
 int ledPin10 = 10; // the pin that the GREEN LED is attached to
 int ledPin9 = 9; // the pin that the GREEN LED is attached to
 int ledPin8 = 8; // the pin that the GREEN LED is attached to
 int incomingByte;      // a variable to read incoming serial data into
 
 
 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
 
 
 
 void setup() {
   
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
   
   Serial.begin(9600); // initialize serial communication
  
   pinMode(ledPin13, OUTPUT);  // initialize the green LED pin as an output
   pinMode(ledPin12, OUTPUT);  // initialize the red LED pin as an output
   pinMode(ledPin11, OUTPUT);  // initialize the red LED pin as an output
   pinMode(ledPin10, OUTPUT);  // initialize the red LED pin as an output
 }
 void loop() {
   
   
// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

Serial.println("test");
Serial.println(incomingByte);

   // see if there's incoming serial data:
   if (client.available() > 0) {
     
     incomingByte = client.read(); // read the oldest byte in the serial buffer
//Preform the code to switch on or off the leds
    if (incomingByte == '0') {
    digitalWrite(ledPin13, HIGH); //If the serial data is 0 turn red LED on
  } 
   if (incomingByte == '1') {
   digitalWrite(ledPin13, LOW); //If the serial data is 1 turn red LED off
 }
 
     if (incomingByte == '2') {
    digitalWrite(ledPin12, HIGH); //If the serial data is 2 turn green LED on
} 
   if (incomingByte == '3') {
   digitalWrite(ledPin12, LOW); //If the serial data is 3 turn green LED off
 }
 
   } 
   if (incomingByte == '4') {
   digitalWrite(ledPin11, HIGH); //If the serial data is 3 turn green LED off
 }
 
 
    
   if (incomingByte == '5') {
   digitalWrite(ledPin11, LOW); //If the serial data is 3 turn green LED off
 }


   if (incomingByte == '7') {
digitalWrite(ledPin12, LOW); //If the serial data is 3 turn green LED off
digitalWrite(ledPin11, LOW); //If the serial data is 3 turn green LED off

 
 }
 
 
    if (incomingByte == '6') {
digitalWrite(ledPin12, HIGH); //If the serial data is 3 turn green LED off
digitalWrite(ledPin11, HIGH); //If the serial data is 3 turn green LED off

 
 }


    if (incomingByte == '8') {
   digitalWrite(ledPin10, HIGH); //If the serial data is 3 turn green LED off

 }
 
     if (incomingByte == '9') {
   digitalWrite(ledPin10, LOW); //If the serial data is 3 turn green LED off

 }
 
      if (incomingByte == 'l') {
   digitalWrite(ledPin13, HIGH); //If the serial data is 3 turn green LED off
   digitalWrite(ledPin10, HIGH); //If the serial data is 3 turn green LED off

 }
 
       if (incomingByte == 'o') {
   digitalWrite(ledPin13, LOW); //If the serial data is 3 turn green LED off
   digitalWrite(ledPin10, LOW); //If the serial data is 3 turn green LED off
   digitalWrite(ledPin8, LOW); //If the serial data is 3 turn green LED off

 }
 
        if (incomingByte == 'b') {
   digitalWrite(ledPin8, HIGH); //If the serial data is 3 turn green LED off

 }



   }
   
    client.println("HTTP/1.1 204 Zoomkat");
  client.println();
  client.println();
  delay(1);
  //stopping client
client.stop();

/////////////////////
//clearing string for next read

 {
 }}}}

Thanks! I re-wrote the code, but I am still receiving errors.

And those errors are?

In theory, if I point my browser to http://192.168.1.102:84/1 the shield should receive a 1

Are you really using port 84? What does the shield receive? Why not print out the value(s) so you don't need to guess?

The serial monitor returns a 0, then it returns 69 for every time i refresh the server.
Serial.println(incomingByte);
How can I program it so that the shield just gets the value after the /"___".

How can I program it so that the shield just gets the value after the /"___".

You can't. The shield gets the full command. It is up to you to look at the command, and only save the parts that are of interest to you.

Yes, but I cant get very far when the shield only returns 69. The value doesnt change. Every time I reload the page, the serial monitor returns the value 69. If the value was different, that would be fine but http://192.168.1.102:84/1 is the same as http://192.168.1.102:84/2 they both return 69. I am using client.read. Am i using it properly?

Have you looked up the character that that ASCII value represents? 69, in decimal, represents an E. 69 in hex represents an i.

if (client.available()) {
char c = client.read();

Serial.println("test");
Serial.println(incomingByte);

Why are you throwing away the first byte? What value is there in printing a variable that you have not just modified?

Get rid if the excess white space. Put each { and } on a separate line. Indent the code in between the braces. This makes it easy to see where each block of code begins and ends. Use some functions, too.

Right now, it is hard to tell, but it looks like you are only getting the first two characters from each client connection, instead of all of them. Hard to tell, though, with the code like it is.