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

publish not send,but no error show #29

Open
Tea-NT opened this issue Oct 8, 2022 · 0 comments
Open

publish not send,but no error show #29

Tea-NT opened this issue Oct 8, 2022 · 0 comments

Comments

@Tea-NT
Copy link

Tea-NT commented Oct 8, 2022

Hi, I am appreciate your Project ,so I want do some contribution, to feedback some question I meet.
I git clone this project to control my ESP8266 board,
if only use mqtt.onData event,it work fine,
but I add a publish call in onData function, the publish not send, other Subscriber can not receive which topic publish. the board also not jump exception.

and the mqtt.onPublish function also not call, I am confused with this, am i use wrong?

  mqtt.onData([](String topic, String data, bool cont) {
    Serial.printf("Data received, topic: %s, data: %s\r\n", topic.c_str(), data.c_str());
    //mqtt.unSubscribe("/qos0");
    Serial.printf("%s \r\n", data.c_str());
    if (data.compareTo("1") == 0 || data.compareTo("ON") == 0)
    {
      digitalWrite(PIN_RELAY1, HIGH );
      digitalWrite(PIN_RELAY2, HIGH );
      mqtt.publish("homeassistant/switch/irrigation/state", "ON", 0, 0); //**actually not work**
      //mqtt.publish("123", "ON", 0, 0);
      digitalWrite(LED_BUILTIN, LOW); //LED ON
      //delay(200);

    }
    //delay(1000); //delay 1 second
    if (data.compareTo("0") == 0 || data.compareTo("OFF") == 0)
    {
      digitalWrite(PIN_RELAY1, LOW );
      digitalWrite(PIN_RELAY2, LOW );
      mqtt.publish("homeassistant/switch/irrigation/state", "OFF", 0, 0); // **actually not work**
      //mqtt.publish("123", "OFF" , 0, 0);
      digitalWrite(LED_BUILTIN, HIGH); //LED OUT
    }
  });
  Serial.println("mqtt OnSub: ");
  mqtt.onSubscribe([](int sub_id) {
    Serial.printf("Subscribe topic id: %d ok\r\n", sub_id);
    mqtt.publish("/qos0", "qos0", 0, 0);                                           // **but this publish WORK FINE!   i am so confused** 
  });

  Serial.println("mqtt OnPub: ");
  mqtt.onPublish([](int pub_id) {                                                   //**this function never call, i don't know why**
    Serial.printf("onPublish return id: %d \r\n", pub_id);
  });

So i use another esp8266 mqtt client PubSubClient, and it work fine.

void callback(char* topic, byte* payload, unsigned int length) {//call back function with PubSubClient
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  String s = "";
  for (int i = 0; i < length; i++) {
    s = s + (char)payload[i];
  }
  Serial.println(s);
  // Switch on the LED if an 1 was received as first character
  if (s == "ON") {
    digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
    client.publish("homeassistant/switch/irrigation/state", (char*)s.c_str());
  } else {
    digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
    client.publish("homeassistant/switch/irrigation/state", (char*)s.c_str());
  }
}
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

1 participant