MQQT connection issues

I have verified the server as I am connecting to it from an embedded system as well as a data collection device running on a linux based platform. The MQTT does not connect and generates an error message. I have included a dump of the output

The thread ‘’ (0x2) has exited with code 0 (0x0).
WIFIMAC: F8.F0.05.7A.77.C2
WIFIADDR:0.0.0.0
WIFI Connected!
WIFIADDR:10.0.0.72
Connecting to the MQTT server…
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (3) ####
#### Message:
#### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::GetHostByName [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 000b] ####
#### GHIElectronics.TinyCLR.Networking.Mqtt.MqttStream::.ctor [IP: 000d] ####
#### GHIElectronics.TinyCLR.Networking.Mqtt.Mqtt::.ctor [IP: 0065] ####
#### MqttDemo.MqttHandler::.ctor [IP: 005e] ####
#### MqttDemo.Program::NetworkWifiAddressHandler [IP: 0074] ####
#### MqttDemo.NetworkWifi::NetworkController_NetworkAddressChanged [IP: 0010] ####
#### GHIElectronics.TinyCLR.Devices.Network.NetworkController::OnNetworkAddressChanged [IP: 000e] ####
#### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::<.ctor>b__8_1 [IP: 001f] ####
#### GHIElectronics.TinyCLR.Native.NativeEventHandler::Invoke [IP: 0000] ####
#### GHIElectronics.TinyCLR.Native.NativeEventDispatcher::MultiCastCase [IP: 0015] ####
Exception thrown: ‘System.InvalidOperationException’ in GHIElectronics.TinyCLR.Devices.Network.dll
Error on MQTT connection!
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (1) ####
#### Message:
#### MqttDemo.MqttHandler::IsConnected [IP: 0008] ####
Exception thrown: ‘System.NullReferenceException’ in MqttDemo.exe
An unhandled exception of type ‘System.NullReferenceException’ occurred in MqttDemo.exe

The program ‘[13] TinyCLR application: Managed’ has exited with code 0 (0x0).

Hello wbasser,

The early part of the stack trace here suggests that your client app has no idea how to resolve the MQTT server’s address e.g, the “GetHostByName” referenced in the Message header.

So it appears that you may have put something along the lines of server=“my.mqttserver.net” or some such in your code, but the client has no idea how to resolve it. Check that DNS knows how to get to your named MQTT server, and also check that you have your network client setup with a DNS address.

If my guesses are out in left field, well, it’s because without code to look at, it’s just that: Guessing. :slight_smile:

I have set the DNS address in the settings. How can I send you the code?

you don’t send code - just create a small enough reference program to repro the error and post it here. Or if it’s already publically available (github repo for example) then post us the link there

This is the public link to the github repository

Ok, so I downloaded and ran the code - it appears to work at my end. There are no exceptions thrown and outside of the application, I’m able to ping the MQTT server listed in the code as well.

Of note, what I don’t see is a debug output of “MQTT Connection changed!” as I would expect from the handler assignment. As you can see from my debug, the code makes it to the flagMqttConnected = true statement without error, however, it takes about a minute for the code to get that far which is in my book a sign of problems.

What we can confirm:

  1. The MQTT server name is being resolved.
  2. A connection is at least being attempted.

What we don’t know:

  1. Is it my LAN Router resolving the MQTT server or is it the settings on the FEZ Duino? (No, I don’t want to set my router to the different DNS as it will disrupt a few other things on my network.)
  2. Is the username/pass correct for the test session?
  3. Is it finally connecting at some point (I quit waiting after about 10 mins.)

So from your original exception, two things are needed to clear up:

  1. Change your DNS primary and secondary to something more common - those 75.75.75.75 and .76 pairs are at least here in the US, off the beaten path as it were.

  2. Confirm your own router/gateway that you’re working from can ping the named MQTT server. You should open a CMD prompt and type:
    nslookup killerbunnies.strongarmtech.io

Make sure that the server can be found at your end (its found at mine.)

Good luck sir!

Screen shots:
(Code execution made it to and waiting at line 76)

(Debug output - the Mqtt connection changed statement is never output.)

1 Like

As far as the MQTT server credentials, I have three other devices, 2 RaspberryPI based and 1 bare metal, all three have no issue with connecting and transferring data. I’ll play around with this some more and see if I can figure out what is going on. I’m going to run wireshark next to sniff out the transactions.

So I added code to verify address by manually looking up the address, it was there. No luck. Modified code to use Adafruit.IO as the source, downloaded the certificate, changed the code to support the same and still no connection. Code is modeled after your app note and C# for IOT and Robotics PDF book.

Same error, on trying to connect.

Hi, we added the issue here and verify soon.

Check MQTT · Issue #1234 · ghi-electronics/TinyCLR-Libraries (github.com)

Hi, we ran your code, similar to @ Redgum, we have no problem with Dns.GetHostEntry.

Further, we had problem with

lclMqttHandler = new MqttHandler(mqttClientId, mqttUrl, mqttPort, mqttName, mqttPass, mqttCert);

That because the certificate you are using is not the root as TinyCLR required. We change ceritificate and seems good to us.

ReturnCode is 0 mean connection accepted.

Let us know anything else we can help.

The original problem was created when I attempted to connect to test.mosquitto.org. No password, user ID and certificate is required. This is the error log:
WIFIMAC: F8.F0.05.7A.77.C2
WIFI Connected!
WIFIADDR:10.0.0.72
WIFIDNS:75.75.75.75
MQTT Server:91.121.93.94
Connecting to the MQTT server…
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (3) ####
#### Message:
#### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::AuthenticateAsClient [IP: 0000] ####
#### System.Net.Security.SslStream::AuthenticateAsClient [IP: 0016] ####
#### System.Net.Security.SslStream::AuthenticateAsClient [IP: 0009] ####
#### GHIElectronics.TinyCLR.Networking.Mqtt.MqttStream::Connect [IP: 0044] ####
#### GHIElectronics.TinyCLR.Networking.Mqtt.Mqtt::Connect [IP: 0086] ####
#### MqttDemo.MqttHandler::.ctor [IP: 00a6] ####
#### MqttDemo.Program::NetworkWifiAddressHandler [IP: 00e5] ####
#### MqttDemo.NetworkWifi::NetworkController_NetworkAddressChanged [IP: 0010] ####
#### GHIElectronics.TinyCLR.Devices.Network.NetworkController::OnNetworkAddressChanged [IP: 000e] ####
#### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::<.ctor>b__8_1 [IP: 001f] ####
#### Exception System.Exception - 0x00000000 (3) ####
#### Message: Connecting failed
#### GHIElectronics.TinyCLR.Networking.Mqtt.Mqtt::Connect [IP: 008f] ####
#### MqttDemo.MqttHandler::.ctor [IP: 00a6] ####
#### MqttDemo.Program::NetworkWifiAddressHandler [IP: 00e5] ####
#### MqttDemo.NetworkWifi::NetworkController_NetworkAddressChanged [IP: 0010] ####
#### GHIElectronics.TinyCLR.Devices.Network.NetworkController::OnNetworkAddressChanged [IP: 000e] ####
#### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::<.ctor>b__8_1 [IP: 001f] ####
Exception thrown: ‘System.Exception’ in GHIElectronics.TinyCLR.Networking.Mqtt.dll
Error on MQTT connection-Connecting failed

As you can see, there is still an error. To re-iterate, a simple C prgram running on a raspberry PI zero accessing the same MQTT server has no issue.

The project and source code is available in this github repository

mosquitto-server branch

we will check again!

We tried again with your new project, and it works just fine. Of course, we need to use root certificate from “io.adafruit.com”. Your certificate won’t work, that why you see the error. TinyCLR needs root certificate. You may need to look at this to see how to download root certificate: TLS Client (ghielectronics.com)
or we can send you the one that we use to your project it work.
Below is connect result, it has no exception, return code 0 mean successful.