Sketch uploads on Arduino Ethernet R3 but NOT on Arduino MEGA 2560!

Hi there. I have a sketch that I cannot compile correctly to Arduino Mega 2560 but it compiles, uploads & works with no problem on Arduino Ethernet R3.

I have all 5 files attached in this post.

I get the error:

c:/program files (x86)/arduino-1.0.2/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr6/crtm2560.o: In function `__vector_default':
(.vectors+0x24): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_9' defined in .text section in c:/program files (x86)/arduino-1.0.2/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr6/crtm2560.o

Please tell me if you can upload it to your Arduino MEGA and what is the problem in my sketch. I am trying to make it compile for over 5 days with no luck... :frowning:
Thanks a lot.

Home_Server.rar (6.9 KB)

Please can somebody help me with this?

Post your code. Attaching the sketch in a .rar format limits how many users will even view it. To avoid incurring the wrath of Nick Gammon, insure to enclose the code in the code brackets.

The code is consisted by 5 tabs and I can onlyupload just 4 files in a post. That's why I used the rar archive.
I am posting it again now.
the fifth file is at the post below.

Home_Server.ino (1.64 KB)

homepage.ino (5.8 KB)

login.ino (3.61 KB)

time.ino (4.78 KB)

and finally the 5th file of the sketch
Can you easily compile it to MEGA 2560?

wol.ino (1.47 KB)

This is how you should post it to get the most out of the forum. This is your home.ino file.

/* 
 Web server sketch for IDE v1.0.1 and w5100/w5200
 Posted October 2012 by SurferTim
 */

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Utility\Socket.h>
#include "Dns.h"
#include <Time.h>
int dmnhd=0; // domain hider value (automatically changed)
byte mac[] = { 
  0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD }; //physical mac address
IPAddress ip(192, 168, 10, 150);  // ip in lan
byte gateway[] = {   192, 168, 10, 254 }; // internet access via router
byte subnet[] = {   255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //8085 forwarded on parnasidos house
static byte g_TargetMacAddress[] = {0x00,0x24,0x1D,0x7D,0x54,0x25}; //for wol NaThAN-PC


IPAddress clientIP;
IPAddress loggedIP;
//char loggedip[]="";
char user[ ] = "admin"; // YOUR Username for Login
char pwd[ ] = "1234"; // YOUR Password for Login
int loggedin = 0;
//////////////////////

void setup()
{
  Serial.begin(9600);

  // disable w5100 while setting up SD
  // uncomment next 5 lines if using a microSD card

    pinMode(9,OUTPUT);
   digitalWrite(9,LOW);
  // Serial.print("Starting SD..");
  // if(!SD.begin(4)) Serial.println("failed");
  // else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{
   
if (loggedin==0){
 
  login();
}
else if (loggedin==1){
  if(loggedIP==clientIP){

 digitalWrite(9, HIGH);    // set pin 4 high
 homepage(); 
  }
  else{
    loggedin=0;
   loop(); 
  }
}
}


/////////////////////////
int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

If you want to check your code first for any exclamation marks. If you use three in a row like this, the code upload could fail. It causes the Mega bootloader to enter a monitor/debugging mode.

Serial.println("It works!!!");

The code fails to compile! The error is given in the first post. I have selected correct board and COMport.
I repeat again to be clear that the sketch compils & uploads to Arduino Ethernet R3 but NOT compiling for Arduino MEGA 2560.

Dear SurferTim I cannot post all the code because it exceedes the maximum length of 9500 characters..

SurferTim:
If you want to check your code first for any exclamation marks. If you use three in a row like this, the code upload could fail. It causes the Mega bootloader to enter a monitor/debugging mode.

Serial.println("It works!!!");

Dear SurferTim thanks a lot!
I had a post command with three explanation marks and I removed them and now my code compiles and uploads with no problem!!!
Thanks a lot!

I had a post command with three explanation marks and I removed them and now my code compiles and uploads with no problem!!!

So, you just repeated the same mistake. I guess some people never learn.

PaulS:

I had a post command with three explanation marks and I removed them and now my code compiles and uploads with no problem!!!

So, you just repeated the same mistake. I guess some people never learn.

True!!! ]:slight_smile:

Good thing. I tried compiling your code, and without the client.remoteIP() function added to the ethernet library, it wouldn't compile for me. It threw a bunch of errors.

That client.remoteIP() function sounds like my addition to the ethernet library. I use it for a primitive firewall as it appears to do in your code also.

SurferTim:
Good thing. I tried compiling your code, and without the client.remoteIP() function added to the ethernet library, it wouldn't compile for me. It threw a bunch of errors.

That client.remoteIP() function sounds like my addition to the ethernet library. I use it for a primitive firewall as it appears to do in your code also.

I have examined your library changes in files and done something like you did.
I do this fo security but its not secure when someone is trying to access your page from the same LAN network.If you have successfully logged in and someone from the same LAN tries to access the page the Arduino will see the other request having the same IP (because I use my DYNDNS domain - Netowork's Public Internet IP address) and let him see the page as logged in.

Another thing I need to ask you is why my SD is not recognised in MEGA and is working correctly in Arduino Ethernet.
I cant even read or write to the SD card. But on Arduino Ethernet the same code workgs great. Why is that?
I have examined pinouts on Arduino Ethernet & MEGA 2560 and seem to use tha same pins.

/*
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */
 
#include <SD.h>

File myFile;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  

  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
	// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
	// nothing happens after setup
}

Before you start the SD card, disable the w5100 SPI. That caused my Mega to fail the SD.begin call. In setup, add this line:

   pinMode(10, OUTPUT);
   // add this
   digitalWrite(10, HIGH);

edit: And they are not the same. The Mega uses D50-D52 instead of D11-D13 for the SPI, but the new ethernet/SD shields don't use those pins anyway. They use the ICSP pins for the SPI data lines.

Thanks once again SurferTim!