Two talking arduinos

Hey everyone, im a bit of a noob when it comes to programming so id love some help! :smiley:

I have successfully made my mini tcp server on my computer to input the text that i type for it to appear in my arduino serial, but now what i want to do is when my arduino receives a certain piece of text lets say "hello" for it to turn a pin on or off on the arduino.
Here is my text so far, i just need some command and programming guidance in what to type. Thanks ! <3

#include <SPI.h>
#include <Ethernet.h>
int led = 13;
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress serverIP(192,168,1,100); // IP Adress to our Server
int serverPort=8888;

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to
EthernetClient client;

void setup() {
   pinMode(led, OUTPUT); 
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting to Server ...");

  // if you get a connection to the Server
  if (client.connect(serverIP, serverPort)) {
    Serial.println("connected");//report it to the Serial
    String msg="Hello Server";//Message to be sent
    Serial.println("sending Message:"+msg);//log to serial
    client.println(msg);//send the message
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  if  (client.available()) {
    char x = client.read("x");  // This is where i think i need to write the commands, but as you can see the client.read("x") dosent work
     }
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnected");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}
    char x = client.read("x");  // This is where i think i need to write the commands, but as you can see the client.read("x") dosent work

I can't see that, because you haven't posted any proof. That line of code won't even compile, though.

What is it you are expecting that code to do? The read() method doesn't take any arguments, so supplying anything doesn't make sense.

char x = Serial.read();
if(x == 'x')
{
   // Got an 'x'
}

might be what you were trying to do.

But, why not operate on the character c that you already read and printed?

Yes in a sence, ive got it talking to my comp program thanks to your help but now i wish for two arduinos to talk to each other using ethernet shields. So far i can get them to connect to each other but thats it. Here is the code

CLIENT- Dont worry about the Led part of it

#include <SPI.h>
#include <Ethernet.h>
int led = 13;
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress serverIP(192,168,1,160); // IP Adress to our Server
int serverPort=23;

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to
EthernetClient client;

void setup() {
   pinMode(led, OUTPUT); 
   digitalWrite(led, LOW);
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting to Server ...");

  // if you get a connection to the Server
  if (client.connect(serverIP, serverPort)) {
    Serial.println("connected");//report it to the Serial
    String msg="Hello Server";//Message to be sent
    Serial.println("sending Message:"+msg);//log to serial
    client.println(msg);//send the message
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
   
  
    if(c == 'a') {
    digitalWrite(led, HIGH);
    delay(2000);
    digitalWrite(led, LOW);
     }
  }
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnected");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}

and this is the server side

#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, 160);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 255, 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();
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   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) {
      // clead out the input buffer:
      client.flush();    
      Serial.println("We have a new client");
      client.println("Hello, client!"); 
      alreadyConnected = true;
    } 

    if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
}

alot of the code is just mumbo jumbo examples but ive still managed to get them too connect, but now i want the server side to when i push a button the client turns a pin high.

Id love some help with this, As i cant program to save my life. Well im learning slowly.

Kind Regards

Lewis Joyce

now i want the server side to when i push a button the client turns a pin high.

So, somewhere on the server, you need to read the state of the switch, and compare to the previous state. If there is a change:
If if's pressed, send one letter to the client.
If it's released, send another character.

Sending something like A for pressed and a for released makes it easy on the client to detect.

Id prefer it, if the button is pressed, send letter "a" to the client. If the letter "a" is recieved by the server turn pin hight

Id prefer it, if the button is pressed, send letter "a" to the client. If the letter "a" is recieved by the server turn pin hight

So, do it that way. It won't bother me.

Simple server code that operates a pin on the arduino. You can make a simple client that sends the same get request to the server. Be aware that using a physical button connected to the client arduino will have to deal with button debounce and similar issues.

//zoomkat 12-08-12
//get submit box code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html or use a '
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

#include <SPI.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
EthernetServer server(84);; //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server text box test1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient 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 += c; 
          //Serial.print(c);
        } 

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

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Pin 5 'on' or 'off': <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change Pin 5!'>");

          client.println("</FORM>");

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}