flespi.io uses an auth token as a username, you can set the token as password also, if you want. How to connect to flespi using HiveMQ Java implementation?
Mqtt3AsyncClient client = MqttClient.builder()
.useMqttVersion3()
.identifier(UUID.randomUUID().toString())
.simpleAuth()
.username(mqttClientConfig.getUser())
.password(mqttClientConfig.getPassword().getBytes()).applySimpleAuth()
.serverHost(mqttClientConfig.getBrokerHost())
.serverPort(mqttClientConfig.getBrokerPort())
.buildAsync();
client.connect()
.whenComplete((connAck, throwable) -> {
if (throwable != null) {
logger.error("MQTT connection error: ", throwable);
} else {
client.publishWith()
.topic(mqttClientConfig.getPublishTopic())
.payload(payload.getBytes())
.qos(MqttQos.fromCode(mqttClientConfig.getQos()))
.send()
.whenComplete((mqtt3Publish, subThrowable) -> {
if (subThrowable != null) {
logger.error("MQTT subscripton error: ", subThrowable);
} else {
logger.info("Published on: {}", mqttClientConfig.getTopic());
}
});
client.disconnect();
}
});
I get the error:
INFO n.k.s.mqtt.HiveMqttClient - Connecting to mqtt.flespi.io:8883...
ERROR n.k.s.mqtt.HiveMqttClient - MQTT connection error:
com.hivemq.client.mqtt.exceptions.ConnectionClosedException: Server closed connection without DISCONNECT.
Port 8883 is normally for MQTT over TLS
If so then you need to add something like the following to the builder
.sslWithDefaultConfig()
See the HiveMQ tutorial here