Can't subscribe to more than one feed

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.
Locked
User avatar
thnikk
 
Posts: 23
Joined: Thu Apr 23, 2015 11:16 pm

Can't subscribe to more than one feed

Post by thnikk »

Hello!

I can't subscribe to more than one feed. I've been troubleshooting for a while and I'm completely lost. The code (and the 2subs example code) both work fine and connect ONLY IF the feeds I'm using don't exist on adafruit.IO. I'm also able to connect with one just fine. Here's my code:

Code: Select all

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "SSID"
#define WLAN_PASS       "Password"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "Username"
#define AIO_KEY         "Key"

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Store the MQTT server, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM    = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM  = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM  = AIO_KEY;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);

/****************************** Feeds ***************************************/

// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
// Setup a feed called 'onoff' for subscribing to changes.
const char COLOR_FEED[] PROGMEM = AIO_USERNAME "/feeds/color";
Adafruit_MQTT_Subscribe color = Adafruit_MQTT_Subscribe(&mqtt, COLOR_FEED);
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
Adafruit_MQTT_Subscribe photocell = Adafruit_MQTT_Subscribe(&mqtt, PHOTOCELL_FEED);

/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {

  Serial.begin(115200);
  delay(10);

  Serial.println(F("Adafruit MQTT demo"));

  // 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());

  // Setup MQTT subscription for onoff & slider feed.
  mqtt.subscribe(&color);
  mqtt.subscribe(&photocell);

}

uint32_t x=0;

void loop() {
  // 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.
  MQTT_connect();

  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    // Check if its the onoff button feed
    if (subscription == &color) {
      Serial.print(F("color: "));
      Serial.println((char *)color.lastread);
    }
    
    // check if its the slider feed
    if (subscription == &photocell) {
      Serial.print(F("photocell: "));
      Serial.println((char *)photocell.lastread);
    }
  }

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

}

// 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 = 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!");
}
I'm not sure if it's just something I'm missing or if it's a bug. Any help would be greatly appreciated!

User avatar
adafruit2
 
Posts: 22200
Joined: Fri Mar 11, 2005 7:36 pm

Re: Can't subscribe to more than one feed

Post by adafruit2 »

if you turn on verbose MQTT output whats the debug console say?

User avatar
thnikk
 
Posts: 23
Joined: Thu Apr 23, 2015 11:16 pm

Re: Can't subscribe to more than one feed

Post by thnikk »

adafruit2 wrote:if you turn on verbose MQTT output whats the debug console say?
I'm sorry, how do I do this?

User avatar
adafruit2
 
Posts: 22200
Joined: Fri Mar 11, 2005 7:36 pm

Re: Can't subscribe to more than one feed

Post by adafruit2 »


User avatar
thnikk
 
Posts: 23
Joined: Thu Apr 23, 2015 11:16 pm

Re: Can't subscribe to more than one feed

Post by thnikk »

Thanks! Here it is:

Code: Select all

Connecting to <ssid>
.........
WiFi connected
IP address: 
10.0.0.46
Added sub 0
Added sub 1
Connecting to MQTT... Connecting to: io.adafruit.com
Connect result: 0
Connection failed
Retrying MQTT connection in 5 seconds...
MQTT disconnect packet:
à [0xE0],   [0x00], 
Connection failed!
Unable to send disconnect packet
Connecting to: io.adafruit.com
Connect result: 1
SERVER GENERATING CLIENT ID
MQTT connect packet:
 <removed for username and AIO key>

sendPacket returned: 64
Read packet:	  [0x20],   [0x02],   [0x00],   [0x00], 
MQTT subscription packet:
‚ [0x82],   [0x17],   [0x00],   [0x00],   [0x00],   [0x12], t [0x74], h [0x68], 
n [0x6E], i [0x69], k [0x6B], k [0x6B], / [0x2F], f [0x66], e [0x65], e [0x65], 
d [0x64], s [0x73], / [0x2F], c [0x63], o [0x6F], l [0x6C], o [0x6F], r [0x72], 
  [0x00], 
sendPacket returned: 25
Read packet:	� [0x90],   [0x03],   [0x00],   [0x00],   [0x00], 
SubAck:	� [0x90],   [0x03],   [0x00],   [0x00],   [0x00], 
MQTT subscription packet:
‚ [0x82],   [0x1B],   [0x00],   [0x01],   [0x00],   [0x16], t [0x74], h [0x68], 
n [0x6E], i [0x69], k [0x6B], k [0x6B], / [0x2F], f [0x66], e [0x65], e [0x65], 
d [0x64], s [0x73], / [0x2F], p [0x70], h [0x68], o [0x6F], t [0x74], o [0x6F], 
c [0x63], e [0x65], l [0x6C], l [0x6C],   [0x00], 
sendPacket returned: 29
Read packet:	2 [0x32],   [0x1D],   [0x00],   [0x12], t [0x74], 
SubAck:	2 [0x32],   [0x1D],   [0x00],   [0x12], t [0x74], 
Failed to subscribe
Retrying MQTT connection in 5 seconds...
MQTT disconnect packet:
à [0xE0],   [0x00], 
sendPacket returned: 2
Connecting to: io.adafruit.com
Connect result: 1
SERVER GENERATING CLIENT ID
MQTT connect packet:
  <removed for username and AIO key>

sendPacket returned: 64
Read packet:	  [0x20],   [0x02],   [0x00],   [0x00], 
MQTT subscription packet:
‚ [0x82],   [0x17],   [0x00],   [0x02],   [0x00],   [0x12], t [0x74], h [0x68], 
n [0x6E], i [0x69], k [0x6B], k [0x6B], / [0x2F], f [0x66], e [0x65], e [0x65], 
d [0x64], s [0x73], / [0x2F], c [0x63], o [0x6F], l [0x6C], o [0x6F], r [0x72], 
  [0x00], 
sendPacket returned: 25
Read packet:	� [0x90],   [0x03],   [0x00],   [0x02],   [0x00], 
SubAck:	� [0x90],   [0x03],   [0x00],   [0x02],   [0x00], 
MQTT subscription packet:
‚ [0x82],   [0x1B],   [0x00],   [0x03],   [0x00],   [0x16], t [0x74], h [0x68], 
n [0x6E], i [0x69], k [0x6B], k [0x6B], / [0x2F], f [0x66], e [0x65], e [0x65], 
d [0x64], s [0x73], / [0x2F], p [0x70], h [0x68], o [0x6F], t [0x74], o [0x6F], 
c [0x63], e [0x65], l [0x6C], l [0x6C],   [0x00], 
sendPacket returned: 29
Read packet:	2 [0x32],   [0x1D],   [0x00],   [0x12], t [0x74], 
SubAck:	2 [0x32],   [0x1D],   [0x00],   [0x12], t [0x74], 
Failed to subscribe
Retrying MQTT connection in 5 seconds...
MQTT disconnect packet:
à [0xE0],   [0x00], 
sendPacket returned: 2

Soft WDT reset

ctx: cont 
sp: 3ffefb70 end: 3ffefd60 offset: 01b0

>>>stack>>>
3ffefd20:  00000001 3ffeeae8 3ffeecf8 402019b6  
3ffefd30:  3fffdc20 00000000 3ffeed24 402019fc  
3ffefd40:  3fffdc20 00000000 3ffeed24 40203735  
3ffefd50:  00000000 00000000 3ffeed40 40100114  
<<<stack<<<

User avatar
adafruit2
 
Posts: 22200
Joined: Fri Mar 11, 2005 7:36 pm

Re: Can't subscribe to more than one feed

Post by adafruit2 »

just updated the library, grab a fresh copy and try!
https://github.com/adafruit/Adafruit_MQTT_Library

User avatar
thnikk
 
Posts: 23
Joined: Thu Apr 23, 2015 11:16 pm

Re: Can't subscribe to more than one feed

Post by thnikk »

That fixed it, thank you so much!

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”