Pages: [1]   Go Down
Author Topic: Arduino over Ethernet - setup issue  (Read 758 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello All,
Thank you in advance for your help. Here's my issue:
I am attempting to have two Arduinos (MEGA2560) communicate with each other via Ethernet. The idea is that each Arduino will transmit its data to the other Arduino as long as both are running.
I've set one Arduino up as a server and the other as a client as such:

Server:
Code:
void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
 
  //Configure all pins
  pinMode(22, OUTPUT);
 
  // listen for incoming clients
  client = server.available();
  while(client==false){
   client = server.available();
  }
}

Client:
Code:
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(client.connect()){
    Serial.println("connected");
  }
  else{
     Serial.println("connection failed");
  }
 
}

It should be noted that this code is adapted from the Arduino examples found on the Arduino website, and any variables used above are declared globally if not otherwise stated.

My exact problem is that the connection will establish, but after some iterations (approx. 2) the client will reset "arbitrarily". I know that this is the case because in Serial I will see "connecting... /n connected" After this occurs, both parties do not receive significant data from the other. In fact, the server no longer believes itself to have a client.

As the problem is certainly in the Client system, here is its entire code:
Code:
/*
  Web client
 
 */

#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[] = {  0x90, 0xA2, 0xDA, 0x00, 0x29, 0x2A };
byte ip[] = { 140,228,164,68 };
byte server[] = { 140,228,164,67 };

int i=0;
int serverAnalogVal[]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

// 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(client.connect()){
    Serial.println("connected");
  }
  else{
     Serial.println("connection failed");
  }
 
}

void loop()
{
  delay(1000);
  if (client.connected()) {
    Serial.println("Client Values");
    for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int c=analogRead(analogChannel);
            client.print(c);
            client.print('a');
            Serial.println(c);
    }
    client.println('\n');
    client.println();
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
 
  // if there are incoming bytes available
  // from the server, read them and print them:
  if(client.connected()){
    while (client.available()) {
      char c = client.read();
   
      if(!(c == 'a' || c == '/n')){
      //Serial.println(serverAnalogVal[i]);
        serverAnalogVal[i]=(serverAnalogVal[i]*10)+(int(c)-48);
      }
      if(c=='a'){
      i++;
      }
    }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    client.stop();
  }

    Serial.println("Server Values");
    for(int i=0;i<6;i++){
      Serial.print(i);
      Serial.print(':');
      Serial.println(serverAnalogVal[i]);
    }
 
  }
}

You'll see that it is supposed to send its analog values to the server and display them on serial, tell the server it's done, and then receive the analog values of the server, output them to serial, and repeat this process. Once again, the problem is that the connection will establish, but after some iterations (approx. 2) the client will reset "arbitrarily". Any solutions/ potential fixes are appreciated.
Thanks,
~Bob
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To clarify what I think the issue may be (and to move myself back up the queue):

I believe that either the Ethernet or serial communication is accidentally resetting my client Arduino. I know that the serial can reset the board (this is, of course, the normal programming method), so I suspect that it is the culprit. Unfortunately, although my final program does not use serial communication, I AM using it for debugging purposes. If this is indeed the problem, I need to:
1. Hopefully identify WHAT in my code is causing the problem
2. Stop the problem from occurring so I can continue development.

Thanks again,
~Bob
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9627
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I know that the serial can reset the board (this is, of course, the normal programming method)

This is true when the pc serial port is opened and is a function of the arduino IDE and the serial chip on the arduino. As far as I know, serial code run on the arduino does not cause a reset.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: