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.
#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();
}