Using MQTT with Particle Electron

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

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:

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!

1 Like