Electron can't connect to MQTT Broker

I have some code that connects to the iot.eclipse.org MQTT broker. This code works as expected on my Photon but is unable to connect to the broker on my Electron. Using the IP address instead of the host name didn’t make a difference.

I’ve tried flashing OTA as well as locally using the CLI. Flashing is successful and the Electron restarts. I get a cyan breathing light but no connection to the broker. The code is such that it re-tries connecting to the broker if it is not connected, before calling the loop() method on the mqttClient.

There is no hardware connected to the Electron (or Photon for that matter). I’m currently just publishing messages to one topic when I receive messages from another topic. Using Serial.println I’m able to see that the Electron is not connecting to the broker.

I haven’t figured out how to format code here so my apologies for pasting unformatted code below.

void setup() {
    //RGB.control(true);
    Serial.begin(9600);
}

void loop() {
  if (!mqttClient.isConnected())
  {
    connectToMqttBroker();
  }

  Serial.println("loop");

  mqttClient.loop();
}

void connectToMqttBroker(){
  if (!mqttClient.connect(clientIdentifier))
  {
      Serial.println("Connection to Broker Failed");
  }
  else
  {
    if (mqttClient.subscribe(commandTopic))
    {
        mqttClient.publish(eventTopic, "Ready & Waiting");
        Serial.print("Subscribed to Topic: ");
        Serial.println(commandTopic);
    }
  }

One contributing factor might be the use of UDP on the Electron while the WiFi devices use TCP
What library is Cellular available in?


I’ve reformatted your code.
Just put your code between

 ```cpp
 // your code
1 Like

Thanks for the tip about code formatting and also for formatting my post!

I believe the Electron does support TCP (I remember reading it somewhere on here).

If what you state is in fact correct, I feel the lack of (library) support as well as any compiler errors or documentation clearly indicating what is and isn’t supported is going to result in a lot of wasted time/effort for folks. I hope there is a plan to bring the Electron in parity with the Photon.

TCP is supported (when using the respective objects like TCPClient/TCPServer or any derivatives of them), but since the default cloud connection is UDP based rather than TCP three is no “permanent” connection unless you take care of that.
But since I have not tested any of this, it’s only a guess what might contribute (as worded in my original post) to your issue.

@ScruffR,

I’m using the MQTT library. One would expect it would be talking MQTT which is TCP/IP based :slight_smile: .

As said, only an idea.
Could also be a side effect like too short timout settings which might not take into consideration the latency introduced through the need to fist make contact to the cell towers, then build the TCP connection.
I found for instance that Blynk responds very sluggish on the Electron while on the Photon it’s near-instant.