# Connection issue ESP32, device disconnects immediately after connecting

**URL:** <https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219>\
**Category:** Help\
**Tags:** mqtt, device\
**Created:** [August 26, 2020, 7:50am UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219 "2020-08-26T07:50:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suroor\_Wijdan](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@Suroor\_Wijdan](https://forums.losant.com/u/Suroor_Wijdan)\
**Post date:** [August 26, 2020, 7:50am UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219/1 "2020-08-26T07:50:52Z")

</div>

I am using an ESP32 board and have followed the tutorial to get it connected to wifi. I am also using Bluetooth to supply the WiFi credentials and verify the device on losant for authorization and then supply the access keys back to the board for connection to losant.

All this works great, WiFI connects but the problem is the device connects to Losant only briefly and gets disconnected almost immediately which results in the device being stuck in the connection wait loop.

I have tried various different posts and answers on this forum but not able to figure out the issue.

I am using the latest losant mqtt module for arduino.

![Screenshot 2020-08-26 at 1.11.42 PM](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/b/be69e18af39c9af5b7743339c944984fc4f0ce10.png)

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [August 26, 2020, 1:07pm UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219/3 "2020-08-26T13:07:03Z")

</div>

@Suroor_Wijdan,

This seems like it could be something on the device side of things. Are there any device logs you can share?

---

<div class="post-metadata">

**Author:** ![Suroor\_Wijdan](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@Suroor\_Wijdan](https://forums.losant.com/u/Suroor_Wijdan)\
**Post date:** [August 26, 2020, 2:19pm UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219/5 "2020-08-26T14:19:27Z")

</div>

Device just logs Connecting to Losant… and then just …

I maybe doing something wrong but sometimes it does seem to work but then the device again disconnects. Attaching screenshot from the code that i have currently.

 ![Screenshot 2020-08-26 at 7.47.30 PM](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/3/38312f5e361be68cd65fc628100fad30e9cb3b4c.png)

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [August 26, 2020, 3:05pm UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219/7 "2020-08-26T15:05:32Z")

</div>

@Suroor_Wijdan,

You mentioned you were following a tutorial, could you provide a link to the tutorial you’re referring to?

Overall, we’ve improved the ESP32 example, you may find the latest code here:

> <https://github.com/Losant/losant-mqtt-arduino/blob/master/examples/esp32/esp32.ino>

Would you be able to give that a try and see if you get the same results?

---

<div class="post-metadata">

**Author:** ![Suroor\_Wijdan](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@Suroor\_Wijdan](https://forums.losant.com/u/Suroor_Wijdan)\
**Post date:** [August 26, 2020, 7:39pm UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219/9 "2020-08-26T19:39:11Z")

</div>

@anaptfox I am actually following that tutorial. The problem is when i run the esp32.ino sketch without modifying anything with just my device id and keys hardcoded. Everything seems to work as expected.

But in my use case, i am authenticating through ble and transferring the keys, deviceId over ble to the device and making an attempt to connect to losant.

I moved to platformio and i have got a log that seems to be originating from the underneath ssl client, not sure what is causing the issue here.

 ![Screenshot 2020-08-27 at 1.04.20 AM](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/5/52a911b43c6ff032305a34516321500b557539e9.png)

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [August 26, 2020, 9:06pm UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219/11 "2020-08-26T21:06:06Z")

</div>

@Suroor_Wijdan,

I was able to reproduce what you’re seeing.

However, it seems to be an issue with an underlying library that the Losant MQTT Arduino uses:

> <https://github.com/256dpi/arduino-mqtt/issues/210>
>
> I'm running this mqtt client with arduino-esp32 framework, using WiFiClient of arduino-esp32 as network layer. In this setup every time after...

I’m going to add some details to that issue for the library developer to see if we can get this resolved.

However, as a path forward, for recent development using the ESP32, we recommend using the [ESP-idf](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html).

You may find instructions on getting your environment set up here:

[https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html#installation-step-by-step](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html#installation-step-by-step)

Also, to help, we are working on a new guide for connecting the ESP32 to Losant using the ESP-idf to provide a solid example. We’ll follow up on this post when that is complete.

---

<div class="post-metadata">

**Author:** ![Suroor\_Wijdan](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@Suroor\_Wijdan](https://forums.losant.com/u/Suroor_Wijdan)\
**Post date:** [August 27, 2020, 7:16am UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219/12 "2020-08-27T07:16:32Z")

</div>

Thanks @anaptfox for looking into it.

What is strange is that sometimes it does work as expected and connects perfectly without being disconnected but then that is 1 out of 3 tries. Not sure what is the core issue, i will keep looking into it to see if there is a memory or heap availability issue on it.

I will also check out the IDF environment. Thanks

---

<div class="post-metadata">

**Author:** ![Wayne\_Hughes](https://avatars.discourse-cdn.com/v4/letter/w/2bfe46/32.png) [@Wayne\_Hughes](https://forums.losant.com/u/Wayne_Hughes)\
**Post date:** [September 3, 2020, 12:35am UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219/14 "2020-09-03T00:35:41Z")

</div>

Hi @anaptfox - any chance this could be impacting the ESP8266 platform? I am experiencing the disconnect after adding some more data to my report section.

---

<div class="post-metadata">

**Author:** ![Wayne\_Hughes](https://avatars.discourse-cdn.com/v4/letter/w/2bfe46/32.png) [@Wayne\_Hughes](https://forums.losant.com/u/Wayne_Hughes)\
**Post date:** [September 3, 2020, 1:17am UTC](https://forums.losant.com/t/connection-issue-esp32-device-disconnects-immediately-after-connecting/3219/16 "2020-09-03T01:17:56Z")

</div>

Actually, I have figured out what is causing my issue. It seems that any more than nine items cannot be reported at one time. My workaround was to create a unique report function for each group of data and send the reports individually.
