eclipse / paho.mqtt.embedded-c

Paho MQTT C client library for embedded systems. Paho is an Eclipse IoT project (https://iot.eclipse.org/)

Home Page:https://eclipse.org/paho

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Subscribe function stuck

gomezramones opened this issue · comments

Hi, I'm running PAHO library on Arduino idle to test QoS 2 and though mqtt connected, it stucked in the sibscribe function waiting and waiting. I'm running this simple code

`#define TINY_GSM_MODEM_SIM5360
#define MQTTCLIENT_QOS2 1

#include <Arduino.h>
#include <MQTTClient.h>
#include <IPStack.h>
#include <TinyGsmClient.h>
#include <Countdown.h>

#define SerialAT Serial3

TinyGsm modem(SerialAT);
TinyGsmClient SIM3G(modem);

IPStack ipstack(SIM3G);
MQTT::Client<IPStack, Countdown, 50, 1> client = MQTT::Client<IPStack, Countdown, 50, 1>(ipstack);

int arrivedcount = 0;

//const char* topicPub = "GsmClientTest/led";
//const char* topicSubs = "GsmClientTest/init";

MQTT::Message message;

void messageArrived(MQTT::MessageData& md)
{
MQTT::Message &message = md.message;

Serial.print("Message ");
Serial.print(++arrivedcount);
Serial.print(" arrived: qos ");
Serial.print(message.qos);
Serial.print(", retained ");
Serial.print(message.retained);
Serial.print(", dup ");
Serial.print(message.dup);
Serial.print(", packetid ");
Serial.println(message.id);
Serial.print("Payload ");
Serial.println((char*)message.payload);
}

void conexionsim(){
Serial.println("Initializing modem...");
Serial3.println("AT");
String S = Serial3.readString();
Serial.println(S);
modem.restart();

Serial.println("Waiting for network...");
if (!modem.waitForNetwork()) {
Serial.println("fail");
conexionsim();
}
Serial.println(" OK");
}

void conexionred(){
const char apn[] ="internet.digitel.ve";// "em";
const char user[] = "";
const char pass[] = "";
Serial.print("Connecting to ");
Serial.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
Serial.println("fail");
conexionsim();
conexionred();

}
Serial.println(" OK");
}

void connect()
{
char hostname[] = "test.mosquitto.org";
int port = 1883;

Serial.print("Connecting to ");
Serial.print(hostname);
Serial.print(":");
Serial.println(port);

int rc = ipstack.connect(hostname, port);
if (rc != 1)
{
Serial.print("rc from TCP connect is ");
Serial.println(rc);
}

Serial.println("MQTT connecting");
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = (char*)"GsmClientTest/init";
rc = client.connect(data);
if (rc != 0)
{
Serial.print("rc from MQTT connect is ");
Serial.println(rc);
}
Serial.println("MQTT connected");

rc = client.subscribe("GsmClientTest/init", MQTT::QOS2, messageArrived);
if (rc != 0)
{
Serial.print("rc from MQTT subscribe is ");
Serial.println(rc);
}
Serial.println("MQTT subscribed");
}

void setup()
{
Serial.begin(9600);
Serial3.begin(115200);
conexionsim();
conexionred();
Serial.println("MQTT Hello example");
connect();
}

void loop()
{
if (!client.isConnected())
connect();

arrivedcount = 0;

// Send and receive QoS 0 message
char buf[100];
strcpy(buf, "Hello World! QoS 0 message");
message.qos = MQTT::QOS0;
message.retained = false;
message.dup = false;
message.payload = (void*)buf;
message.payloadlen = strlen(buf)+1;
int rc = client.publish("GsmClientTest/led", message);
while (arrivedcount == 0)
{
Serial.println("Waiting for QoS 0 message");
client.yield(1000);
}

// Send and receive QoS 1 message
strcpy(buf, "Hello World! QoS 1 message");
message.qos = MQTT::QOS1;
message.payloadlen = strlen(buf)+1;
rc = client.publish("GsmClientTest/led", message);
while (arrivedcount == 1)
{
Serial.println("Waiting for QoS 1 message");
client.yield(1000);
}

// Send and receive QoS 2 message
strcpy(buf, "Hello World! QoS 2 message");
message.qos = MQTT::QOS2;
message.payloadlen = strlen(buf)+1;
rc = client.publish("GsmClientTest/led", message);
while (arrivedcount == 2)
{
Serial.println("Waiting for QoS 2 message");
client.yield(1000);
}
delay(2000);
}`

I have been looking up on other forums to see how to solve this problem but I don't see any answer. May you help me?