Report this

What is the reason for this report?

How to allow remote connection of Paho MQTT client to Mosquitto MQTT broker hosted on Digital Ocean

Posted on July 1, 2020

I have hosted the mosquitto MQTT broker on Digital Ocean Cloud following this tutorial. (https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-ubuntu-16-04). I tried to connect it to the Paho MQTT Android Client to which it could not connect. To debug I removed the SSL certificates that were added to the broker through letsencrypt but still it could not connect.This is my /etc/mosquitto/conf.d/default.conf file. I have also allowed firewall for port 1883.

allow_anonymous true
 password_file /etc/mosquitto/passwd


 listener 1883
 protocol mqtt

Here is my Android code:

public class MainActivity extends AppCompatActivity {

public MqttAndroidClient CLIENT;
public MqttConnectOptions MQTT_CONNECTION_OPTIONS;


@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String clientId = MqttClient.generateClientId();
    MqttAndroidClient client =
            new MqttAndroidClient(this.getApplicationContext(), 
                                 "tcp://192.34.63.138:1883",
                                  clientId);

    try {
        IMqttToken token = client.connect();
        token.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken ) {
                // We are connected
                Log.d("mqtt", "connected, token:" );
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable 
                                  exception) {
                // Something went wrong e.g. connection timeout or 
                   firewall problems
                Log.d("mqtt", "not connected" + exception.toString());

            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }

}
}

These are the error I am getting in android:

D/AlarmPingSender: Unregister alarmreceiver to
MqttServicepaho16008840983860
2020-06-18 21:44:29.316 9084-9115/com.example.testmqtt D/AlarmPingSender:
Unregister alarmreceiver to MqttServicepaho16017475349560
2020-06-18 21:44:51.127 9084-9118/com.example.testmqtt D/AlarmPingSender:
Unregister alarmreceiver to MqttServicepaho16008840983860
2020-06-18 21:44:51.145 9084-9084/com.example.testmqtt D/mqtt: not
connectedMqttException (0) - java.net.SocketTimeoutException: connect timed
out

Please guide me on how I should try to solve this error. I will be grateful for your guidance.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

The Android log suggests a connection timeout issue. This may be caused by one of the following issues:

  1. Wrong MQTT Broker Address: Ensure that the IP address of your MQTT broker that you are using in the Android app is correct and reachable from the network where your Android device is connected. Use ping <IP_Address> command on your system terminal to verify the connection.

  2. Firewall Settings on the Broker side: You mentioned that you allowed the port 1883 on the firewall, but you may need to verify this. If you are using ufw firewall on the Ubuntu server, you can check the firewall status using sudo ufw status command. The firewall should allow incoming traffic on port 1883.

  3. Port not correctly configured in MQTT broker: Even though your Mosquitto configuration seems fine, you may want to double-check if Mosquitto is correctly running and listening on the port 1883. You can use netstat -tuln | grep 1883 command on your broker’s system terminal to verify this.

  4. Network Issue on Android side: There could be restrictions on the network that your Android device is connected to. Some networks may block outbound connections to certain ports. Test the connection with another network if possible.

  5. SSL/TLS Issue: If you are using SSL/TLS in the Mosquitto broker, you should use ssl:// instead of tcp:// in the server URI in the Android client. However, you mentioned that you have removed the SSL certificates for debugging. If you haven’t reconfigured Mosquitto to accept non-secure connections, this could be the issue.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.