🎁📬❄️📦 Holiday Shipping Deadlines are approaching! International customers and domestic ground - get your orders in 12/9/2020 📦❄️📬🎁
0

Can't connect to MQTT server
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Can't connect to MQTT server

by CodeNamiti on Fri Nov 06, 2020 10:14 am

I have been trying to get the output of serial monitor to show the reading IO Adafruit but I can't even connect to IO Adafruit MQTT server. I try reinstalling the libraries and modifying the code. It just say the connection failed repeatedly


Code: Select all | TOGGLE FULL SIZE
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

//Wifi parameters
#define ssid  "XXXXXXXX"   
#define pass   "XXXXXXXXXXX"
WiFiClient client;

// AdaFruit IO
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 8883
#define AIO_USERNAME "XXXXXXX"
#define AIO_KEY "XXXXXXX"

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);


#define ledPin D6
#define ldrPin D1
#define moisturePin A0            // moisture sensor pin
#define motorPin D0
float moisturePercentage;              //moisture reading

//Set up the feed you're publishing to
Adafruit_MQTT_Publish Moisture = Adafruit_MQTT_Publish(&mqtt,AIO_USERNAME "/feeds/Moisture");

//Set up the feed you're subscribing to
 Adafruit_MQTT_Subscribe Light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED");
 Adafruit_MQTT_Subscribe waterPump = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/waterPump");

void MQTT_connect();

void setup()
{
  Serial.begin(115200);
  delay(10);
  mqtt.subscribe(&Light);
  mqtt.subscribe(&waterPump);
  pinMode(ldrPin, INPUT);
  pinMode(motorPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(motorPin, LOW); // keep motor off initally
 
  Serial.println("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");              // print ... till not connected
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

}
 
void loop()

{
  MQTT_connect();

   if (mqtt.connected())
  {
    Serial.print("MQTT connected!");
    return;
  }

 int ldrStatus = analogRead(ldrPin);

    if (ldrStatus <= 200) {
   
    digitalWrite(ledPin, HIGH);
   
    Serial.print("Its DARK, Turn on the LED : ");
   
    Serial.println(ldrStatus);
   
    }
    else {
   
    digitalWrite(ledPin, LOW);
   
    Serial.print("Its BRIGHT, Turn off the LED : ");
   
    Serial.println(ldrStatus);
   
    }

  moisturePercentage = ( 100.00 - ( (analogRead(moisturePin) / 1023.00) * 100.00 ) );

 
    Serial.print("Soil Moisture is ");
    Serial.print(moisturePercentage);
    Serial.println("%");
   
   

if (moisturePercentage < 10) {
  digitalWrite(motorPin, HIGH);         // tun on motor
}
if (moisturePercentage > 10 && moisturePercentage < 30) {
  digitalWrite(motorPin, HIGH);        //turn on motor pump
}
if (moisturePercentage > 30) {
  digitalWrite(motorPin, LOW);          // turn off mottor
}


if (! Moisture.publish(moisturePercentage))
       {                     
         delay(5000);   
       }
Adafruit_MQTT_Subscribe * subscription;
while ((subscription = mqtt.readSubscription(5000)))
     {
   
   if (subscription == &Light)
     {
      //Print the new value to the serial monitor
      Serial.println((char*) Light.lastread);
     
   if (!strcmp((char*) Light.lastread, "OFF"))
      {
        digitalWrite(ledPin, HIGH);
    }
    if (!strcmp((char*) Light.lastread, "ON"))
      {
        digitalWrite(ledPin, LOW);
    }
 }
   
   if (subscription == &waterPump)
     {
      //Print the new value to the serial monitor
      Serial.println((char*) waterPump.lastread);
     
   if (!strcmp((char*) waterPump.lastread, "OFF"))
      {
        digitalWrite(motorPin, HIGH);
      }
   if (!strcmp((char*) waterPump.lastread, "ON"))
      {
        digitalWrite(motorPin, LOW);
      }
   
     }
   }
     
}
void MQTT_connect()
{
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected())
     {
      return;
     }
     
      Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
      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!");
}

CodeNamiti
 
Posts: 9
Joined: Fri Nov 06, 2020 4:47 am

Re: Can't connect to MQTT server

by brubell on Fri Nov 06, 2020 10:28 am

Hi,

What board are you using?
Could you please post the output from the serial monitor?

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

Re: Can't connect to MQTT server

by CodeNamiti on Sat Nov 07, 2020 3:47 am

I am using ESP8266 12E to connect to IO Adafruit. I also download Adafruit_IO_Arduino, Adafruit_MQTT_Library, ArduinoMqttClient, WiFi101 and WiFiEsp library too. Am I missing a library or something?

Code: Select all | TOGGLE FULL SIZE
.......
WiFi connected
Connecting to MQTT... Retrying MQTT connection in 5 seconds...
Retrying MQTT connection in 5 seconds...
Retrying MQTT connection in 5 seconds...

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Soft WDT reset

>>>stack>>>

ctx: cont
sp: 3ffffdd0 end: 3fffffc0 offset: 01a0
3fffff70:  00000001 3ffee728 3ffee928 40201192 
3fffff80:  3fffdad0 00000000 3ffee970 4020122f 
3fffff90:  40208cd0 f232a8c0 feefeffe 3ffee9b0 
3fffffa0:  3fffdad0 00000000 3ffee970 402063a4 
3fffffb0:  feefeffe feefeffe 3ffe84e4 40100c29 
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
Connecting to
WiFi connected

CodeNamiti
 
Posts: 9
Joined: Fri Nov 06, 2020 4:47 am

Re: Can't connect to MQTT server

by brubell on Mon Nov 09, 2020 11:42 am

It's difficult for me to debug this type of stack error.

Could you please install the ESP Exception Decoder
https://github.com/me-no-dev/EspExceptionDecoder

Then,
    Open a sketch and build it.
    Upload the sketch and monitor the Serial port for Exceptions
    When you get an Exception, open Tools > ESP Exception Decoder menu item. This will open a new window.
    Paste the stack trace into the window's top pane and the result will show in the bottom.
    Copy the result from the bottom window into the forum for me to analyze it.

Thanks

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

Re: Can't connect to MQTT server

by CodeNamiti on Tue Nov 10, 2020 10:18 pm

Thank Brubell! I manage to connect to MQTT server by one of the samples they show and connect to MQTT server!

CodeNamiti
 
Posts: 9
Joined: Fri Nov 06, 2020 4:47 am

Please be positive and constructive with your questions and comments.


cron