Closed PietroMB closed 3 years ago
Your failing to connect because the broker is sending you a not authorized reply in the conn ack message. Although the code sample above does this -
wait client.connect("myUser", "some-pass");
From the log you are actually sending -
I/flutter ( 9336): 2020-10-18 21:23:22.960963 -- Authenticating with username '{user}' and password '{some-password}'
i.e. the password is 'some-password', not 'some-pass' and the user name is 'user', not 'myUser'.
Note you don't have to pass auth parameters on both the connect call and the authenticateAs method on the connect message, one or the other will do.
Sorry, I've not made a good job hiding my credential uniformly, ofc in the connection strings I've putted the correct ones.
I've tryed this way
final connMess = MqttConnectMessage()
//.authenticateAs("myUser", "some-password")
.withClientIdentifier('sma')
.keepAliveFor(20) // Must agree with the keep alive set above or not set
.startClean() // Non persistent session for testing
.withWillQos(MqttQos.atMostOnce);
print('EXAMPLE::Mosquitto client connecting....');
client.connectionMessage = connMess;
try {
await client.connect("myUser", "some-password");
} on Exception catch (e) {
print('######EXAMPLE::client exception - $e');
client.disconnect();
}
and this way, but same errors
Future<int> main() async {
final client = MqttServerClient('192.168.1.2', 'sma');
client.logging(on: true);
client.keepAlivePeriod = 20;
client.onDisconnected = onDisconnected;
client.onSubscribed = onSubscribed;
final connMess = MqttConnectMessage()
.authenticateAs("myUser", "some-password")
.withClientIdentifier('sma')
.keepAliveFor(20) // Must agree with the keep alive set above or not set
.startClean() // Non persistent session for testing
.withWillQos(MqttQos.atMostOnce);
print('EXAMPLE::Mosquitto client connecting....');
client.connectionMessage = connMess;
try {
await client.connect();
} on Exception catch (e) {
print('######EXAMPLE::client exception - $e');
client.disconnect();
}
OK, you are getting this in your conn ack message -
I/flutter ( 9336): 2020-10-18 21:23:48.058776 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code of notAuthorized and a disconnection origin of none
So your broker is not authorizing you, you need to look at your broker logs to see why this is, the client can't help you much more here.
This is the server log
1603100496: New connection from 192.168.1.7 on port 1883. [WARN] Not found myUser on local database 1603100497: Socket error on client
, disconnecting. 1603100497: New connection from 192.168.1.7 on port 1883. 1603100497: Socket error on client , disconnecting. 1603100497: New connection from 192.168.1.7 on port 1883. 1603100497: Socket error on client , disconnecting.
I don't know why the warning, I uses this username in all IoT devices and they works
Well, it doesn't like something, the user name is correct I assume as 'myUser', there's no trailing or leading whitespace we can't see is there? What broker are you using BTW?
No whitespace in username, I use Mosquitto broker as docker bridged
Oh no, I'm really really sorry, closing " I accidentally pressed a key in the password string,(my password was generated by a password manager) and I didn't notice the extra character in the review. Now it works
Hi, I've an issues with this code, I use MQTT Explorer with the same credential and same port and it works, someone can help me?