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.
if (!client.isConnected()) reconnect();
client.loop(); // Call the loop continuously to establish connection to the server.
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.
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!
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!