0

Connecting to MQTT... Connection failed
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Connecting to MQTT... Connection failed

by ohararp2 on Thu Jan 10, 2019 1:17 pm

Adafruit,

I am a pretty seasoned Arduino user and have a new application I am developing. I am trying to add a new device to my feed (using well proven code on an Adafruit Huzzah ESP8266). Unfortunately, I am having trouble Connecting to MQTT with the following code. All libraries are up to date namely Adafruit MQTT (0.20.3). I am wondering if my demo account may be blocking new connections/feeds. I have another dashboard and devices that are able to connect without an issue (much older MQTT library though). Any help is appreciated.

Code: Select all | TOGGLE FULL SIZE
Connecting to OHARARP
........................
WiFi connected
IP address:
192.168.87.135
Connecting to MQTT... Connection failed
Retrying MQTT connection in 5 seconds...


Code: Select all | TOGGLE FULL SIZE
//https://www.kevindarrah.com/wiki/index.php?title=TrigBoard
//----------------------------------------------------
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
//----------------------------------------------------
// PINS!!!
//----------------------------------------------------
#define LEDpin 0 // this is the on-board LED - write this LOW to turn on
#define DONEpin 15  // write this PIN HIGH to kill your own power
#define EXTWAKEpin 16 // check this pin right when you boot up to see if the external switch woke you up - will be LOW if externally woken
//----------------------------------------------------
//FUNCTIONS
//----------------------------------------------------
void WIFI_connect();
void MQTT_connect();
void verifyFingerprint();
//---------------- Variable Setup ------------------------------------

//globals for credentials
char trigBoardName[40];
char triggerMessage[100];
boolean resetFlag = false;

char batteryBuf[5] = "";// used to convert float to a char array
boolean externalWake = false;// to figure out who woke up the board
float batteryVoltage = 0;
float batteryThreshold = 3.4;

//------------ Create an ESP8266 WiFiClient---------------------------
WiFiClientSecure client;

//------------------------ WiFi Access Point Setup------------------------
#define WLAN_SSID       "xxx"
#define WLAN_PASS       "xxx"

//------------------------Adafruit.io Setup --------------------------------
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  8883                   // 8883 for MQTTS
#define AIO_USERNAME    "ohararp2"
#define AIO_KEY         "big key here"
                         
//------------------------------ Feeds --------------------------------------*/
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// io.adafruit.com SHA1 fingerprint
const char* fingerprint = "AD 4B 64 B3 67 40 B5 FC 0E 51 9B BD 25 E9 7F 88 B6 2A A3 5B";

// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
//Adafruit_MQTT_Publish garageOne = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/ledred");
//Adafruit_MQTT_Publish garageMsg = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/garage.garageterminal");

/*************************** Error Reporting *********************************/
Adafruit_MQTT_Subscribe errors = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/errors");

void MQTT_connect();
void verifyFingerprint();
//------------------------------------------------------------------------------
// Setup Routine
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(115200);//debug
  checkTrigger();

  mqtt.subscribe(&errors);
}

//------------------------------------------------------------------------------
// Main Loop
//------------------------------------------------------------------------------
void loop() {// we're done, so kill power.  All code should run in void setup()

  if (externalWake || batteryVoltage < batteryThreshold) {// only if external trigger was fired or the battery is low, send a push
      WIFI_connect();

      //verifyFingerprint();
     
      // Check MQTT is connected (Connection/Reconnect Auto Handled)
      // Only Run MQTT_connect() in Main Loop and not in Setup!!!
      MQTT_connect();
      Serial.println("MQTT Action Now"); //Only runs if External Trigger
      test.publish(1);
      //garageMsg.publish("garage1"); // Publish Data
      //garageOne.publish(1); // Publish Data
      externalWake = false;
  }

  // Go back to sleep
  for (int i = 0; i < 10; i++) {
    digitalWrite(DONEpin, HIGH);
    delay(100);
    digitalWrite(DONEpin, LOW);
    delay(100);
  }

  // or if you hold the wake button, you'll end up here
  //longPress();
} //-----Main Loop End

//------------------------------------------------------------------------------
// Sub Functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Wifi Connect/Reconnect
//------------------------------------------------------------------------------
void WIFI_connect() {

  // Stop if already connected.
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Wifi Connected");
    return;
  }

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(LEDpin, !digitalRead(LEDpin));
    delay(200);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());
}

//------------------------------------------------------------------------------
// MQTT Connect
//------------------------------------------------------------------------------
// 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;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

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

  uint8_t retries = 10;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}
//------------------------------------------------------------------------------
// MQTT Connect
//------------------------------------------------------------------------------
void verifyFingerprint() {

  const char* host = AIO_SERVER;

  Serial.print("Connecting to ");
  Serial.println(host);

  if (! client.connect(host, AIO_SERVERPORT)) {
    Serial.println("Connection failed. Halting execution.");
    while(1);
  }

  if (client.verify(fingerprint, host)) {
    Serial.println("Connection secure.");
  } else {
    Serial.println("Connection insecure! Halting execution.");
    while(1);
  }

}

//------------------------------------------------------------------------------
// Check Trigger
//------------------------------------------------------------------------------

void checkTrigger() {
  pinMode(EXTWAKEpin, INPUT);// need to immediately check this pin

  if (digitalRead(EXTWAKEpin) == LOW) {//flag if HIGH, then the external wake circuit woke the board up
    externalWake = true;
  }
  pinMode(DONEpin, OUTPUT);
  batteryVoltage = (4 - 3.5)/(712 - 621) ;
  batteryVoltage = analogRead(A0) * batteryVoltage + (4 - batteryVoltage * 712);
 
  pinMode(LEDpin, OUTPUT);// LED on Board
  digitalWrite(LEDpin, LOW); //LOW to turn on

  Serial.println("");
  if (externalWake)
    Serial.println("External Wake");
  else {
    Serial.println("Timer Wake");
}
  Serial.print("Battery Voltage = ");
  Serial.println(batteryVoltage);
  dtostrf(batteryVoltage, 3, 1, batteryBuf);//converts battery voltage float to char array
}

ohararp2
 
Posts: 1
Joined: Fri Feb 05, 2016 10:06 am

Re: Connecting to MQTT... Connection failed

by brubell on Mon Jan 14, 2019 12:03 pm

Are you able to connect using the mqtt_esp8266 example from Adafruit MQTT?

=> https://github.com/adafruit/Adafruit_MQ ... sp8266.ino

brubell
 
Posts: 471
Joined: Fri Jul 17, 2015 10:33 pm

Re: Connecting to MQTT... Connection failed

by ohararpS on Mon Jan 14, 2019 12:08 pm

Brubell, no luck with this either. I was able to get an unsecure connection on port 1883 working though.

ohararpS
 
Posts: 1
Joined: Thu Jan 10, 2019 8:52 pm

Please be positive and constructive with your questions and comments.