Go Down

Topic: Sketch working on Mega 2560 R3 but not on R1/2 (Read 1 time) previous topic - next topic



my Sketch is working on the Mega 2560 R3 but not on an older one.
I don't know if its an R1 or R2 cause there is no print on the PCB.
I also switched the ethernet shield to make sure its not the shield.

The MQTT Server does not receive the "hello world" from the older Mega 2560.

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PubSubClient.h>

*                         Connections                         *
#define ONE_WIRE_BUS 5 // oneWire pin is D5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

*                      Ethernet Settings                      *
// Ethernet Card Mac Address
byte mac[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
// IPv4 address
byte ip[] = {192, 168, 0, 132};
// Subnet mask
byte subnet[] = {255, 255, 255, 0};
// Default gateway
byte gateway[] = {192, 168, 0, 1};
// MQTT Server
byte server[] = { 192, 168, 0, 1 };
// Preferred DNS sever
// byte dns[] = {192, 168, 0, 1};

*                           MQTT                              *
void callback(char* topic, byte* payload, unsigned int length)


// Fire up PubSub client
PubSubClient client(server, 1883, callback);

*                           Setup                             *
void setup()
  byte i;
  byte dsAddress[8];
  // Open serial communications and wait for port to open:
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

  Serial.println( "Searching for DS18B20..." );
  oneWire.reset_search(); // Start the search with the first device
  if( !oneWire.search(dsAddress) )
    Serial.println( "none found. Using default MAC address." );
  } else {
    Serial.println( "success. Setting MAC address:" );
    Serial.print( " DS18B20 ROM  =" );
    for( i = 0; i < 8; i++)
      Serial.write(' ');
      Serial.print( dsAddress[i], HEX );
    // Offset array to skip DS18B20 family code, and skip mac[0]
    mac[1] = dsAddress[3];
    mac[2] = dsAddress[4];
    mac[3] = dsAddress[5];
    mac[4] = dsAddress[6];
    mac[5] = dsAddress[7];

  Serial.print( " Ethernet MAC =" );
  for( i = 0; i < 6; i++ )
    Serial.write( ' ' );
    Serial.print( mac[i], HEX );

  Ethernet.begin(mac, ip, subnet, gateway);
  Serial.print(" IPv4 address: ");

  // Start up the Dallas Temperature library
  sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement

   if (client.connect("arduinoClient", "testuser", "testpass")) {
    client.publish("outTopic","hello world");

void loop()


Nobody has a idea why it works on one and not the other :~ ?


my Sketch is working on the Mega 2560 R3 but not on an older one.

I have no idea of what you are trying to do, the code looks pretty alien. I am only going on the quoted line. The only reason I can think of for this to happen is that there are several differences in the pins between Megas and what I assume is a Uno. This particularly applies to analogue and signal from the clock. I was obliged to move from Uno to Mega2560 and, after a bad start, all the problem lay in calling the pins, and the worst I had to was to put in a flying lead from the clock.  I guess going the other way could be just as bumpy.


No Uno.
As i wrote both are Megas - just different revisions.

What looks alien maybe is that i use a DS18B20 to generate a unique MAC address
on all my Ethernet Arduinos.
This way you always have unique MAC address's without taking care of them.


You probably don't want to know this, but I have just the reverse situation trying to get data from a GPS module.

See "Sketch works on ATMega328 but not on ATMega2560" in this forum from yesterday


Not exactly the reverse situation - cause mine are both Mega2560 :P

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131