Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback fires really rare #219

Open
aleksai opened this issue Dec 17, 2016 · 5 comments
Open

Callback fires really rare #219

aleksai opened this issue Dec 17, 2016 · 5 comments

Comments

@aleksai
Copy link

aleksai commented Dec 17, 2016

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
Copy link

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
Copy link
Author

aleksai commented Dec 19, 2016

@knolleary Please, have a look.

@aleksai aleksai closed this as completed Dec 19, 2016
@aleksai aleksai reopened this Dec 19, 2016
@knolleary
Copy link
Owner

@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
Copy link
Owner

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
Copy link
Author

aleksai commented Dec 20, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants