# \[SOLVED\] Frequent MQTT disconnections; "Device Already Connected"

**URL:** <https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96>\
**Category:** Help\
**Created:** [April 13, 2016, 9:31pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96 "2016-04-13T21:31:36Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [September 7, 2016, 1:15pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/21 "2016-09-07T13:15:04Z")

</div>

You’ll want to double-click the mqtt node to bring up its configuration. Then edit the broker details and use the following information:

 ![](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/1X/3a107134e7ef3f384158755c2cd60cd378eca91e.png)

**Broker** : [broker.losant.com](http://broker.losant.com)  
**Port** : 1883  
**Client ID** : your gateway’s device ID  
**Username** : your access key  
**Password** : your access secret

You are correct on the topic to publish to, which is **losant/your-peripheral-id/state**. The payload is required to be JSON and look like the following:

```json
{
  "data" : {
    "attribute-name" : Number | String | Boolean,
    "attribute-name-2" : Number | String | Boolean
  }
} 

```

You can use the Application Communication Log to help debug possible issues.

 ![](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/1X/23ded7a759dfe112cec284de5597b39036d86c55.png)

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [January 4, 2017, 12:39am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/22 "2017-01-04T00:39:24Z")

</div>

I wrote a full article describing how to use Node-RED as a Losant gateway. Check it out here: [https://www.losant.com/blog/how-to-use-node-red-as-losant-gateway](https://www.losant.com/blog/how-to-use-node-red-as-losant-gateway)

---

<div class="post-metadata">

**Author:** ![Sreyams\_Jain1](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sreyams\_Jain1](https://forums.losant.com/u/Sreyams_Jain1)\
**Post date:** [March 21, 2019, 9:07pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/23 "2019-03-21T21:07:18Z")

</div>

Brandon,

following this, I have a question about subscribing to this message sent using the MQTT broker.  
I have issues in not able to retrieve the message sent. MQTT subscribe node is getting disconnected as soon as the message is sent o the broker, I am working with Julia on this issue on the post I made recently. Have not found any answer yet

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [March 21, 2019, 9:14pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/24 "2019-03-21T21:14:47Z")

</div>

Could be a lot of different things. The first place to look is the [Application Communication Log](https://docs.losant.com/applications/overview/#application-log). You’ll want to have this open when the disconnect occurs to see if anything is logged.

---

<div class="post-metadata">

**Author:** ![Sreyams\_Jain1](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sreyams\_Jain1](https://forums.losant.com/u/Sreyams_Jain1)\
**Post date:** [March 22, 2019, 12:39am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/25 "2019-03-22T00:39:16Z")

</div>

In Node-red, I have a MQTT node sending msg to losant broker. Also have a MQTT node that subscribes to the same topic to display the msg just sent.  
The application log shows correct as below  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/0/0b9f99f70a4e8f241f41005b2c9c5c5e8cdbf518.png)

However, if I send a message using python mqtt code as below  
import time  
from losantmqtt import Device  
from datetime import datetime

# Construct device

device = Device(“5c91925b750e410008659cb4”, “1964a537-e8af-4970-ad37-e50fbf9e2a43”, “e44ba12e78fb315a4d87cd92f8b9103f9630cac141731039d3d3e2e68b98b2be”)

def on\_connect(device):  
now = datetime.now()  
datetime\_object = now.strftime("%m/%d/%Y, %H:%M:%S")  
frame = “Sending frame data”  
peopleCount = 1  
status = device.send\_state({ “frame”: “Hello”,“peopleCount”: 2 })  
print(status)  
device.close()

# Listen for commands.

device.add\_event\_observer(“connect”, on\_connect)

# Connect to Losant.

device.connect()  
#device.close()

Application log shows as below

![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/6/64fa51ae984e9b068bff64b789e70fd4ded1c5db.png)

After a few seconds, application log show with few more status … complete log is now

![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/3/3b5513cd40ef42ae09410710a7cf9b33c4a536f0.png)

On the Node-red side, I was expecting the message form subscribing to show up … but does not. The MQTT node went disconnect state and then got connected

Any ideas?

---

<div class="post-metadata">

**Author:** ![Sreyams\_Jain1](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sreyams\_Jain1](https://forums.losant.com/u/Sreyams_Jain1)\
**Post date:** [March 22, 2019, 12:40am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/26 "2019-03-22T00:40:57Z")

</div>

Having posted the code with access keys etc , I have just deleted those credentials to be safe on my end.

---

<div class="post-metadata">

**Author:** ![Sunday\_Ajiroghene](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/sunday_ajiroghene/32/4915_2.png) [@Sunday\_Ajiroghene](https://forums.losant.com/u/Sunday_Ajiroghene)\
**Post date:** [May 23, 2019, 9:26pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/27 "2019-05-23T21:26:13Z")

</div>

Hello I am a little bit of this challenge.

I followed exactly Taron Foxworth tutorial on Arduino esp8266 and DHT22. I however replaced DHT22 with DHT11.

After communication with losant platform, it disconnect and try to connect but stays in this loop forever.

You guide needed please

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [May 24, 2019, 2:47pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/28 "2019-05-24T14:47:49Z")

</div>

Hello @Sunday_Ajiroghene,

Welcome to the Losant Forums! I need just a bit more information to assist in debugging. What is the output on your serial monitor? Are you able to send data to Losant, and if so, what is your [Application Log](https://docs.losant.com/applications/overview/#application-log) showing?

Thanks,  
Julia

---

<div class="post-metadata">

**Author:** ![Sunday\_Ajiroghene](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/sunday_ajiroghene/32/4915_2.png) [@Sunday\_Ajiroghene](https://forums.losant.com/u/Sunday_Ajiroghene)\
**Post date:** [May 24, 2019, 4:14pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/29 "2019-05-24T16:14:23Z")

</div>

Yes, I am able to send data. The issue am having is disconnection after a while.

I basically followed through the tutorial pane on DHT22, Arduino and Losant.

A critical review of the sample code shows that whenever the http request return zero, there is a while loop it gets into and never leaves forever.

My guess is, due to the erratic nature of Internet connectivity there would always be such moment where device cannot connect to the cloud on a fleeting moment.

Instead of entering the while loop and never leaves, I added a break statement. I also increased the time of wait for sensor to read before concluding there is a bug.

With these, its a bit stable. Looses network, re-establish connection without throwing such exception. I would observed it further.

Meanwhile, I have a more serious issue with pir + esp8266 + mongoose +losant.

It’s not just making a single connection. Detail report soon.

Regards

---

<div class="post-metadata">

**Author:** ![Sunday\_Ajiroghene](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/sunday_ajiroghene/32/4915_2.png) [@Sunday\_Ajiroghene](https://forums.losant.com/u/Sunday_Ajiroghene)\
**Post date:** [May 30, 2019, 6:54am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/30 "2019-05-30T06:54:45Z")

</div>

Hello Brandon,

Which is the best and most stable route to take device to losant, is it through node red, Lora etc.

I recently took a few device to losant using MQTT via esp8266 and it appears so easy using the Arduino IDE.

Finally, which is the best way to connect [NCD.io](http://NCD.io) sensors to Losant?

Please your response is anticipated.

Regards

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [May 30, 2019, 7:57pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/31 "2019-05-30T19:57:59Z")

</div>

Hi @Sunday_Ajiroghene,

The most stable route will likely depend on the device, but there is a specific NCD device for connection to Losant that takes about 5 minutes to set up. It can be found [here](https://store.ncd.io/product/wifi-micro-gateway-losant-iot/).

Let me know if you have any more questions!

Have a great day,  
Julia

---

<div class="post-metadata">

**Author:** ![Sunday\_Ajiroghene](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/sunday_ajiroghene/32/4915_2.png) [@Sunday\_Ajiroghene](https://forums.losant.com/u/Sunday_Ajiroghene)\
**Post date:** [May 30, 2019, 11:00pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/32 "2019-05-30T23:00:16Z")

</div>

Ok!

I have gone through the resource material, thanks.

For this NCD - losant pair, what communication protocol is been implemented.

I can see it doesn’t look like mqtt yet I can place, which it is.

Your reply would be useful to me as a way of being informed.

Regards,

Sunny

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [May 31, 2019, 3:23pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/33 "2019-05-31T15:23:55Z")

</div>

Hi,

The connection is done with MQTT, there is also a more detailed walkthrough by [NCD.io](http://NCD.io) that can be found [here](https://ncd.io/wifi-micro-gateway-setup-for-losant/).

Thanks!  
Julia

---

<div class="post-metadata">

**Author:** ![shweta\_Shweta](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@shweta\_Shweta](https://forums.losant.com/u/shweta_Shweta)\
**Post date:** [February 18, 2025, 9:46am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/34 "2025-02-18T09:46:00Z")

</div>

Hi, I am having same disconnect problem and we have mqtt connection in a continous running streaming recipe means there can be reason that it disconnects due to this "Message throughput limit exceeded”, so how to check this if limit is exceeded and how to solve this? Please let me know.

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [February 18, 2025, 2:50pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/35 "2025-02-18T14:50:44Z")

</div>

You will see that message in the [device’s connection logs](https://docs.losant.com/devices/overview/#connection-log). It indicates that your device was publishing messages faster than the [throughput limit](https://docs.losant.com/mqtt/overview/#message-limits) per client connection.

You can catch occurrences of this using the [Device: Disconnect Trigger](https://docs.losant.com/workflows/triggers/on-disconnect/).

[Previous page](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96.md?page=1)
