Open talhaakca opened 2 years ago
@talhaakca In the MQTTManager below function uncomment //.authenticateAs(username, password)// Non persistent session for testing
... that should be enough
` final MqttConnectMessage connMess = MqttConnectMessage()
.withClientIdentifier(_identifier)
.withWillTopic(
'willtopic') // If you set this you must set a will message
.withWillMessage('My Will message')
.startClean() // Non persistent session for testing
//.authenticateAs(username, password)// Non persistent session for testing
.withWillQos(MqttQos.atLeastOnce);
print('EXAMPLE::Mosquitto client connecting....');
_client!.connectionMessage = connMess;
}
I have enabled the comment lines as you said.
but getting error on this line. Username and password are not defined : .authenticateAs(username, password)// Non persistent session for testing
I defined this in mqttmanager.dart
. but how to get this information from user in setting_screen
? can you help
I'm very inexperienced.
then i need to make on/off button. I have to send 1 and 0 in the button. but first you need to provide username and encrypted connection
@talhaakca You need to pass the user name and password as additional parameters to initializeMQTTClient
function and use those values in .authenticateAs(username, password)
void initializeMQTTClient({
required String host,
required String identifier,
required String username,
required String password,
}
Is it possible to explain in more detail?
I can't see where am I doing wrong?
... +
class _SettingsScreenState extends State<SettingsScreen> {
final TextEditingController _hostTextController = TextEditingController();
final TextEditingController _userTextController = TextEditingController();
final TextEditingController _passTextController = TextEditingController();
late MQTTManager _manager;
@override
void dispose() {
_hostTextController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
_manager = Provider.of<MQTTManager>(context);
return Scaffold(
appBar: _buildAppBar(context) as PreferredSizeWidget?,
body: _manager.currentState == null
? CircularProgressIndicator()
: _buildColumn(_manager));
}
Widget _buildAppBar(BuildContext context) {
return AppBar(
title: const Text('Settings'),
backgroundColor: Colors.greenAccent,
);
}
Widget _buildColumn(MQTTManager manager) {
return Column(
children: <Widget>[
StatusBar(
statusMessage: prepareStateMessageFrom(
manager.currentState.getAppConnectionState)),
_buildEditableColumn(manager.currentState),
],
);
}
Widget _buildEditableColumn(MQTTAppState currentAppState) {
return Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
_buildTextFieldWith(_hostTextController, 'server adresini giriniz..',
currentAppState.getAppConnectionState),
_buildTextFieldWith(_userTextController, 'Kullanıcı adını giriniz..',
currentAppState.getAppConnectionState),
_buildTextFieldWith(_passTextController, 'şifrenizi giriniz..',
currentAppState.getAppConnectionState),
const SizedBox(height: 10),
_buildConnecteButtonFrom(currentAppState.getAppConnectionState)
],
),
);
}
Widget _buildTextFieldWith(TextEditingController controller, String hintText,
MQTTAppConnectionState state) {
bool shouldEnable = false;
if ((controller == _hostTextController &&
state == MQTTAppConnectionState.disconnected)) {
shouldEnable = true;
} else if (controller == _hostTextController && _manager.host != null) {
_hostTextController.text = _manager.host!;
}
return TextField(
enabled: shouldEnable,
controller: controller,
decoration: InputDecoration(
contentPadding:
const EdgeInsets.only(left: 0, bottom: 0, top: 0, right: 0),
labelText: hintText,
));
}
Widget _buildConnecteButtonFrom(MQTTAppConnectionState state) {
return Row(
children: <Widget>[
Expanded(
child: RaisedButton(
color: Colors.lightBlueAccent,
child: const Text('Connect'),
onPressed: state == MQTTAppConnectionState.disconnected
? _configureAndConnect
: null, //
),
),
const SizedBox(width: 10),
Expanded(
child: RaisedButton(
color: Colors.redAccent,
child: const Text('Disconnect'),
onPressed: state != MQTTAppConnectionState.disconnected
? _disconnect
: null, //
),
),
],
);
}
void _configureAndConnect() {
// TODO: Use UUID
String osPrefix = 'Flutter_iOS';
if (Platform.isAndroid) {
osPrefix = 'Flutter_Android';
}
_manager.initializeMQTTClient(
host: _hostTextController.text,
identifier: osPrefix,
username: _userTextController.text,
password: _passTextController.text
);
_manager.connect();
}
void _disconnect() {
_manager.disconnect();
}
}
in this way, text entries are closed; https://imgur.com/qZJZ63Z
I can't find what to do. I am not getting any errors either. @anoop4real
@talhaakca
Right now in the condition we are checking only for _hostTextController
for all other fields the value shouldEnable is false;
If you want those fields to be enabled always then add in _buildTextFieldWith
if ((controller == _hostTextController &&
state == MQTTAppConnectionState.disconnected)) {
shouldEnable = true;
} else if (controller == _hostTextController && _manager.host != null) {
_hostTextController.text = _manager.host!;
} else {
// Add this to enabled username and password.
shouldEnable = true;
}
How to request username and password information from the user while connecting to mqtt broker?