Exception (9) when sending JSON to MQTT broker

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
smitthhyy
 
Posts: 4
Joined: Sat Nov 24, 2012 7:57 am

Exception (9) when sending JSON to MQTT broker

Post by smitthhyy »

Building a weather sensor using the Adafruit Feather HUZZAH ESP8266 is producing an Exception (9) error that has me stuck.

Parts included here are:
1. The output as seen using the serial monitor
2. The arduino script used to generate the output and error
3. The debug additions to Adafruit_MQTT_Client.cpp (add Serial.print in strategic places)
4. The debug additions to Adafruit_MQTT.cpp (add Serial.print in strategic places)
5. Version information

Any help and/or suggestions will be greatly appreciated

Code: Select all

Connecting to WiFi-AP
........
WiFi connected
IP address: 
192.168.0.13
Sending to MQTT broker:sizeof(strObj): 240
strObj: {"BMPTempValue":27.70,"BMPpressureValue":988.28,"BMPaltitudeValue":210.00,"DHT22tempValueC":27.00,"DHT22tempValueF":80.60,"DHT22heatIndexValueC":27.45,"DHT22heatIndexValueF":81.41,"DHT22humidityValue":50.50,"BatteryValue":86.27}
Adafruit_MQTT_Client::connected()
Connecting to MQTT... Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len)
    len: 30
    len is > 0
    client->connected() = true
    Sending: 30
MQTT Connected!
connected so wait half sec then publish strObj to weatherOne

Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos)
    Topic: trevor/feeds/weatherOne
    Data: {"BMPTempValue":27.70,"BMPpressureValue":988.28,"BMPaltitudeValue":210.00,"DHT22tempValueC":27.00,"DHT22tempValueF":80.60,"DHT22heatIndexValueC":27.45,"DHT22heatIndexValueF":81.41,"DHT22humidityValue":50.50,"BatteryValue":86.27}

Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos)
    Topic: trevor/feeds/weatherOne
    bLen: 228
    QOS: 0
    uint16_t len = publishPacket(buffer, topic, data, bLen, qos); len = 256
    DOING - if (!sendPacket(buffer, len)) return false
Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len)
    len: 256
    len is > 0

Exception (9):
epc1=0x402030a9 epc2=0x00000000 epc3=0x00000000 excvaddr=0x35342e37 depc=0x00000000

ctx: cont 
sp: 3ffef830 end: 3ffefaa0 offset: 01a0

>>>stack>>>
3ffef9d0:  3ffe8afc 3ffeea48 00000100 402030a6  
3ffef9e0:  3ffee88e 3ffe8678 3ffeea48 402033c8  
3ffef9f0:  00000100 3ffee88e 3ffeea48 3ffee86c  
3ffefa00:  00000100 3ffee88e 3ffeea48 40202cf9  
3ffefa10:  00000000 3ffe8678 3ffe834c 402033c8  
3ffefa20:  00000003 0000001b 3ffeea48 00000000  
3ffefa30:  3ffe8678 3ffe834c 3ffeea48 40202dc9  
3ffefa40:  3ffee86c 3ffe834c 3ffeea48 4020339d  
3ffefa50:  3ffe8618 3ffeea74 3ffeea48 3ffeea74  
3ffefa60:  3fffdad0 3ffe834c 3ffeea48 40202f2a  
3ffefa70:  3fffdad0 3ffe834c 3ffeea48 40201de0  
3ffefa80:  3fffdad0 00000000 3ffeea6c 40203778  
3ffefa90:  feefeffe feefeffe 3ffeea80 40100718  
<<<stack<<<

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

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
The arduino script that produces the above is:

Code: Select all

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

#define WLAN_SSID       "---your ssid---"
#define WLAN_PASS       "---your passphrase---"
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "---your username---"
#define AIO_KEY         "---your key---"

WiFiClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish weatherOne = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/weatherOne");

char strObj[240] = "{\"BMPTempValue\":27.70,\"BMPpressureValue\":988.28,\"BMPaltitudeValue\":210.00,\"DHT22tempValueC\":27.00,\"DHT22tempValueF\":80.60,\"DHT22heatIndexValueC\":27.45,\"DHT22heatIndexValueF\":81.41,\"DHT22humidityValue\":50.50,\"BatteryValue\":86.27}";

void setup() {
  Serial.begin(115200);
  delay(10);

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

void loop() {
  Serial.print("Sending to MQTT broker:");
  Serial.print("sizeof(strObj): ");Serial.println(sizeof(strObj));
  Serial.print("strObj: ");Serial.println(strObj);
  MQTT_connect();
  Serial.print("connected so wait half sec then ");
  delay(500);
  Serial.println("publish strObj to weatherOne");
  bool publishResult = weatherOne.publish(strObj);
  Serial.print("Publish results is: ");
  if (publishResult) {
    Serial.println("OK");
  } else {
    Serial.println("Failed!");
  }
  delay(2000);
}

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!");
}
The following modifications made to the Adafruit_MQTT_Client.cpp to show the debug output as above.
Note the changes are addition of Serial.print and Serial.println lines only!

Code: Select all

..... no changes to original library code above this function .....

bool Adafruit_MQTT_Client::connected() {
  Serial.println("Adafruit_MQTT_Client::connected()");
  // Return true if connected, false if not connected.
  return client->connected();
}

uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
                                          int16_t timeout)
..... no change to this function ......

bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len) {
    Serial.println("Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len)");
  uint16_t ret = 0;
    Serial.print("    len: "); Serial.println(len);
  while (len > 0) {
    Serial.println("    len is > 0");
    Serial.print("    DOING - if (client->connected()) returns: ");
    if (client->connected()) {
      // send 250 bytes at most at a time, can adjust this later based on Client
      Serial.println("true");
      uint16_t sendlen = min(len, 250);
      Serial.print("    Sending: "); Serial.println(sendlen);
      ret = client->write(buffer, sendlen);
      DEBUG_PRINT(F("Client sendPacket returned: ")); DEBUG_PRINTLN(ret);
      len -= ret;

      if (ret != sendlen) {
    DEBUG_PRINTLN("Failed to send packet.");
    return false;
      }
    } else {
      Serial.println("false");
      DEBUG_PRINTLN(F("Connection failed!"));
      return false;
    }
  }
  return true;
}
..... end of Adafruit_MQTT_Client.cpp
The following modifications made to the Adafruit_MQTT.cpp to show the debug output as above.
Note the changes are addition of Serial.print and Serial.println lines only!

Code: Select all

..... no changes to original library code above this function .....

bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
  Serial.println();
  Serial.println("Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos)");
  Serial.print("    Topic: "); Serial.println(topic);
  Serial.print("    Data: "); Serial.println(data);
    return publish(topic, (uint8_t*)(data), strlen(data), qos);
}

bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos) {
  Serial.println();
  Serial.println("Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos)");
  Serial.print("    Topic: "); Serial.println(topic);
  Serial.print("    bLen: "); Serial.println(bLen);
  Serial.print("    QOS: "); Serial.println(qos);
  // Construct and send publish packet.
  Serial.print("    uint16_t len = publishPacket(buffer, topic, data, bLen, qos); len = ");
  uint16_t len = publishPacket(buffer, topic, data, bLen, qos);
  Serial.println(len);
  Serial.println("    DOING - if (!sendPacket(buffer, len)) return false");
  if (!sendPacket(buffer, len))
    return false;
  Serial.println("    sendPacket returned true");
  // If QOS level is high enough verify the response packet.
  if (qos > 0) {
    len = readFullPacket(buffer, MAXBUFFERSIZE, PUBLISH_TIMEOUT_MS);
    DEBUG_PRINT(F("Publish QOS1+ reply:\t"));
    DEBUG_PRINTBUFFER(buffer, len);
    if (len != 4)
      return false;
    if ((buffer[0] >> 4) != MQTT_CTRL_PUBACK)
      return false;
    uint16_t packnum = buffer[2];
    packnum <<= 8;
    packnum |= buffer[3];

    // we increment the packet_id_counter right after publishing so inc here too to match
    packnum++;
    if (packnum != packet_id_counter)
      return false;
  }

  return true;
}
..... no changes to original library code below this point .....
Version information:
Arduino v1.8.1 on Windows 10 64bit
ESP8266WiFi Built-In by Ivan Grokhotkov Version 1.0.0
Adafruit MQTT library by Adafruit Version 0.17.0

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Exception (9) when sending JSON to MQTT broker

Post by adafruit_support_mike »

[moved to the adafruit.io forum]

User avatar
smitthhyy
 
Posts: 4
Joined: Sat Nov 24, 2012 7:57 am

Re: Exception (9) when sending JSON to MQTT broker

Post by smitthhyy »

Hi @adafruit_support_mike,
This happens when trying to send to other brokers too. It's a problem with the Adafruit MQTT library, not io.adafruit.com service.

User avatar
jwcooper
 
Posts: 1004
Joined: Tue May 01, 2012 9:08 pm

Re: Exception (9) when sending JSON to MQTT broker

Post by jwcooper »

smitthhyy wrote:Hi @adafruit_support_mike,
This happens when trying to send to other brokers too. It's a problem with the Adafruit MQTT library, not io.adafruit.com service.
We primarily test the Adafruit MQTT library against IO. If you find issues with that library with other brokers, we do accept pull requests. There are far too many brokers to test them all, and many of them have weird edge case issues or custom feature sets.

Thanks!

User avatar
smitthhyy
 
Posts: 4
Joined: Sat Nov 24, 2012 7:57 am

Re: Exception (9) when sending JSON to MQTT broker

Post by smitthhyy »

Solved.
Replace the Adafruit MQTT library with the pubsubclient library. I hope one day Adafruit will figure it out and fix it and post here.

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”