0

I looked around for solutions to my issue by searching on the forum and elsewhere and haven't seen similar issues - (https://arduino.stackexchange.com/search?page=2&tab=Relevance&q=MQTT%20ESP8266)

I am describing below what I am trying to achieve: (I am relatively new)

I would like to pass streaming joystick data from a "Remote Control" through MQTT to a "OnBoard" unit to drive my robot. Both the "Remote Control" and "OnBoard" units are NodeMCUs ESP8266.

I am able to separately send data from my Joystick to the MQTT broker and also separately read streaming data from the MQTT broker on any MQTT client. However, the moment I connect the "Remote Control" and the "OnBoard" Units to run their respective code, I keep receiving a Lost Connection to MQTT server error. I also observe this happens right after my Joystick ESP8266 connects.

I suspect this is related to one of the following -

  1. 0>> I am doing something fundamentally wrong [Syntax, usage of libraries, logic?]
  2. connection parameters to the MQTT client [keep alive?, QoS?, delay?] b) there
  3. is something to with sequence of Sub & Pub?
  4. the version of ESP MQTT libraries i am using??
  5. this is not the best way to stream data to control devices? WebSockets?

I am copying my code for both units below:

RemoteControl Unit: [Joystick input is given through Serial]

#include <PubSubClient.h>
#include <EspMQTTClient.h>

void onConnectionEstablished();

EspMQTTClient client(
  "mywifi",           // Wifi ssid
  "somepwd!",           // Wifi password
  "iot.eclipse.org",  // MQTT broker ip
  1883,             // MQTT broker port
  "usr",            // MQTT username
  "mqttpass",       // MQTT password
  "test1",          // Client name
  onConnectionEstablished, // Connection established callback
  true,             // Enable web updater
  true              // Enable debug messages
);

void setup()
{
  Serial.begin(115200);
}

void onConnectionEstablished()
{
  Serial.println("Remote Connection Established");
}

void loop()
{
  if (Serial.available() > 0) {
    Serial.println("Inside Serial");
    char bfr[101];
    memset(bfr,0,101);
    Serial.readBytesUntil('\n',bfr,100);
    client.publish("DriveChannel", bfr);
  }
  client.loop();
}

OnBoard Unit:

#include <PubSubClient.h>
#include <EspMQTTClient.h>

void onConnectionEstablished();

EspMQTTClient client(
  "mywifi",           // Wifi ssid
  "somepwd!",         // Wifi password
  "iot.eclipse.org",  // MQTT broker ip
  1883,             // MQTT broker port
  "usr",            // MQTT username
  "mqttpass",       // MQTT password
  "test1",          // Client name
  onConnectionEstablished, // Connection established callback
  true,             // Enable web updater
  true              // Enable debug messages
);

void setup()
{
  Serial.begin(115200);
}

void onConnectionEstablished()
{
  client.subscribe("DriveChannel", [] (const String &payload)
  {
    Serial.print("In Connection payload---> ");
    Serial.println(payload);
  });
}

void loop()
{
  delay(3000);
  client.loop();
}
  • 1
    try different client names – Juraj Feb 23 at 12:55
  • @Juraj perfect... missed that. Thank you for pointing out. – bhanu Feb 23 at 15:50
  • was it the cause? – Juraj Feb 23 at 17:39
  • Yes @juraj. I think that was the issue. Since the other mqtt clients (app or browser based) assign their own client id I wasn't having an issue subscribing to it but these two had the same client ids and that seemed to conflict. I have a steady stream of data flowing now with no loss of connection. However, I am seeing a significant delay, it almost feels like instructions being sent to Mars Rover :) , about 10 to 20 second delay for the signals to reach my Onboard unit from the joystick unit. – bhanu Feb 24 at 23:49
  • and the 3 secs delay in loop? – Juraj Feb 25 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.