Using MQTT with Particle Electron

I am just getting into MQTT and found a good example in this link:

https://www.mathworks.com/help/thingspeak/use-photon-client-to-publish-to-a-channel.html

My question is that the code establishes a connection to the broker using the code below. For a Photon, that is not a problem, but for an Electron, does this mean that this MQTT connection will keep consuming cellular data even if I am not publishing?

If the answer is yes, how does one close then reopen the connection? Any sample code is appreciated and thanks in advance.

void MQTTConnect()
{
    //https://www.mathworks.com/help/thingspeak/use-photon-client-to-publish-to-a-channel.html
    if (!client.isConnected()) reconnect();
    client.loop();  // Call the loop continuously to establish connection to the server.
}

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

Thank you very much @031014. I appreciate your time and extensive input.

From your input, it appears its is better fr me to keep the connection alive as the disconnect/connect overhead will exceed the keep alive pings given my “not infrequent” publishing to the broker …

1 Like

No problem! Glad to help.

Again, you may have some benefit that I am unaware of when using the electron, since I have not used it. I honestly don’t know if there are background operations that your provider sends to the Electron that use up data or not. Good luck using MQTT!