Arduino Mega interface with Ethernet shield 05

I just start learning arduino. I got a mega and one ethernet shield which says it is compatible with mega. I put them together and it didn't work. Pin 53 and pin 10 are set to be high output. Does anyone know where it goes wrong?

I put them together and it didn't work.

It? What is "it"? The Mega didn't work? The shield didn't work? The code on the Mega didn't work?

What code?

Does anyone know where it goes wrong?

Many, many possibilities. Far too many to guess at, given such meager clues.

I used this example
#include <Ethernet.h>
#include <SPI.h>

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac = {
0x90, 0xA2, 0xDA, 0x00, 0x1C, 0x63 };
// assign an IP address for the controller:
byte ip = {
…, …, …, … };
// Assign the address of the server you want to connect to:
byte server = {
173,203,98,29 }; // pachube.com

// initialize the library instance:
Client client(server, 80);

long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop

void setup() {
pinMode(53, OUTPUT);
// pinMode(10, OUTPUT);
// digitalWrite(10, HIGH);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);

// start the ethernet connection and serial port:
Ethernet.begin(mac, ip);
Serial.begin(9600);
// give the ethernet module time to boot up:
delay(1000);
}

void loop() {
// read the analog sensor:
int sensorReading = analogRead(0);

// if there’s incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}

// if there’s no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println(“disconnecting.”);
client.stop();
}

// if you’re not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > 10000)) {
sendData(sensorReading);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData(int thisData) {
// if there’s a successful connection:
if (client.connect()) {
Serial.println(“connecting…”);
// send the HTTP PUT request.
// fill in your feed address here:
client.print(“PUT /api/12661.csv HTTP/1.1\n”);
client.print(“Host: www.pachube.com\n”);
// fill in your Pachube API key here:
client.print(“X-PachubeApiKey: a39e254aa5ac64f2958e1…\n”);
client.print("Content-Length: ");

// calculate the length of the sensor reading in bytes:
int thisLength = getLength(thisData);
client.println(thisLength, DEC);

// last pieces of the HTTP PUT request:
client.print(“Content-Type: text/csv\n”);
client.println(“Connection: close\n”);

// here’s the actual content of the PUT request:
client.println(thisData, DEC);

// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn’t make a connection:
Serial.println(“connection failed”);
}
}

// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:

int getLength(int someValue) {
// there’s at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you’re at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}

Mega connects to ethernet shield through ICSP. The serial monitor said ‘connection faild’.

Problem solved. Because the gateway's ip address is not x.x.x.01 and the subnet is 255.255.254.0. I have to initial the ethernet setting by using ethernet.begin(mac,ip,gateway,subnet).

Hey leo,

can you expand on your solution?

Wiznet chipset? Old mega? Did you have to connect the the ethernet shield to pins 50-53 or just plug it in to the mega?

I had the same problem with the default gateway address of 192.168.xxx.001 in the code. Check your gateway address and if it does not end in .1 (001) then you need to 'over ride' the default gateway address. No hardware changes - just software.