[SOLVED] Mosquito MQTT and Arduino w/ Ethernet Shield Adafruit MQTT Library

I would appreciate any help or ideas anyone can give:
My system is: Openhab2 and Mosquitto 1.4.14 running on a Raspberry PI. I have 3 Adafruit ESP8266 Huzzah’s each sending data via MQTT. These use the adafruit MQTT library and all work fine. Additionally, I have various Insteon devices connected via a PLM/USB Port, These work fine also.

I now want to include an Arduino Mega with an Ethernet Shield. I expected to be able to use my ESP code with some revisions. Cannot get it to work.

The Mega connects to ethernet and I can ping it fine. It hangs at the “MQTT Connect” function. It does not return an error code. There is blank space.
I have used MQTT Spy to watch Mosquitto. From what I can tell, I don’t see anything suspicious. I can inject messages for the tag and they show up in Openhab.

This program is supposed to send the number “25” to my openhab dashboard to test the link. No luck.

Thanks for any help.

The Arduino Program:

//Mega Adafruit MQTT_Home_rev 0
// Update: 6/09/18

#include <SPI.h>
#include <Ethernet.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>

// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xBE, 0xBE, 0xEF, 0xFE, 0xfD };
//0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED original
//0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED other

/************************* MQTT Broker Setup *********************************/

#define mqtt_server      "xxxxxxxxxx"
#define mqtt_serveport   1883
#define mqtt_username    "xxxxxxx"
#define mqtt_password    "xxxxx"

EthernetClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, mqtt_server, mqtt_serveport, mqtt_username, mqtt_password);

// Setup a feed called 'MegaA00' for publishing.
Adafruit_MQTT_Publish MegaA00 = Adafruit_MQTT_Publish(&mqtt, "openhab/in/MegaA00/state");

//Bug workaround
void MQTT_connect();

void setup() {
  Serial.println(F("Mega Adafrut MQTT rev 0"));
  while (Ethernet.begin(mac) != 1)
    Serial.println("Error getting IP address via DHCP, trying again...");

    Serial.println("IP address: "); Serial.println(Ethernet.localIP());
  Serial.println("before MQTT connect");
  Serial.println("after MQTT connect");

void loop() {
  float x=25;

  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.


  // Publish to MQTT
  Serial.print(F("\nSending value "));
  if (! MegaA00.publish(x++)) {
  } else {

  // ping the server to keep the mqtt connection alive
  if(! mqtt.ping()) {

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.

void MQTT_connect() {
  int8_t ret;
  Serial.println("mqtt connect routine");
  // Stop if already connected.
  if (mqtt.connected()) {

  Serial.print("Connecting to MQTT... ");

  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println("Retrying MQTT connection in 5 seconds...");
       delay(5000);  // wait 5 seconds
  Serial.println("MQTT Connected!");

Use the pubsubclient library instead

2X void MQTT_connect();
and one is empty

void MQTT_connect();

I use pubsubclient, here is is a basic example:

Thanks for the input. After some additional testing i get an error “Connection Refused, unacceptable protocol version”.
I will probably take the advice on using the PubSub and will look to adapt my code. It will be next week before I get a chance to try this.

Been quite a while, but I got back to this. I changed over to pubsubclient and everything works well.