0

ESP32: mqtt.readSubscription(5000) always returns 0
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.

ESP32: mqtt.readSubscription(5000) always returns 0

by avidNewb on Fri Jul 12, 2019 1:48 pm

Background:
I will post my code below. I am using an ESP32 and would like to connect Adafruit IO and Google Assistant. As a proof of concept I'm using the example code below. I have configured a feed exactly as per the example.

Problem:
MQTT connects and reports connected. However, "mqtt.readSubscription(5000)" always returns 0 and I cannot read the feed. It never enters the loop that a successful read would allow.

Example link:
https://iotdesignpro.com/projects/google-assistant-controlled-led-using-ESP32-and-adafruit-io

Code (with PII removed):
Code: Select all | TOGGLE FULL SIZE
#include <WiFi.h>

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_Client.h"

#define WLAN_SSID       "XXXXXX"

#define WLAN_PASS       "XXXXXX"

#define AIO_SERVER      "io.adafruit.com"

#define AIO_SERVERPORT  1883                 

#define AIO_USERNAME    "XXXXXXX"

#define AIO_KEY         "XXXXXXXXXXXXXXXXXXXXXXXXXX"

int output=2;

WiFiClient client;     // Create an ESP8266 WiFiClient class to connect to the MQTT server.

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);        // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.

Adafruit_MQTT_Subscribe LED_Control = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED_Control");

void MQTT_connect();

void setup() {

  Serial.begin(115200);

  delay(10);

pinMode(2,OUTPUT);

 // 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) {

    delay(500);

    Serial.print(".");

  }

  Serial.println();

 Serial.println("WiFi connected");

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

  mqtt.subscribe(&LED_Control);

}

uint32_t x=0;

void loop() {

   MQTT_connect();

Adafruit_MQTT_Subscribe *subscription;

  while ((subscription = mqtt.readSubscription(5000))) {

    if (subscription == &LED_Control) {

      Serial.print(F("Got: "));

      Serial.println((char *)LED_Control.lastread);

       if (!strcmp((char*) LED_Control.lastread, "ON"))

      {

        digitalWrite(2, HIGH);

      }

      else

      {

        digitalWrite(2, 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(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!");

}

avidNewb
 
Posts: 1
Joined: Fri Jul 12, 2019 1:41 pm

Please be positive and constructive with your questions and comments.