Mqtt disconnecting within 1 ms specifically

I am trying to connect to losant using sim7600x 4g module with arduino via mqtt. I am trying to send temp data to the losant (I have used tinyGSM library).

Now for this I have used this code:

#define TINY_GSM_MODEM_SIM7600


#include <TinyGsmClient.h>
#include <Losant.h>


// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial

// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1

// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[]  = "*****";
const char user[] = "";
const char pass[] = "";

// Losant credentials.
const char* LOSANT_DEVICE_ID = "***"
const char* LOSANT_ACCESS_KEY = "***"
const char* LOSANT_ACCESS_SECRET = "**"

TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
LosantDevice device(LOSANT_DEVICE_ID);


void connect() {
  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  modem.restart();

  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem: ");
  SerialMon.println(modemInfo);

  // Unlock your SIM card with a PIN
  modem.simUnlock("1234");

  SerialMon.print("Waiting for network...");
  if (!modem.waitForNetwork()) {
    SerialMon.println(" fail");
    while (true);
  }
  SerialMon.println(" OK");

  SerialMon.print("Connecting to ");
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, user, pass)) {
    SerialMon.println(" fail");
    while (true);
  }
  SerialMon.println(" OK");

  // Connect to Losant.
  SerialMon.println();
  SerialMon.print("Connecting to Losant...");

  device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
  while (!device.connected()) {
    delay(500);
    SerialMon.print(".");
  }
  SerialMon.println("Connected!");
}

void setup() {
  // Set console baud rate
  SerialMon.begin(115200);
  delay(10);

  // Set GSM module baud rate
  SerialAT.begin(115200);
  delay(3000);


  connect();
}
void loop() {

  bool toReconnect = false;

  if (!device.connected()) {
    SerialMon.println("Disconnected from Losant");
    //SerialMon.println(device.mqttClient.state());
    toReconnect = true;
  }

  if (toReconnect) {
    connect();

  }
  StaticJsonDocument<2500> jsonBuffer;
  JsonObject root = jsonBuffer.to<JsonObject>();
  root["temp"] = 25.5;
  device.sendState(root);
  delay(1000);

  device.loop();

  delay(100);
}

Now you can see i Have hardcoded the data in the Json document so as to directly send the my previously made device dashboard in specific attribute.

main issue:
the device connects to the sim network but doesnt proceed to connect to losant. the code below runs but never shows “connected” in the serial monitor.

  device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
  while (!device.connected()) {
//    device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
    delay(500);
    SerialMon.print(".");
  }
  SerialMon.println("Connected!");
}

I am attaching the SS of the serial monitor here for better understanding .

Now upon inspection of the losant device log I find that it only connects for a milisecond and then immediately disconnects.Below i have attached the SS.

I would be very thankful if anyone of you can look into the issues I am facing .

Hi @Kaustav_Thakur and welcome to the Losant forums!

We’ll take a look at your code and see if we can determine the source of the issue.

Note that we do have an Arduino MQTT client library that might be easier to work with. I suggest giving that a try.

We are already using Losant Library as you can see in the code. Please add support for TinyGSM or MQTT AT Commands in your library. We are using SIM7600 4G Modules. I guess it’s very important for Losant to add support for communication via 4G Modules.

Are you able to connect to a different MQTT broker successfully from any MQTT client on that device? You can test with a free public broker (such as HiveMQ).

Also, I suggest temporarily removing (or commenting out) any code related to device state (attributes) while troubleshooting the connection component, such as the following:

StaticJsonDocument<2500> jsonBuffer;
JsonObject root = [jsonBuffer.to](http://jsonbuffer.to/)<JsonObject>();
root["temp"] = 25.5;
device.sendState(root);
delay(1000);

Please let us know your results and we’ll take it from there.

When I am connecting with WifiClient it works, everything works. But the issue comes when i try to use TinyGSM Client with Sim module(sim7600x specifically in my case)

I tried Hive as you suggested but it gets disconnected soon after connecting using TinyGSM Client.

Kindly look into this as IoT depends not only on Wifi but also 4G Modules like these.

Thanks for the feedback. It sounds like the underlying MQTT client library that we use (arduino-mqtt) might not support the module you’re using. Unfortunately, as we don’t have control over that library, our suggestion is to try a different MQTT client, such as:

Please let us know if we can provide any further assistance.