# Using MQTT with Particle Electron

**URL:** <https://community.particle.io/t/using-mqtt-with-particle-electron/40452>\
**Category:** Cloud\
**Created:** [March 19, 2018, 6:12pm UTC](https://community.particle.io/t/using-mqtt-with-particle-electron/40452 "2018-03-19T18:12:04Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![031014](https://avatars.discourse-cdn.com/v4/letter/0/53a042/32.png) [@031014](https://community.particle.io/u/031014)\
**Post date:** [March 20, 2018, 5:06pm UTC](https://community.particle.io/t/using-mqtt-with-particle-electron/40452/2 "2018-03-20T17:06:36Z")

</div>

Hey, I have done a bit with MQTT, but not specifically on the Electron. So you might need to do some experimentation on your end specifically to do what I am explaining...

MQTT has the option to send a "hearbeat" packet, which pings the Broker to keep the connection alive. Overall, the communication needed is pretty small, as you can see here:

[https://help.devicewise.com/display/ARG/MQTT+data+usage](https://help.devicewise.com/display/ARG/MQTT+data+usage)

Anyway, if you want to completely disconnect from the MQTT server as well as cellular, you may actually penalize yourself by using more overhead to reconnect once you come back up. Check out the example scenarios given in the link above. Also, your QoS will use a bit more data depending on your choices, as well having a birth message and a will should the device become disconnected suddenly.

Anyway...

I took a look through the libraries, and it looks like there is a client.disconnect() function inside of the MQTT library, which looks to be the one specified on the page you linked. That way, you can be sure you are disconnected from the broker for sure, and safely break your cellular connection. I've linked some previous threads that talk about turning cellular on and off on the Electron here:

> [@How to retain control of an Electron using Cellular.on and Cellular.off](https://community.particle.io/t/how-to-retain-control-of-an-electron-using-cellular-on-and-cellular-off/25158/8):
>
> Can someone confirm that Cellular.off() will not work if electron is in green flashing “Looking for Internet” mode ? Seems I can get it to white breathing (cellular off) mode for a few seconds the first time I try (in flashing green mode) Then it goes back to flashing green by itself after a couple seconds and won’t respond to Cellular.off() at all from then on. I’m trying to put some fault tolerancy in my app to reset the cellular completely, but seems once in Looking for Internet mode, ther…

> [@Electron Cellular.off() and Sleep Modes](https://community.particle.io/t/electron-cellular-off-and-sleep-modes/27670/2):
>
> I’ve been playing around with the Electron and Sleep Modes also. Watching the current consumption of the Electron when putting it to sleep you can see it takes a few before it turns off and current drops way down. I’m using the code from this post and it may explain why your seeing what you’re seeing also:

Then, you void function to disconnect from cell (and the MQTT Broker) when you don't want to keep using cell data, but you will need to reconnect both to cell and the MQTT broker each time you want to connect again. As mentioned above, this might not actually be the most data efficient, and you may want to crunch some numbers to make sure your transmission rate is going to be worth turning off and on again.

I am still learning MQTT and the Particle stuff, so if anyone has any advice I would be glad to hear it as well!

---

_[View the full topic](https://community.particle.io/t/using-mqtt-with-particle-electron/40452)._
