Hello! I've been trying to use my Mega 2560 R3 with a W5100 Ethernet Shield. The shield says that it is MEGA compatible directly on the board. This is for a home security project. I'm using the Mega to sense the door sensors and, when open, change a relay connected to my alarm control box. I'm using the web server to return some JSON about the door sensors.
When I use pins 6, 7, and 8, everything works fine. The web server is solid. But, I eventually need to add more inputs (and that's why I'm using a Mega), so I'm trying to use the higher numbered pins starting at 22. Moving the relay actuator from 6 to 22 works fine. However, when I move an input sensor from 7 to 23, the web server starts hanging on the 4th HTTP request from my browser.
For kicks, I tried using pin 40 instead of 23, but it didn't help. I haven't changed any of the real logic of the web server-- most of it is lifted directly from the web server example. I've tried printing status messages out to the serial monitor and found that the web server gets stuck in a loop where it seems like the currentLineIsBlank variable never gets set to true. The shield continues to respond to pings when the web server hangs. I'm using the Mac OS X ping command line utility. The RX light on the shield blinks every 6-10 seconds.
Looking around the web, I've seen articles about staying away from pins 4, 10-13, and 50-53. I've disable the microSD SPI and explicitly enabled the ethernet SPI. The part where this is reproducible on the 4th request is really strange.
Is there some kind of incompatibility with the Mega's higher level pins and the ethernet shield? Can anyone venture a guess as to why the lower pins are rock-solid? That makes me think that the ethernet shield and Mega should be compatible.
Thanks for any help you might be able to provide!
-Anthony
Here's my 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[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x03 };
IPAddress ip(192,168,123,39); //<<< ENTER YOUR IP ADDRESS HERE
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
String jsonSpacer = ", ";
// Declare pins
int zone6Actuator = 22;
int frontDoor = 40; // When this is set to pin 7 everything works.
int doorToGarage = 8;
int dins[] = {frontDoor, doorToGarage};
void setup()
{
// Diable microSD SPI.
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
// Select ethernet w5100 SPI slave.
digitalWrite(10, LOW);
pinMode(zone6Actuator, OUTPUT);
for (int i = 0; i < sizeof(dins)/sizeof(int); i++) {
pinMode(dins[i], INPUT);
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
// Listen for a
if ((digitalRead(frontDoor) == LOW) || (digitalRead(doorToGarage) == LOW)) { // LOW => loop with control box is open => ALARM state
digitalWrite(zone6Actuator, HIGH); // HIGH energizes the relay
} else { // HIGH => loop with control box is closed => NON-ALARM state
digitalWrite(zone6Actuator, LOW); // LOW returns the relay to its normal state
}
// listen for incoming clients
EthernetClient 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: application/json");
client.println();
String status = "{";
boolean first = 1;
for (int i = 0; i < sizeof(dins)/sizeof(int); i++) {
int circuitOpen = digitalRead(dins[i]);
if (first) {
status = status + "'ad" + dins[i] + "':" + circuitOpen;
first = 0;
} else {
status = status + jsonSpacer + "'ad" + dins[i] + "':" + circuitOpen;
}
}
status = status + "}";
client.println(status);
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();
}
}