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 June 19, 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 error message suggests that the Android client is unable to establish a connection to your MQTT broker. This could be caused by one of several issues. Let’s take a look at some potential solutions:

  1. Firewall or Networking Issues: Verify that your DigitalOcean droplet’s firewall settings allow inbound connections on port 1883. If you’re using UFW to manage your firewall, you can check your firewall status using the command sudo ufw status. To allow connections on port 1883, use the command sudo ufw allow 1883. If you’re using DigitalOcean’s Cloud Firewalls, ensure that your rules permit inbound traffic on port 1883.

  2. Mosquitto Broker Configuration: Check the Mosquitto broker’s log files for any issues. By default, these logs are stored in /var/log/mosquitto/mosquitto.log. You can view the log using tail -f /var/log/mosquitto/mosquitto.log and see if there are any issues when you try to connect from your Android client.

  3. Incorrect Server IP: You mentioned that you’ve hosted the Mosquitto broker on DigitalOcean Cloud, but you’re using the IP address 192.34.63.138 in your Android client. This is a private IP address, and it’s not likely to be the correct IP address for your DigitalOcean droplet. You should replace this with the public IP address of your droplet.

  4. Network Access: Make sure your Android device has access to the Internet and is able to reach your Mosquitto server. You can check this by pinging the IP address of your server from your Android device. There are apps available on the Google Play Store for this purpose, such as PingTools Network Utilities.

  5. App Permissions: Make sure your Android app has the necessary permissions to access the Internet. In your AndroidManifest.xml file, you should have the line <uses-permission android:name="android.permission.INTERNET" />.

If all of the above steps are correct, then it’s possible there might be an issue with the way you’re using the Paho MQTT client. If this is the case, I would recommend looking at the documentation or other examples of how to use the Paho MQTT client to see if you’re missing anything.

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.