martin-ger / uMQTTBroker

MQTT Broker library for ESP8266 Arduino
MIT License
445 stars 106 forks source link

Cannot connect to the broker from another ESP #16

Open aartek opened 6 years ago

aartek commented 6 years ago

Hi, I've run your broker on my esp and it works fine. I can use i.e Chrome's MQTTLens extension, subscribe to topics and receive messages, but when I try to use the client you've linked in the readme on another ESP, it cannot connect to the broker (connection callback is never called). Could you check if there is something wrong in my code? Maybe I need to provide some more configuration?

Broker

#include <ESP8266WiFi.h>
#include <uMQTTBroker.h>

/*
 * Your WiFi config here
 */
char ssid[] = "artur";     // your network SSID (name)
char pass[] = "password"; // your network password

class myMQTTBroker: public uMQTTBroker
{
public:
    virtual bool onConnect(IPAddress addr, uint16_t client_count) {
      Serial.println(addr.toString()+" connected");
      return true;
    }

    virtual bool onAuth(String username, String password) {
      Serial.println("Username/Password: "+username+"/"+password);
      return true;
    }

    virtual void onData(String topic, const char *data, uint32_t length) {
      char data_str[length+1];
      os_memcpy(data_str, data, length);
      data_str[length] = '\0';

      Serial.println("received topic '"+topic+"' with data '"+(String)data_str+"'");
    }
};

myMQTTBroker myBroker;

void startWiFiAP()
{
  WiFi.disconnect(true);
  WiFi.softAP(ssid, pass);
  Serial.println("AP started");
  Serial.println("IP address: " + WiFi.softAPIP().toString());
}

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  Serial.println("siema");

  startWiFiAP();

  // Start the broker
  Serial.println("Starting MQTT broker");
  myBroker.init();

  myBroker.subscribe("#");
}

int counter = 0;

void loop()
{
/*
 * Publish the counter value as String
 */
  myBroker.publish("broker/counter", (String)counter++);
  myBroker.publish("broker/test", (String)counter++);

  // wait a second
  delay(1000);
}

Client code (copied from example):

#include <ESP8266WiFi.h>
#include <MQTT.h>

void myDataCb(String& topic, String& data);
void myPublishedCb();
void myDisconnectedCb();
void myConnectedCb();

#define CLIENT_ID "client3"
#define TOPIC "broker/test"

// create MQTT
MQTT myMqtt(CLIENT_ID, "192.168.4.1", 1883);

const char* ssid     = "artur";
const char* password = "password";

//
void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println("Connecting to MQTT server");  

  // setup callbacks
  myMqtt.onConnected(myConnectedCb);
  myMqtt.onDisconnected(myDisconnectedCb);
  myMqtt.onPublished(myPublishedCb);
  myMqtt.onData(myDataCb);

  Serial.println("connect mqtt...");
  myMqtt.connect();

  Serial.println("subscribe to topic...");
  myMqtt.subscribe(TOPIC);

  delay(10);
}

//
void loop() {
}

/*
 * 
 */ 
void myConnectedCb()
{
  Serial.println("connected to MQTT server");
}

void myDisconnectedCb()
{
  Serial.println("disconnected. try to reconnect...");
  delay(500);
  myMqtt.connect();
}

void myPublishedCb()
{
  //Serial.println("published.");
}

void myDataCb(String& topic, String& data)
{

  Serial.print(topic);
  Serial.print(": ");
  Serial.println(data);
}

Client's output:

Connecting to artur
........
WiFi connected
IP address: 
192.168.4.2
Connecting to MQTT server
connect mqtt...
subscribe to topic...

Message "connected to MQTT server" is never displayed. Data are not received.

I also tried with this client https://github.com/knolleary/pubsubclient and its example code but it returns 2 : MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier from the state method, however I'm not sure how accurate this message is, because changing client id didn't help.

martin-ger commented 6 years ago

Currently I'm on a Business Trip. Will look into it on the Weekend.

Am 4. September 2018 16:27:37 schrieb Artur notifications@github.com:

Hi, I've run your broker on my esp and it works fine. I can use i.e Chrome's MQTTLens extension, subscribe to topics and receive messages, but when I try to use the client you've linked in the readme on another ESP, it cannot connect to the broker (connection callback is never called). Could you check if there is something wrong in my code? Maybe I need to provide some more configuration? Broker

include

include / Your WiFi config here */

char ssid[] = "artur"; // your network SSID (name) char pass[] = "password"; // your network password class myMQTTBroker: public uMQTTBroker { public: virtual bool onConnect(IPAddress addr, uint16_t client_count) { Serial.println(addr.toString()+" connected"); return true; } virtual bool onAuth(String username, String password) { Serial.println("Username/Password: "+username+"/"+password); return true; } virtual void onData(String topic, const char data, uint32_t length) { char data_str[length+1]; os_memcpy(data_str, data, length); data_str[length] = '\0'; Serial.println("received topic '"+topic+"' with data '"+(String)data_str+"'"); } }; myMQTTBroker myBroker; void startWiFiAP() { WiFi.disconnect(true); WiFi.softAP(ssid, pass); Serial.println("AP started"); Serial.println("IP address: " + WiFi.softAPIP().toString()); } void setup() { Serial.begin(115200); Serial.println(); Serial.println(); Serial.println("siema"); startWiFiAP(); // Start the broker Serial.println("Starting MQTT broker"); myBroker.init(); myBroker.subscribe("#"); } int counter = 0; void loop() { / Publish the counter value as String / myBroker.publish("broker/counter", (String)counter++); myBroker.publish("broker/test", (String)counter++); // wait a second delay(1000); }

Client code (copied from example):

include

include void myDataCb(String& topic, String& data);

void myPublishedCb(); void myDisconnectedCb(); void myConnectedCb(); #define CLIENT_ID "client3"

define TOPIC "broker/test" // create MQTT

MQTT myMqtt(CLIENT_ID, "192.168.4.1", 1883); const char ssid = "artur"; const char password = "password"; // void setup() { Serial.begin(115200); delay(1000); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.println("Connecting to MQTT server"); // setup callbacks myMqtt.onConnected(myConnectedCb); myMqtt.onDisconnected(myDisconnectedCb); myMqtt.onPublished(myPublishedCb); myMqtt.onData(myDataCb); Serial.println("connect mqtt..."); myMqtt.connect(); Serial.println("subscribe to topic..."); myMqtt.subscribe(TOPIC); delay(10); } // void loop() { } / */ void myConnectedCb() { Serial.println("connected to MQTT server"); } void myDisconnectedCb() { Serial.println("disconnected. try to reconnect..."); delay(500); myMqtt.connect(); } void myPublishedCb() { //Serial.println("published."); } void myDataCb(String& topic, String& data) { Serial.print(topic); Serial.print(": "); Serial.println(data); }

Client's output: Connecting to artur ........ WiFi connected IP address: 192.168.4.2 Connecting to MQTT server connect mqtt... subscribe to topic...

Message "connected to MQTT server" is never displayed. Data are not received. I also tried with this client https://github.com/knolleary/pubsubclient and its example code but it returns 2 : MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier from the state method, however I'm not sure how accurate this message is, because changing client id didn't help. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

aartek commented 6 years ago

Ok, I've solved it. Don't know why, but solution for another issue I had in one of my previous project, helped.

  // WiFi fix: https://github.com/esp8266/Arduino/issues/2186
  WiFi.persistent(false);
  WiFi.mode(WIFI_OFF);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);