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

Is subscribe broken? #1065

Open
rwb196884 opened this issue Aug 27, 2024 · 9 comments
Open

Is subscribe broken? #1065

rwb196884 opened this issue Aug 27, 2024 · 9 comments

Comments

@rwb196884
Copy link

Broker is Debian 12 and I'm seeing lots of messages at zigbee2mqtt/lux in mosquitto_sub but nothing from this.

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

// ESP8266 wifi board things.
#define RX 6 // blue
#define TX 7 // black
SoftwareSerial Esp8266(RX, TX);
const char AP[] = "ap";
const char PASS[] = "secret";
WiFiEspClient wifi_client;

// mqtt things.
const char*  mqtt_broker = "192.168.0.113";
const int mqtt_port = 1883;
const char mqtt_topic[] = "zigbee2mqtt/lux";
PubSubClient mqtt_client(wifi_client);



void setup() {
   pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  Esp8266.begin(9600);
  WiFi.init(&Esp8266);
  
  // Check for the presence of the shield.
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);
  }

  // Connect to WiFi network.
  while ( WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(AP);
    WiFi.begin(AP, PASS);
  }
  Serial.println("WIFI connected.");

  mqtt_client.setServer(mqtt_broker, 1883);
  mqtt_client.setCallback(mqtt_callback);
}

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

void mqtt_reconnect() {
  // Loop until we're reconnected.
  while (!mqtt_client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect.
    if (mqtt_client.connect("arduinoClient")) {
      Serial.println("  MQTT connected");

      int s = mqtt_client.subscribe("zigbee2mqtt/lux");
      Serial.print("    subscribe ");
      Serial.println(s);
    } else {
      Serial.print("  MQTT connection failed, rc=");
      Serial.print(mqtt_client.state());
      Serial.println(" try again in 5 seconds...");
      delay(5000);
    }
  }
}

int n = 0;
void loop() {
  if (!mqtt_client.connected()) {
    mqtt_reconnect();
    mqtt_client.subscribe("zigbee2mqtt/lux");
    Serial.print("MQTT state: ");
    Serial.println(mqtt_client.state());
    n = 0;
  }
  
  if( n == 40 ) {
    Serial.println("<");
    n = 0;
  }
  else {
    Serial.print("<");
    n++;
  }
  
  mqtt_client.loop();
  Serial.print(">");
}

Output:

[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: ap
[WiFiEsp] Connected to calleva
WIFI connected.
Attempting MQTT connection...[WiFiEsp] Connecting to 192.168.0.113
  MQTT connected
    subscribe 1
MQTT state: 0
<><><

There are usually two or three <> then a < and long pause after which most of the time mqtt_reconnect is called but sometimes it says [WiFiEsp] TIMEOUT: 10 and hangs.

No message is ever received and mqtt_allback is never called.

Is this supposed to work?

@rwb196884
Copy link
Author

It seems that if the message has to be sent in between < and > being printed -- so you have to spam hundreds of them to the broker on the off-chance that one of them gets through.

@LB5825
Copy link

LB5825 commented Aug 28, 2024

I am having the same issue have you found a fix for this?

@rwb196884
Copy link
Author

No.

This library was last updated over 4 years ago and the owner has lost interest: this library is dead and it's fucked.

There is a fork of this that is being maintained at https://github.com/thingsboard/pubsubclient but I cannot see it in Library Manager and it has no installation insctuctions.

@knolleary
Copy link
Owner

Can you try adding a delay(50) in your main loop? On some hardware, calling client loop in a hard spin can overwhelm the network layer.

This library is in maintenance mode. It is pretty much stable and successfully used by many users. Equally, as you say, forks exist that add additional features if that is what you want.

@rwb196884
Copy link
Author

Neither this nor the ThingsBoard version receive data from subscriptions. Gut feeling is that it's something to do with timing and loop.

A workaround is to run this on debian to read the topic and expose it to the network.

hile true; do nc -l -p 1884 -q 1 -c "mosquitto_sub -h localhost -t \"foo/bar/buz\" -C 1 -F \"%p\"" ; done

and then read it like this

     // Try to get the new temp.
      wifi_client.connect("192.168.0.113", 1884);
      delay(500);
      bool waited = 0;
      for(int i=0; i < 12 && (!wifi_client.connected() || !wifi_client.available()); i++){
        Serial.print(i);
        Serial.print("... ");
        delay(500);
        waited = true;
      }
      if( waited ){ Serial.println(); }
      StaticJsonDocument<32> jDoc;
      if (wifi_client.available()) {
        deserializeJson(jDoc, wifi_client);
        foo = jDoc["foo"];
        Serial.print(current_foo);
        Serial.print(" -> ");
        Serial.println(foo);
        if(foo> 0 & current_foo == 0 ){ current_foo = foo; }
      }
      else {
        Serial.println("No data from 1884");
      }
      wifi_client.stop();

A problem with this is that you can only see the latest retained message on the topic in MQTT therefore the value must represent the desired state rather than an instruction to change state; comparing foo to the current_foo determines whether an action must be taken.

@abdosn
Copy link

abdosn commented Oct 15, 2024

Would you use WiFiClient instead of WiFiEspClient
Also try to add some code in loop to check the connection and reconnect of wifi

@rwb196884
Copy link
Author

rwb196884 commented Oct 20, 2024

Moved from Arduino nano to ESP32 and same problem -- nothing is ever received.

#include <WiFi.h>
#include <PubSubClient.h>

const char *ssid = "196883";
const char *password = "SECRET_PASWORD";

const char * mqtt_broker = "192.168.0.113";
const int mqtt_port = 1883;

WiFiClient wifi_client;

PubSubClient mqtt_client(wifi_client);

void setup() {
  Serial.begin(115200);
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);

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

  mqtt_client.setServer(mqtt_broker, mqtt_port);
  mqtt_client.setCallback(mqtt_callback);
  while (!mqtt_client.connected()) {
    Serial.println("-- connect_mqtt --");
    if (mqtt_client.connect("arduinoClient")) {
      mqtt_client.subscribe("zigbee2mqtt/#");
      return;
    } else {
      Serial.print("  failed, rc=");
      Serial.print(mqtt_client.state());
      Serial.println(". Retrying...");
      delay(2000);
    }
  }
}

void mqtt_callback(char* topic, byte* payload, unsigned int length) {
  Serial.println(topic);

  return;
  Serial.println("Payload:");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

int n = 0;
int m = 0;
void loop() {
  if(n > 1000000) {
    Serial.println(m);
    m++;
    n = 0;
  }
  n++;
  mqtt_client.loop();
}

This receives some messages from some topics -- 1mosquitto_sub` receives more messages from more topics.

@rwb196884
Copy link
Author

It turns out that payloads of length 243 or more are ignored -- without warning or error.

#!/bin/sh

n=1;
msg="x"

while [ $n -lt 230 ]; do
        msg=$( echo "x$msg" )
        n=$( echo "$n + 1" | bc )
done

while [ $n -lt 300 ]; do
        echo "Sending ${n}."
        mosquitto_pub -h localhost -t 'Esp32test' -m "$msg"
        msg=$( echo "x$msg" )
        n=$( echo "$n + 1" | bc )
        sleep 1
done

@knolleary
Copy link
Owner

See https://github.com/knolleary/pubsubclient?tab=readme-ov-file#limitations for the default max packet size supported and how to increase it.

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

4 participants