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:
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:
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:
/*
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