I'm facing a similar issue trying to make arduino mega 2560 rev3 work with arduino ethernet shield with PoE module rev3.
i'm able to ping the Ethernet shield from my PC, and i can see in wireshark that an acknowledment is also received from Ethernet shield.
Next i started up the Webserver example and tried querying the ethernet shield ip address from browser, however after some time the page request fails. i put some Serial prints in arduino code, but what i could figure out was that somehow if(client) statement is always false.
then i wrote a small C# program to connect to server , and i could see that socket connection is successfully established and i'm able to send data as well (without any errors or exceptions) but on receiving end if(client) statement is always false as if arduino never received a connection request.
interesting thing i noticed in wireshark that i could see packet with my 2 bytes (he Hex 68 65) of data sent from PC and an acknowledge packet as well from the ethernet shield.
packet sent from PC
570 732.708302 192.168.1.40 192.168.1.77 TCP 56 20892 > cddbp-alt [PSH, ACK] Seq=1 Ack=1 Win=65520 Len=2
Ack received from Arduino Ethernet shield
576 732.910612 192.168.1.77 192.168.1.40 TCP 60 cddbp-alt > 20892 [ACK] Seq=1 Ack=3 Win=2046 Len=0
sending 2 bytes after initial connection success from Cthe conn but somehow in code if(client) statement is always false.
there are few topics online available on how to do it but unfortunately they didn;t work for me.
1st approach: as mentioned on arduino help docs and also on sparkfun (where i purchased ethernet shield) it is mentioned that for mega SPI pins are 50, 51, 52 and 53. pin 53 is hardware SS and should be set to OUTPUT otherwise SPI won't work.
i made following connections
Pin-Mega |
Pin-Ethernet shield |
4 |
4 |
10 |
10 |
11 |
50 |
12 |
51 |
13 |
52 |
Reset |
Reset |
IOREF |
IOREF |
AREF |
AREF |
on mega pin 53 was left open.
network settings and code
IP: 192.168.1.77
i didn't specify any mask or gateway.
#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, 0x0D, 0x58, 0x6E };
IPAddress ip(192, 168, 1, 77);
IPAddress gateway(192, 168, 1, 1);
IPAddress mask(255, 255, 255, 0);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
int port = 8880;
EthernetServer server = EthernetServer(port);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(53, OUTPUT);
/// disabeling the SD card
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);//, gateway, mask);
server.begin();
Serial.print("server is at ");
Serial.print(Ethernet.localIP()); Serial.print(":"); Serial.println(port);
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if(client)
{
Serial.println("client connected");
Serial.println(client.read());
Serial.println(client.read());
while(client.connected())
{
client.write("hi");
client.println("hi, i am ethernet shield connected on mega 2560.");
// give the web browser time to receive the data
delay(1000);
Serial.println("data sent to client.");
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
C# program code
public void SendReceiveData()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(IPAddress.Parse("192.168.1.77"), 8880);
if (socket.Connected)
{
Console.WriteLine("Client Connected.");
//while (!Stop)
{
Thread.Sleep(2000);
var data = Encoding.Default.GetBytes("hello");
socket.Send(new byte[] { data[0], data[1] });
while (!Stop)
{
Thread.Sleep(2000);
var dataAvailable = socket.Available;
if (dataAvailable != 0)
{
byte[] buffer = new byte[1024];
socket.Receive(buffer, 0, dataAvailable, SocketFlags.None);
Console.WriteLine("{0} bytes received.", dataAvailable);
Console.WriteLine(Encoding.Default.GetString(buffer, 0, dataAvailable));
}
}
}
socket.Disconnect(false);
socket.Close();
}
}
2nd approach as SurferTim says that ethernet shield is compatible with mega and no bending pins are required,
i hooked up following connections and tried the same program as mentioned above.
ping test was successful, but the problem with the program and sending receiving data was not working.
Pin-Mega |
Pin-Ethernet shield |
4 |
4 |
10 |
10 |
11 |
11 |
12 |
12 |
13 |
13 |
Reset |
Reset |
IOREF |
IOREF |
AREF |
AREF |
ICSP header |
ICSP header |
its a very long post but i wanted to add all the information i had. already wasted 2 days on this thing and i'm starting to hate Ethernet shield now.
any help will be much appreciated.