Pages: [1]   Go Down
Author Topic: arduino and eathernet communication  (Read 412 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello thanks for looking.
My project is on arduiono device and my project is to send and receive voice  between two arduino device using internet.
Components i got are two of arduino boards (MEGA 2560) and two of eithernet shields.

Connection:
As m very new to arduinos and I dont know much about them so to get started what initially m doing is to send a bit from "A" end which has arduino connected to ethernet shield, and ethernet shield  connected to arduino at "B" end . Those two Ethernet sheilds are connected with each other by a "twisted cabel"(which is same as internet cable but twisted so it cn receive and send the data)

Problums.
1- Now m not sure how to make it work ? I hv connected the USB cable from Computer A to end A and from Computer B to End B.  its quite funny but sorry i dont know which Software i hv to use .
 
2- How Ethernet shield reads data from arduino at "A" end and how B end converts that data.

I will lock forward for ur reply . Pls do help me out.
Regards
Mick.
Logged

China
Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please follow the steps below. Hope it helps.

1. Download the Ethernet library here (http://arduino.cc/en/Reference/Ethernet).
2. Unzip it to the folder <arduino location>\libraries\
3. Open the sketch WebClient. Change the ip and server to be 192,168,1,177  192,168,1,178 respectively. Change the mac address to be 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE7 (see the code below)
4. Compile and upload the code to Auduino A.
5. Open the sketch WebServer, and hange the ip to be 192,168,1,178 . Change the mac address to be 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE8  (see the code below)
6. Compile and upload the code to Auduino B.
7. Connect Arduino A and B to PC with USB.
8. Open the Serial Monitor on the PC.
9. Reset Arduino B first, then reset Arduino A.
10. Expected Result: You can find the output from the Serial Monitor.


WebClient.pde
Code:
#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:
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE7 };
byte ip[] = { 192,168,1,177 };
byte server[] = { 192,168,1,178 };

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client(server, 80);

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // start the serial library:
  Serial.begin(9600);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect()) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  }
  else {
    // kf 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 the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

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

WebServer.pde
Code:
#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:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE8 };
byte ip[] = { 192,168,1, 178 };

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("<br />");
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Logged

Pages: [1]   Go Up
Jump to: