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

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

Broker: 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:

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

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

1 Like

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

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

1 Like

Could be a lot of different things. The first place to look is the Application Communication Log. You’ll want to have this open when the disconnect occurs to see if anything is logged.

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

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

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

image

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?

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

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

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 showing?

Thanks,
Julia

1 Like

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

1 Like

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 sensors to Losant?

Please your response is anticipated.

Regards

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.

Let me know if you have any more questions!

Have a great day,
Julia

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

Hi,

The connection is done with MQTT, there is also a more detailed walkthrough by NCD.io that can be found here.

Thanks!
Julia