๐ŸŽ„โ›„โ„๏ธ๐ŸŽ It's beginning to look a lot like ADABOX-mas! The next ADABOX from Adafruit ships soon! Get yours now, or give as a gift! ๐ŸŽ„โ›„โ„๏ธ๐ŸŽ
0

MQTT connection closed
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.

MQTT connection closed

by rajendran78 on Wed Nov 06, 2019 5:39 am

I am currently using BC66 module to send GPS data (using L96) to adafruit IO. I was able to publish this data to my feed. But now when I try I get +QMTSTAT 1,1 which means the connection is closed by a peer. Even re-opening of the connection doesn't resolve the issue.
I was able to publish data 1 month ago, but now with the same hardware and code, it can't. I have even checked with my NBIoT service provider and they have informed no issue at their side.
I have attached my log below
14:25:58.432 -> BC66 Setup...
14:26:00.400 -> BC66 Setup success
14:26:00.400 -> Connecting to Adafruit io via MQTT...
14:26:01.431 -> at+qmtcfg="version",0,4
14:26:01.478 -> OK
14:26:02.509 -> AT+QMTCLOSE=1
14:26:02.509 -> ERROR
14:26:05.508 -> at+qmtopen=1,"io.adafruit.com",1883
14:26:05.555 -> OK
14:26:06.540 ->
14:26:07.711 -> +QMTOPEN: 1,0
14:26:07.711 -> 2
14:26:09.726 -> at+qmtconn=1,"io.adafruit.com","<username>",<aio key>"
14:26:09.726 -> OK
14:26:09.726 ->
14:26:09.726 -> +QMTCONN: 1,0,0
14:26:09.726 ->
14:26:09.726 -> +QMTSTAT: 1,1
14:26:09.726 -> Publish GPS Data
14:26:10.757 -> at+qmtpub=1,0,0,0,"<username>/feeds/GPS/csv","24,1.4439263,103.7842,10"
14:26:10.757 -> ERROR

rajendran78
 
Posts: 8
Joined: Tue May 07, 2019 3:49 am

Re: MQTT connection closed

by brubell on Fri Nov 08, 2019 5:42 pm

Hi rajendran20,

I was able to publish data 1 month ago, but now with the same hardware and code, it can't. I have even checked with my NBIoT service provider and they have informed no issue at their side.


Thanks for ruling out the Network - it's difficult to debug IoT projects!

What tier of Adafruit IO do you have and how frequently is your code publishing data to Adafruit IO?

Could you check the Adafruit IO Website's monitor page (https://io.adafruit.com/monitor) for errors relating to the publish, and post them here?

Thanks!

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

Re: MQTT connection closed

by rajendran78 on Mon Nov 11, 2019 2:23 am

Hi, Thanks alot for getting back, I have attached the connection status in the monitor as requested. Could see that it keeps disconnecting once connection has been made.

2019/11/11 2:13:06pm
io.adafruit.com
connected
2019/11/11 2:12:56pm
io.adafruit.com
disconnected
111.65.58.183
2019/11/11 2:12:56pm
io.adafruit.com
connected
2019/11/11 2:12:46pm
io.adafruit.com
disconnected
111.65.58.183
2019/11/11 2:12:46pm
io.adafruit.com
connected
2019/11/11 2:12:36pm
io.adafruit.com
disconnected
111.65.58.183
2019/11/11 2:12:36pm
io.adafruit.com
connected

rajendran78
 
Posts: 8
Joined: Tue May 07, 2019 3:49 am

Re: MQTT connection closed

by rajendran78 on Mon Nov 11, 2019 2:25 am

I am using the free tier , and I used to post a GPS data every 30mins.

rajendran78
 
Posts: 8
Joined: Tue May 07, 2019 3:49 am

Re: MQTT connection closed

by brubell on Mon Nov 11, 2019 11:06 am

rajendran20 wrote:I am using the free tier , and I used to post a GPS data every 30mins.


Ok, could you please post the code you're using and I'll take a look?

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

Re: MQTT connection closed

by rajendran78 on Mon Nov 11, 2019 9:52 pm

Hi,

I have attached my code. Thanks again for helping out.
Attachments
nbiot_FYP.ino
(7.43 KiB) Downloaded 2 times

rajendran78
 
Posts: 8
Joined: Tue May 07, 2019 3:49 am

Re: MQTT connection closed

by brubell on Tue Nov 12, 2019 11:00 am

rajendran20,

I don't like opening attachments on my computer. Instead, could you paste your code using code tags?

Code: Select all | TOGGLE FULL SIZE
like this

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

Re: MQTT connection closed

by rajendran78 on Tue Nov 12, 2019 9:28 pm

Here goes,
//ESP32 retrieves GPS data from Serial1 and Trasmits via Serial2 using BC66 NBIoT module

#include <Arduino.h>
#include <FuGPS.h>
#define SerialAT Serial2
#define PwrEn 23
#define PwrKey 22
#define Reset 21

FuGPS fuGPS(Serial1);
bool gpsAlive = false;
char gpsbuffer[120];


String ComReturnData = "";
bool Connection = false;
bool gpsdata=false;
int count=0;


void setup()
{
Serial.begin(115200);
//Serial1.begin(9600, SERIAL_8N1, 16, 17);
Serial1.begin(9600, SERIAL_8N1, 5, 18);

// fuGPS.sendCommand(FUGPS_PMTK_SET_NMEA_BAUDRATE_9600);
// fuGPS.sendCommand(FUGPS_PMTK_SET_NMEA_UPDATERATE_1HZ);
fuGPS.sendCommand(FUGPS_PMTK_API_SET_NMEA_OUTPUT_RMCGGA);
Serial.begin(115200);
SerialAT.begin(115200, SERIAL_8N1, 16, 17);
SerialAT.flush();
Serial.println("BC66 Setup...");
pinMode(PwrEn, OUTPUT);
pinMode(PwrKey, OUTPUT);
pinMode(Reset, OUTPUT);
digitalWrite(PwrEn, LOW);
digitalWrite(PwrKey, HIGH);
digitalWrite(Reset, LOW);
ComReturnData = "";
bool Connection = false;
delay(2000);
Serial.println("BC66 Setup success");
}

void loop()
{
char *p = gpsbuffer;
gpsdata=false;
Serial.println("Waiting for GPS Data...");

// Valid NMEA message
if (fuGPS.read())
{
// We don't know, which message was came first (GGA or RMC).
// Thats why some fields may be empty.

gpsAlive = true;

Serial.print("Quality: ");
Serial.println(fuGPS.Quality);

Serial.print("Satellites: ");
Serial.println(fuGPS.Satellites);

if (fuGPS.hasFix() == true)
{
Serial.println("Speed" + String(fuGPS.Speed, 6)+"Latitude" + String(fuGPS.Latitude, 6) + "Longtitude" + String(fuGPS.Longitude, 6)+"Altitude" + String(fuGPS.Altitude, 6));
gpsbuffer[0] = '"';p++;
dtostrf(fuGPS.Speed, 2, 6, p);
p += strlen(p);
p[0] = ','; p++;

dtostrf(fuGPS.Latitude, 2, 6, p);
p += strlen(p);
p[0] = ','; p++;

dtostrf(fuGPS.Longitude, 3, 6, p);
p += strlen(p);
p[0] = ','; p++;

dtostrf(fuGPS.Altitude, 2, 6, p);
p += strlen(p);
p[0] = '"';p++;
// null terminate
p[0] = 0;

Serial.print("Sending: ");
Serial.println(gpsbuffer);
gpsdata=true;

}
}

// Default is 10 seconds
if (fuGPS.isAlive() == false)
{
if (gpsAlive == true)
{
gpsAlive = false;
Serial.println("GPS module not responding with valid data.");
Serial.println("Check wiring or restart.");
}
}
if(gpsdata==true)
{
BC66_boot();
BC66_Initial(); //Used for checking NBIoT connection, can be disabled
BC66_MQTT_Connect();
delay(30000);
}
}

void BC66_boot() //BC66 Reboot routing
{
Serial.println("Initializing AT...");
digitalWrite(PwrKey, LOW);
delay(1000);
digitalWrite(PwrKey, HIGH);
delay(1000);
while (ComReturnData.indexOf("OK") < 0)
{
SerialAT.println("AT\r\n");
Serial.println("AT\r\n");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
delay(1000);
}
Serial.println("");
Serial.println("Checking ATI...");
ComReturnData = "";
while (ComReturnData.indexOf("OK") < 0)
{
SerialAT.println("ATI\r\n");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
delay(1000);
}
Serial.println("");
Serial.println("Checking CPIN...");
ComReturnData = "";
while (ComReturnData.indexOf("OK") < 0)
{
SerialAT.println("AT+CPIN?\r\n");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
if (ComReturnData.indexOf("ERROR") > 0)
{
Serial.println("SIM card not found... Please insert card and reboot device\n");
ComReturnData = "";
}
}
Serial.println("");
Serial.println("Checking CSQ...");
ComReturnData = "";
while (ComReturnData.indexOf("OK") < 0)
{
SerialAT.println("AT+CSQ\r\n");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
}
Serial.println("");
Serial.println("Checking CREG...");
ComReturnData = "";
while (ComReturnData.indexOf("OK") < 0)
{
SerialAT.println("AT+CREG?\r\n");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
}
Serial.println("");
Serial.println("BC66 Start Succeed! \n");
}
//========================================================================

void BC66_Initial() //BC66 Module Initialization, attach to network and register in telcom.
{
Serial.println("Checking CGATT...");
ComReturnData = "";
//while (ComReturnData.indexOf("+CGATT: 1") < 0)
while (ComReturnData.indexOf("OK") < 0)
{
SerialAT.println("AT+CGATT?\r\n");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
}
Serial.println("");
}

void BC66_MQTT_Connect()
{
reset:
Serial.println("Connecting to Adafruit io via MQTT...");
//Serial.println("at+qmtopen=1,io.adafruit.com,1883");
ComReturnData = "";
while (ComReturnData.indexOf("ERROR")+ ComReturnData.indexOf("OK") < 0 ) //To force close mqtt connection if its open, if already close Error will be return
{
SerialAT.println("AT+QMTCLOSE=1");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
}
ComReturnData = "";
delay(2000);
while (ComReturnData.indexOf("OK") < 0 ) //Establish connection to MQTT
{
SerialAT.println("at+qmtopen=1,\"io.adafruit.com\",1883");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
}
count=0;
while (ComReturnData.indexOf("+QMTOPEN: 1,0") < 0 ) //Wait for connection success status
{
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
count++;
if(count>75) //Do count up to 75 while waiting for status, if more than 75 times, re connect again
{
goto reset;
}
}
Serial.println(count);
ComReturnData = "";
while ( ComReturnData.indexOf("OK") < 0 ) //Connect to adafruit.io using username and AIO key
{
SerialAT.println("at+qmtconn=1,\"io.adafruit.com\",\"username\",aiokey");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
}
while ( ComReturnData.indexOf("+QMTCONN: 1,0,0") < 0 ) //wait for connection success status
{
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
}
ComReturnData = "";
count=0;
while (ComReturnData.indexOf("OK") < 0 && count<2) //publish data 2 times
{
Serial.println("Publish GPS Data");
SerialAT.println(String("")+"at+qmtpub=1,0,0,0,\"username/feeds/GPS/csv\","+gpsbuffer);
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
if(ComReturnData.indexOf("ERROR")>=0)
{
count++;
Serial.println(count);
}
}
ComReturnData = "";
while (ComReturnData.indexOf("ERROR")+ComReturnData.indexOf("OK")<0 ) //close MQTT connection
{
SerialAT.println("at+qmtclose=1");
ComReturnData = SerialAT.readString();
ComReturnData.trim();
Serial.println(ComReturnData);
}
delay(2000);
ComReturnData = "";
Serial.println("");
}

rajendran78
 
Posts: 8
Joined: Tue May 07, 2019 3:49 am

Re: MQTT connection closed

by brubell on Wed Nov 13, 2019 11:24 am

Could you try publishing "hello world" or some generic data to Adafruit IO, to rule out the possibility that the GPS data is what's causing the disconnect from the broker?

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

Re: MQTT connection closed

by rajendran78 on Wed Nov 13, 2019 12:59 pm

Hi,

But I am having my MQTT connection closed even before publishing my GPS data. Once I execute AT+QMTCONN, it returns back +QMTCONN 1,0,0 which means successful connection. But immediately +QMTSTAT 1,1 arrives which indicates connection closed. So as you can see when I execute AT+QMTPUB to publish the GPS data, it returns the error as the connection has been closed. So adafruit.io closes my connection once I authenticate successfully which is what I cant understand. Thanks.

rajendran78
 
Posts: 8
Joined: Tue May 07, 2019 3:49 am

Please be positive and constructive with your questions and comments.