Question

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

Posted June 19, 2020 71 views
UbuntuInitial Server Setup

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.

Submit an answer

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