knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.82k stars 1.47k forks source link

Callback fires really rare #219

Open aleksai opened 7 years ago

aleksai commented 7 years ago

Hi.

Using Arduino MEGA, ESP-12-F, NodeHAP-JS and Mosca. Publisher and Client successfully connecting and subscribing to topic. When I'm publishing from server (switching on and off in Home app) in topic nothing happens basically. But if I toggle in Home app fast and like 3-4 times one message can be received by ESP and Arduino and callback is firing. But how to make it stable?

My code:

#include <WiFiEsp.h>
#include <PubSubClient.h>

const char* ssid =  "LD 1904";
const char* password =  "krokodil14";
const char* mqtt_server = "192.168.1.6";
const char* topic1 = "AnyaLights";
int mqtt_port = 1883;
const String mqtt_device_name = "ESP8266-0101";
void callback(char* topic, byte* payload, unsigned int length);

WiFiEspClient espClient;
PubSubClient mqttClient(mqtt_server, mqtt_port, callback, espClient);

bool on = false;

void setup() {
  Serial.begin(115200);
  Serial3.begin(115200);
  WiFi.init(&Serial3);

  setup_wifi();
  mqttConnectAndSubscribe();
}

void loop() {
  mqttLoop();
  crossFade(red);
  mqttLoop();
  crossFade(green);
  mqttLoop();
  crossFade(blue);
  mqttLoop();
  crossFade(yellow);
}

void mqttLoop() {
  if (!mqttClient.connected()) {
    mqttConnectAndSubscribe();
  }
  mqttClient.loop();
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(said);

  if (mqttClient.connected()) mqttClient.disconnect();

  WiFi.begin(ssid, password);

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

  randomSeed(micros());

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

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[1] == 'n') {
    on = true;
  } else {
    on = false;
  }
}

void mqttConnectAndSubscribe() {
  Serial.print("Checking MQTT connection...");
  if(!mqttClient.connected()) {
    Serial.print("MQTT Not connected, trying to connect, ");
    // Attempt to connect
    mqttClient.connect(mqtt_device_name.c_str());
    if (mqttClient.connected()) {
      Serial.println("connected and subscribed to topic");
      mqttClient.subscribe(topic1);
    }
  } else {
    Serial.println("failed.");
  }
}
alistairbill commented 7 years ago

I can confirm a similar thing happens to me. Publishing messages works fine, but printing out messages from a topic will only work if you send a large number of messages to that channel in a short space of time. I'm also using the WifiEspClient library.

aleksai commented 7 years ago

@knolleary Please, have a look.

knolleary commented 7 years ago

@lenyapugachev can you describe a bit more clearly what you are trying to do?

You are publishing to a topic that the ESP is subscribed to, but you don't always receive the message? Are you publishing at a high rate? What size messages are involved?

knolleary commented 7 years ago

You have a function called crossFade in you code that I don't see defined anywhere. What is done in that function? How long does it spend in that function?

aleksai commented 7 years ago

Hello, @knolleary

Thanks for your help.

crossFade doesn't matter, now all my tests including just one line in loop and it is mqttLoop();

Publishing is successful, I'm not sure about rate and size of publishing messages because I'm using HAP-NodeJS and can't found rate and size defining in its code. But, I'm sure ESP got messages because it's blinking everytime I'm publishing. Also, publishing can cause reconnection of ESP to MQTT. Here is a video about that (I didn't catch moment of rare message receiving, because it is really rare, but it still happens from time to time):

Video on Dropbox