# Mqtt disconnecting within 1 ms specifically

**URL:** <https://forums.losant.com/t/mqtt-disconnecting-within-1-ms-specifically/5096>\
**Category:** Help\
**Tags:** mqtt\
**Created:** [June 9, 2023, 9:34am UTC](https://forums.losant.com/t/mqtt-disconnecting-within-1-ms-specifically/5096 "2023-06-09T09:34:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaustav\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@Kaustav\_Thakur](https://forums.losant.com/u/Kaustav_Thakur)\
**Post date:** [June 9, 2023, 9:34am UTC](https://forums.losant.com/t/mqtt-disconnecting-within-1-ms-specifically/5096/1 "2023-06-09T09:34:32Z")

</div>

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:

```auto
#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.

```auto
  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 .

 ![serialmonitor](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/5/583b67c97807da1c9b00fb274b92c88042ab1b05.png)

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.

 ![losant](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/e/e2dff2458085eb6507aced1f45af2989288f08a7.png)

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

---

<div class="post-metadata">

**Author:** ![Sebastian\_Turner](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/sebastian_turner/32/4024_2.png) [@Sebastian\_Turner](https://forums.losant.com/u/Sebastian_Turner)\
**Post date:** [June 9, 2023, 2:55pm UTC](https://forums.losant.com/t/mqtt-disconnecting-within-1-ms-specifically/5096/2 "2023-06-09T14:55:46Z")

</div>

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](https://docs.losant.com/mqtt/arduino/) that might be easier to work with. I suggest giving that a try.

---

<div class="post-metadata">

**Author:** ![Kaustav\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@Kaustav\_Thakur](https://forums.losant.com/u/Kaustav_Thakur)\
**Post date:** [June 10, 2023, 5:35am UTC](https://forums.losant.com/t/mqtt-disconnecting-within-1-ms-specifically/5096/3 "2023-06-10T05:35:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Turner](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/sebastian_turner/32/4024_2.png) [@Sebastian\_Turner](https://forums.losant.com/u/Sebastian_Turner)\
**Post date:** [June 12, 2023, 6:25pm UTC](https://forums.losant.com/t/mqtt-disconnecting-within-1-ms-specifically/5096/4 "2023-06-12T18:25:14Z")

</div>

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](https://www.hivemq.com/public-mqtt-broker/)).

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Kaustav\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@Kaustav\_Thakur](https://forums.losant.com/u/Kaustav_Thakur)\
**Post date:** [June 13, 2023, 1:47pm UTC](https://forums.losant.com/t/mqtt-disconnecting-within-1-ms-specifically/5096/5 "2023-06-13T13:47:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Turner](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/sebastian_turner/32/4024_2.png) [@Sebastian\_Turner](https://forums.losant.com/u/Sebastian_Turner)\
**Post date:** [June 13, 2023, 2:39pm UTC](https://forums.losant.com/t/mqtt-disconnecting-within-1-ms-specifically/5096/6 "2023-06-13T14:39:01Z")

</div>

Thanks for the feedback. It sounds like the underlying MQTT client library that we use ([arduino-mqtt](https://github.com/256dpi/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:

> **[GitHub - knolleary/pubsubclient: A client library for the Arduino Ethernet...](https://github.com/knolleary/pubsubclient)**
>
> A client library for the Arduino Ethernet Shield that provides support for MQTT. - GitHub - knolleary/pubsubclient: A client library for the Arduino Ethernet Shield that provides support for MQTT.

> **[GitHub - arduino-libraries/ArduinoMqttClient: ArduinoMqttClient Library for...](https://github.com/arduino-libraries/ArduinoMqttClient)**
>
> ArduinoMqttClient Library for Arduino. Contribute to arduino-libraries/ArduinoMqttClient development by creating an account on GitHub.

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