Pages: [1]   Go Down
Author Topic: Sketch working on Mega 2560 R3 but not on R1/2  (Read 740 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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:
#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:
  Serial.begin(9600);
    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 );
    }
    Serial.println();
   
    // 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 );
  }
  Serial.println();

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

  // 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");
    client.subscribe("inTopic");
  }
   
}
 
void loop()
{
  client.loop();
}
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nobody has a idea why it works on one and not the other smiley-confuse ?
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not exactly the reverse situation - cause mine are both Mega2560 smiley-razz
Logged

Pages: [1]   Go Up
Jump to: