Adafruit MQTT
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit MQTT

by wileye on Tue Jun 19, 2018 9:16 pm

Not sure if this is the right area, but there were other MQTT questions in here.

I have been using the Adafruit MQTT library with the Huzzah ESP8266 with a Mosquitto broker (1.4.14) running on a raspi. This then communicates with OpenHab2. This works very well and I like the Huzzah part.
I have a need for more IO at one location and plan to use an Arduino Mega with an ethernet shield. (I plan to use most of the analog and 10-15 of the discrete inputs).

I expected to use my code from the the ESP applications with minor changes (Ethernet instead of wifi library, etc.) and all the IO stuff for the inputs.
The first test I ran was just a sending a single number "25".

The Mega connects to ethernet and I can ping it fine. It hangs at the “MQTT Connect” function. The error code is "1", "connection refused, unacceptable protocol version".

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.

Thanks for any help.

The 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");

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!");

Posts: 1
Joined: Tue Jun 19, 2018 8:48 pm

Please be positive and constructive with your questions and comments.