B4J Question "Wait for" statement timeout with MQTT

Similar threads

B4J Tutorial [IoT] MQTT Protocol
B4J Library MqttBroker
B4A Tutorial [B4X] MQTT Chat Room
B4A Tutorial [B4X] MQTT - Connect & Reconnect
B4A Code Snippet [B4X] MQTT SSL and Self Signed Certificates

Amin Ismail

Active Member
Licensed User
I have been using the following function (posted elsewhere in the forum) without any problems. However at one particular site where my application is installed, when the application is started after power up, it will hang at the "wait for" statement. The log will show "Trying to Connect" and the application get "hung" at the "wait for" statement making it unresponsive. It's not a network issue because I checked connectivity and even if it were, the program should not get hung up. Just wondering how that can happen. Is there a timeout built into the "wait for"?

B4X:
Sub ConnectAndReconnect
    Do While working
        If mqtt.IsInitialized Then mqtt.Close
        mqtt.Initialize("mqtt", server, clientID)
        Dim mo As MqttConnectOptions
        mo.Initialize(user, password)
        Log("Trying to connect")
        mqtt.Connect2(mo)
        Wait For mqtt_Connected (Success As Boolean)
        If Success Then
            Log("Mqtt connected")
            mqtt.Subscribe(mytopic,0)
            Do While working And mqtt.Connected
                mqtt.Publish2("ping", Array As Byte(0), 1, False) 'change the ping topic as needed
                Sleep(30000)
            Loop
            Log("Disconnected")
        Else
            Log("Error connecting.")
        End If
        Sleep(30000)
    Loop
End Sub
 

Amin Ismail

Active Member
Licensed User
Actually I should have said the MQTT connection does not take place and not that the application seemed unresponsive. I can connect to the MQTT server with another MQTT client so the server is not the issue. So I put the three Log statements in the function to figure out why. It seems to me like the "wait-for" is not timing out since I get the "Trying to connect" message, but I never get the "Mqtt connected" or the "Error Connecting" message.
 

Amin Ismail

Active Member
Licensed User
Ok.. so that makes sense and explains why I never see the other messages.
Yes, I am absolutely sure that ConnectAndReconnect is being called only once in AppStart.
The strange thing about this case is that the code works perfectly at home and everywhere else. But just at this one location, I have this issue when I power up the computer. To get it to work, I have to bring up Chromium first (it is running on a Raspberry Pi Zero W), connect to a web site like google, then bring up the application and it works. So it appears to be a network issue but what puzzled me was that if it were a network issue it should have returned with a "Error Connecting" message in the Log. Instead it just seemed to "hang" in the Wait-for statement.
Anyway, thanks for your help. I got the information I needed. I'm going to implement a work-around for now which I think should work.
 

Michael1968

Active Member
Licensed User
Hi Amin,
do you try to connect to the AdafruitIO Server ?
How do you init : mqtt.Initialize("mqtt", server, clientID) ?

I have the same issue....my programm works fine on PC and Laptop but with the Pi there was a Connecting error.
change connecting from SSL port 8883 to TCP port 1883 solved my problem.

there must be something wrong with the ssl connect from the RPI

Best regards
Michael
 

Amin Ismail

Active Member
Licensed User
Thank you for you suggestion, Michael. However I am not using the Adafruit Server but using my own Mosquitto Server configured for Normal TCP on 1883 (no TLS/SSL). I'm running a B4J GUI App on a headless RPi-ZeroW connected to a big screen TV. It displays the real-time temperatures from 3 swimming pools here at our YMCA Aquatic Center in Orlando. I also have a B4A and B4I App that does the same thing and has pretty much the same code. It appears to be a network issue with the WiFi connection at the YMCA since the B4J App works perfectly at my home and office …. i.e. when the Pi is powered up, the connection to the MQTT server takes place automatically. However only at the Y does this not happen unless you first bring up a web site on the browser. I've implemented a "clumsy" workaround now and all is well. But regardless, what is driving me nuts was why the "wait-for" never comes back with the "success" flag set/reset. The mobile Apps don't have this issue. I simulated a failed network connection at home and the "wait-for" returned a failure as it should, so if there was an issue with the Y's network I should have at least got a "Error Connecting" message in the Log - but I didn't. Anyway, if I solve the mystery, I'll be sure to update this post.
Thanks again!
Best
Amin
 
Last edited:

techknight

Well-Known Member
Licensed User
You wont get an error connecting if the firewall/proxy service is configured as a black hole. Basically, instead of TCP reporting Closed/No Service, it doesnt report anything, no NACK, no ACK, nothing.

This configuration is alot more common than you think. From the outside, PFSense does this automatically as a standard configuration when you port scan from the outside.

However, from the inside, most people use McAfee Web Gateway when it comes to schools anyways.
 

Amin Ismail

Active Member
Licensed User
I think you hit the nail on the head. That probably explains why I have to bring up the browser and connect to a web site before my application will connect to the MQTT server.

My work-around is to externally bring up the chromium Browser on boot and then launch my application after a delay. This works fine but in my opinion is a clumsy way of handling this issue. Also, using HTTPUtils within the application to connect to a web site first does not solve the issue - it will connect to the web site but not to the MQTT server. Is there any way to programmatically handle a situation like this?


Thanks!
 
Last edited:

OliverA

Expert
Licensed User
However only at the Y does this not happen unless you first bring up a web site on the browser.
It almost sounds like an authentication issue. Does a user at the Y first have to go through a portal to get WiFi access? Could it be that the Chrome browser has a cookie stored that allows it to access (and therefore the computer) the WiFi network of the Y?
 

Amin Ismail

Active Member
Licensed User
It almost sounds like an authentication issue. Does a user at the Y first have to go through a portal to get WiFi access? Could it be that the Chrome browser has a cookie stored that allows it to access (and therefore the computer) the WiFi network of the Y?
No, however I see what you are saying. There is no portal. Actually there are two WiFi connections there - An open one and a closed one (requiring a password) - This problem happens on both connections, so I think techknight is right about the proxy service. And as you say, running Chromium probably kick-starts the connection via a cookie or something.
Thanks for your suggestion.
 

techknight

Well-Known Member
Licensed User
A Captive Portal setup can behave in similar ways, But the end result is a Proxy Server in either case.

To fix this problem, the network administrator must whitelist the MAC Address of your device on the network to prevent this from happening, or give you a static IP address for you to assign to the device that has clearance.

I ran into a websockets issue as well, By Default most proxy servers like McAfee have Websockets disabled by default, and have to be cleared on a case by case basis. I ran into that trap a couple months ago with an LED Sign.
 
Top