AnaviTechnology / anavi-thermometer-sw

Open source Arduino sketch for the smart WiFi dev board ANAVI Thermometer
GNU General Public License v3.0
27 stars 17 forks source link

version >= 1.0.2 won't connect to mqtt broker #44

Open brianmay opened 3 years ago

brianmay commented 3 years ago

If I install version 1.0.1, it works fine.

But if I then install version 1.0.2 or 1.0.3 it doesn't appear to be sending any messages to my mosquitto server. I suspect it can't connect to the server for some reason. wifi is connected though.

If I then install version 1.0.1 again it works fine.

If I work anything else out I will add here.

Maybe something wrong with mqtt authentication???

leon-anavi commented 3 years ago

Hi @brianmay,

Thank you for reporting this issue. Could you please share the serial output from ANAVI Thermometer that you get with version 1.0.3 when there is an issue with the MQTT connection?

The serial output should contain information what is going on when the thermometer tries to connect to the MQTT broker.

Thanks, Leon

brianmay commented 3 years ago

Oh, I never thought of looking at the serial data.... Anyway, here is what I think is relevant:

MQTT: 1+N: Using single connection, multiple status topics
Mini I2C OLED Display at address 3C: OK
Press button within 2 seconds for factory reset...
mounting FS...
mounted file system
reading config file
opened config file
{"mqtt_server":"mqtt.linuxpenguins.xyz","mqtt_port":"1883","workgroup":"workgroup",}
                                                                                   n
*WM: Adding parameter
*WM: server
*WM: Adding parameter
*WM: port
*WM: Adding parameter
*WM: workgroup
*WM: Adding parameter
*WM: user
*WM: Adding parameter
*WM: pass
*WM: Adding parameter
*WM: temp_scale
*WM: Adding parameter
*WM: ha_name
*WM: Adding parameter
*WM: ota_server
*WM: Adding parameter
*WM: 
*WM: 
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Status:
*WM: 6
*WM: Using last saved values, should be faster
*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.5.62
connected...yeey :)
local ip
192.168.5.62
MQTT Server: mqtt.linuxpenguins.xyz
MQTT Port: 1883
MQTT Username: brian
MQTT Password: ****************
Saved temperature scale: celsius
Temperature scale: Celsius
Home Assistant sensor name: 
No OTA server
set_spec client_id: anavi-d7065-esp8266
MQTT esp8266 failed, rc=-1

I wonder about the "No OTA server" but surely this isn't required for MQTT to work? Other then that, I don't see anything obviously wrong.

brianmay commented 3 years ago

This is the same thing with 1.0.1:

Mini I2C OLED Display at address 3C: OK
Press button within 2 seconds for factory reset...
mounting FS...
mounted file system
reading config file
opened config file
{"mqtt_server":"mqtt.linuxpenguins.xyz","mqtt_port":"1883","workgroup":"workgroup",}
                                                                                   n
*WM: Adding parameter
*WM: server
*WM: Adding parameter
*WM: port
*WM: Adding parameter
*WM: workgroup
*WM: Adding parameter
*WM: user
*WM: Adding parameter
*WM: pass
*WM: Adding parameter
*WM: temp_scale
*WM: Adding parameter
*WM: ha_name
*WM: Adding parameter
*WM: ota_server
*WM: Adding parameter
*WM: 
*WM: 
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Status:
*WM: 6
*WM: Using last saved values, should be faster
*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.5.62
connected...yeey :)
local ip
192.168.5.62
MQTT Server: mqtt.linuxpenguins.xyz
MQTT Port: 1883
MQTT Username: brian
MQTT Password: ****************
Saved temperature scale: celsius
Temperature scale: Celsius
Home Assistant sensor name: 
No OTA server
Attempting MQTT connection...connected
leon-anavi commented 3 years ago

@brianmay thank you for providing the serial output. Yes, "No OTA server" isn't related to the MQTT connection.

The open source firmware of ANAVI Thermometer relies on the popular open source Arduino library pubsubclient. I believe the rc=-1 is coming from it and I've just seen a report about issues caused by too long username/password.

Is it possible for you to have a look at the MQTT broker logs and to try by configuring a shorter password in the MQTT broker?

Btw we had the same error code for an older version of the firmware but it has been fixed: https://github.com/AnaviTechnology/anavi-thermometer-sw/issues/43

Thanks, Leon

brianmay commented 3 years ago

Crazy. The MQTT logs don't have anything helpful unfortunately. The password is 16 characters, will try to setup an 8 character password for testing.

brianmay commented 3 years ago

Confirmed. An 8 character password looks better. So maybe this problem wasn't fixed :-(

leon-anavi commented 3 years ago

Hi @brianmay,

Thank you for testing it out and providing feedback. Great, that the work around with a shorter password does the job.

The fix in https://github.com/AnaviTechnology/anavi-thermometer-sw/issues/43 was for the length of the ClientID. So you are experiencing a different issue which results in the same rc=-1 error code from pubsubclient.

It is interesting why longer password used to work with older versions of the firmware? Let's leave this issue open for further investigation of the password length limitations.

Thanks, Leon

brianmay commented 3 years ago

OK, thanks.

Yes, it is stange that this did work with 1.0.1

Also can confirm the obvious: Both passwords consist entirely of standard ASCII characters.

ajinkya-open commented 1 year ago

[ tested and workinf for my case ] check if your case it help i made a routing in code like below im not testin gmqttclient connection, but the underlying espclient i checking with connection with 80 port, i hosted a apache just for pining purpose nothing elsem , in main loop i check it for connection purpose and in global variable i setting the indication of internet avalabel if ping is success as belo, and in connect loop of mq tt i check if internet is availeble then only i reconect to it, elese i dont, and one more obseration , using IP it workes satisfacotry but if i use domain instead of ip , it wont work as intended, Im stuck to manage broker ip , because shoing ip directoly is not a good solution

ping function

boolean pingServer()
{
  // if (pinger.conne
  if (pinger.connect(brokerIP, 80))
  {
    // Serial.println("ping : true");
    internetAvailable = true;
    // Make a HTTP request:
  }
  else
  {
    // Serial.println("ping : false");
    internetAvailable = false;
  }
  return internetAvailable;
}

in reconnec logic of mqtt do this

void reconnect()
{
  // Loop until we're reconnected
  if (internetAvailable)
  {
    while (!client.connected())
    {
      // Serial.print("Attempting MQTT connection...");
      // Create a random client ID
      String clientId = "XXXXX-";
      clientId += String(random(0xffff), HEX);
      // Attempt to connect
      if (client.connect(clientId.c_str()))
      {
        // Serial.println("connectedMQTT");
        client.subscribe(topicX.c_str());
//publish to some topi to know status of connectivity
      }
      else
      {
        // Serial.print("failed, rc=");
        // Serial.print(client.state());
        // Wait 5 seconds before retrying
      }

    }
  }
  else
  {
    // Serial.println("Internet not present so not pinging 1883"); // it continuesly print it, better keep commended this line
  }
}

let know if it work in your case also, pub sub lib, generic async mqtt lib has same underlying issue , that breakage of internet from ISP causes continuous disconnection status, keep ing pending reconnect state, but in this patch try if working in your case