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
[BUG] MQTT connection fails #623
Comments
It could be that the Arduino Due library does not handle the DNS call for IP addresses in the same way that the ESP32 libraries do. I read that you would have to call the |
Update: On my system, I get this, if I use an IP address that is not in use:
Once I switch back to the correct server's IP address, I get this:
|
Using the hostname did not work either. My local DNS server is configured correctly and does resolve the name (which I've verified using another system). |
Can you try the following please:
to these lines
and see if that works and what the serial monitor outputs as mqtt_host and mqtt_port? |
I have now uploaded a version that calls setServer with an IPAddress object, so if that was the problem, it should work now. |
It's working now when using an IP address. 👍🏻 |
Good to know it works - and yes, if you have the time, please feel free to figure out what's going (wr)on(g). My changes simply converted the IP address from a string to actual numerical values which the EDIT: I just had a look myself and figured out that at least on my machine, the code in Dns.h for the getHostByName function is exactly the same, including accepting an IP address as a string instead of its numerical values (for this, it doesn't even call a DNS server). Therefore, it should also have no problem resolving a hostname. So the problem must lie somewhere else. |
After fiddling around with the Arduino Dns.h library and dig/nslookup from another Linux machine, I've found out that my DNS server was in fact configured wrong and could not resolve some local addresses. Everything is fine now! The issue can be closed. |
Grrr ;)... Ok, in order not to keep useless code in the scarce memory that we have, can you please test again by replacing these lines:
with this line:
If this works (as it obviously should, going by the code), I can revert everything to the state before... |
Damn, I was wrong about the DNS resolving on the Arduino... My DNS server really does work now, but the Arduino's name resolution still does not. I had overlooked some left over debug code I wrote, which had an IP address hard coded... 😞
The test for a valid IP could be done more elegant using a RegExp or so as the address components (addr[n]) are not used afterwards. Maybe you have an idea for a better solution. As I said, I am not a C developer... Of course Dns.h needs to be included. I did it in line 615 of BSB_LAN.ino, as it only seems to be needed when MQTT is used:
After all I think there must be a difference between some network libraries used for Arduino and for ESP. Otherwise I could not explain the different behaviour. |
Ok, thanks for testing, but I'm going to wait now until some other Arduino Due user confirms this problem. Because since the libraries seem to be identical to the point that I could follow up with it, the whole DNS resolution would have to have a problem on the Due then. The code you posted is almost identical to what is already in the EthernetClient.cpp for the Due:
Maybe there is a problem with your library inclusion or something like that. And/or you could add debug messages in this section to figure out when it's called and what paths are taken. But since the Client object of PubSubClient is based on EthernetClient, it should work right out of the box, as it does on the ESP32. |
I have updated today to BSB-LAN, Version 3.4.4-20240331020948 and for some so far unknown reason the MQTT broker is not showing the expected data content. I have received some indication from my DNS server that there had been a lot of suspicious requests to "BSB-LAN/status": This is the content of my current file "BSB_LAN_config.h":
|
Is this related to the original bug report? Because you seem to be able to make a connection, you just mention the broker "not showing the expected data content" which could mean anything from no data to wrong data or correct data in the wrong places. |
I guess it is related to the original bug report.
Change this line in
to
succeeded to connect. Therefore I think that some differences in DNS might exist which lead to the issue on the Arduino Due. Interresting wise my PiHole (acting as DNS Server) did alert a "rate limit":
The dns query was not asking for the mqtt hostname, instead it was asking for the |
@drfjordan Interesting find! I've looked into the dnsmasq logfiles on my server and found the same behavior (192.168.0.95 is BSB-LAN's IP):
"bsb-lan" is in lowercase letters on my system because I've changed the MQTTTopicPrefix in the config. Note that my workaround (#623 (comment)) is still working on my system (for both given IP and server name), maybe that helps when trying to find the actual problem. |
Ok, thanks for the confirmation. I had just written to @drfjordan to open up a separate bug report for this, but it really seems that the root cause is the same for both, which is that for whatever reason the will topic is used as domain name and not the actual hostname entered in the configuration. This might explain why there is also no difference when you use an IP address or a FQDN because in both cases it uses a wrong memory address. First I thought that this is a problem with a new EEPROM layout, but since @drfjordan has UseEEPROM set to 0, the EEPROM should not be used at all. Could you nevertheless both clear the EEPROM using
This really looks like some kind of memory leak, but if hard-coding the hostname works for both of you, it has to happen somewhere in those lines of code above. Let's hope we find it quickly :)... |
Clearing the EEPROM did not change anything (I've actually disabled the EEPROM feature anyways). The last
Then the output stops and the Arduino is gone. So I commented out the last
Note that the first two tries always give a different result in the second line than all subsequent tries. |
Thanks for responding so quickly. The second line (with the "garbage") is expected, as it just prints out whatever there is in the reserved memory location. The fact that it contains the right content (i.e. hostname and port) shows that it works up to here. The rest is all correct as well: The string gets cut into the right components. And yet you still get the Will Topic as the domain name that your DNS server has to resolve? Even with your workaround at the top, you should arrive at the same content until you do the DNS lookup where you query the host by name. Since both Arduino Due and ESP32 use the same PubSubClient library that we distribute with this project, any kind of overflow would have to happen on the ESP32 as well - and it works fine on my setup. PubSubClient even provides three different types of the setServer function, one which accepts an IP address based on the IPAddress data type (that's the one you're using), and two more which accept an int array and a char array, of which my code uses the latter one. So I'm really puzzled where this mixup actually takes place and why it doesn't take place on the ESP32. To test this further, could you go to PubSubClient.cpp's setServer functions and change the const char * variant to this one:
And then in
as the first lines at the beginning of the function. Take care that you pick the right connect() function, this is the last and longest one. You can remove all the previous debug messages so that it's clear which one is the one you added just now. |
One more test: Can you enable version check and check in your DNS that BSB-LAN tries to look up and connect to bsb-lan.de in order to get the current version available? This also uses the get host by name functionality that I have recently implemented with MQTT. So if this one works when looking up the version number, then there is no hardware or library reason why it shouldn't also work with MQTT... |
I have enabled version check and it seems to work (see attachment)
-> This means that DNS as such does resolve the corresponding host.
Am Mo., 1. Apr. 2024 um 17:33 Uhr schrieb fredlcore <
***@***.***>:
… One more test: Can you enable version check and check in your DNS that
BSB-LAN tries to look up and connect to bsb-lan.de in order to get the
current version available? This also uses the get host by name
functionality that I have recently implemented with MQTT. So if this one
works when looking up the version number, then there is no hardware or
library reason why it shouldn't also work with MQTT...
—
Reply to this email directly, view it on GitHub
<#623 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AS5ZPGUE72OUWDBLE6UJ4Y3Y3F46HAVCNFSM6AAAAABEADW2DKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRZHE4DEMZSHA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Mit freundlichem Gruß,
Dr. Frank Jordan
|
Thanks for testing - so this confirms that the issue is not related to any incapabilities of the Arduino Due libraries, but must happen somewhere inbetween the parsing of the hostname and the connection within PubSubClient... |
I've added the debug output as suggested (and added the name of the current method to not get confused by the output).
So the problem really lies somewhere between setting the hostname and connecting. |
Thanks, that makes it even weirder. Because the setServer output shows that both domain and port have been stored as part of the PubSubClient object. It's basically encapsulated there and should remain as is until it is overwritten by another setServer function call. So maybe you can help me see something here that I don't see in this code in question:
The first line is the setServer call which internally does this:
@tiger42's outut has shown that this works as it should.
And if your output comes right after the call to PubSubClient's Can you please try and comment out the four lines above and instead fill the |
There was some unnecessary dealings with |
I have tested the latest update and it seems to work now as expected.
Used configuration as IP with port: "192.168.1.2:1883"
Result: MQTT does connect and publish
Used configuration with host name only "mqtt.jordan.home"
Result: MQTT does connect and publish
Many thanks for your help!
Am Mo., 1. Apr. 2024 um 20:10 Uhr schrieb fredlcore <
***@***.***>:
… There was some unnecessary dealings with String instead of char arrays
which I have just fixed in the most recent master version, it compiles, but
I haven't tested whether it actually does what it should. But even if it
doesn't, it might help to figure out if the problem lies in these String
handlings...
—
Reply to this email directly, view it on GitHub
<#623 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AS5ZPGWHE32YMR5UCAIVH5LY3GPI3AVCNFSM6AAAAABEADW2DKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZQGI3TGMJVHA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Mit freundlichem Gruß,
Dr. Frank Jordan
|
I can also confirm, that the MQTT connection now works without problems. |
Thanks to both of you for testing, it's really strange that this piece of code which is around for years, suddenly now makes a problem due to some other very small and rather unrelated code... |
BSB-LAN Version
#define MAJOR "3"
#define MINOR "3"
#define PATCH "3"
#define COMPILETIME "20240227001351"
Architecture
Arduino Due
Bus system
BSB
Describe the bug
Since upgrading to the latest version (any commit after 32eeec3) the connection to my MQTT broker fails with the message "Failed to connect to MQTT broker, retrying...".
Of course I have changed the connection settings in the BSB_LAN_config.h to match the new config scheme.
Before (v3.3.2):
byte mqtt_broker_ip_addr[4] = {192,168,0,1};
After (v3.3.3):
char mqtt_broker_addr[33] = "192.168.0.1";
I've also tried to explicitly specify the port (
char mqtt_broker_addr[33] = "192.168.0.1:1883";
), but that doesn't make any difference.I've switched back and forth between the versions, and I am sure that the problem was introduced with one of the MQTT related commits on Feb 26th.
Log files - Bug reports without log files will be closed
Expected behavior
The connection should not fail.
Attach your BSB_LAN_config.h file to your bug report (remove any confidential information if necessary). Do not ZIP or otherwise compress it. Bug reports without this file attached will be closed immediately.
The text was updated successfully, but these errors were encountered: