I’m trying unsuccessfully so far to connect a Core against a (HASS.io) MQTT broker on my LAN. The ports are not forwarded on my router so any connections from the WAN won’t make it through to the broker.
Does this explain why the Core doesn’t even manage to connect to the broker (have checked the logs, no trace).
The broker has other connections coming in from RaPis so it’s not that the broker is misconfigured.
I’m basically wondering if the MQTT messages come from the cloud/WAN or if they are sent over the LAN?
Hi @daneboomer,
I reply this issue from your private message. here is same answer.
MQTT connection startup sequence is from local. MQTT based protocol is TCP it’s hard to connect from MQTT broker on cloud(or internet) to Photon on local LAN.
I think about your problem, you could check the TCP connection from your Core(Photon?) is really sending or not.
Brilliant! Got it working in the end, thank you, somehow I’d acquired a version of the code which had “server” in inverted commas. When I took out the inverted commas it connected beautifully.
Now to have some fun!
My question is: is it best to have a Core or a Photon dedicated to JUST doing MQTT stuff, or would that Core/Photon only be possible to use as a kind of rosetta stone between MQTT land and the Particle cloud?
I notice that in the demo there’s no provision for the Core/Photon to ever attempt to re-connect if the broker goes down - is that something I’d implement myself just by changing this
if (client.isConnected())
client.loop();
to something like this
if (client.isConnected())
client.loop();
else
client.connect("sparkclient");
Your changing for reconnect to the MQTT server loop is right. Demo source code is just a first step sample:)
MQTT(TLS) work with AWS IoT/Azure/Google IoT Core…etc general MQTT cloud servers, this library could use for several MQTT servers and Particle cloud protocol exchange proxy as you have pointed out.
I think “which is the best?” is dependent on application requirement. If it want to use publish/subscribe easily Particle cloud is good.
On the other case, if application have to connect to MQTT cloud server for several reason, this library would one of the answer.
MQTT _mqttClient(MQTT_ENDPOINT, MQTT_PORT, MQTT_KEEPALIVE, _mqttDispatch);
bool commsProcessor() {
// first see if we're already connected, process the mqtt
// loop if we are
if (_mqttClient.isConnected()) {
_mqttClient.loop();
return true;
}
// ok, so we're not (yet) connected, get a connection and
// subscribe to topics
_mqttClient.connect("<your TOPIC>"); // fill in with your 'top' topic
if (_mqttClient.isConnected()) {
_mqSubscribe(SUB_COMMANDS); // defines for sub topic
_mqSubscribe(SUB_CONFIG);
_mqSubscribe(SUB_ECHO);
mqDebugPrintf("mqtt is up");
return true;
}
else {
//USBSerial1.printf(" FAILED!\n");
return false;
}
}
(code example is shortened up a bit).
commsProcessor() is called regularly in the loop(). So this method ensures you process messages as well as handle mqtt server disconnects (to some degree).
Thanks, Hiro
However, I was more wondering about whether running MQTT AND other bits and pieces on a Core (or even a Photon) was asking too much of the device and whether it would be best to dedicate a Core (or a Photon) to acting as an interchange device between the Particle Cloud and MQTT land.