From d4ed804624210601e1b8179628bbb3e5e0785a26 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Tue, 5 Jan 2021 21:49:28 +0100 Subject: [PATCH] Add online and secured mqtt connexion --- lib/main.dart | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 92e29c4..4af8f9c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -66,16 +66,20 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { int _counter = 0; - final MqttServerClient client = MqttServerClient.withPort('192.168.31.140', 'flutter_client00', 1883); //192.168.31.140 + //final MqttServerClient client = MqttServerClient.withPort('192.168.31.140', 'flutter_client00', 1883); + final MqttServerClient client = MqttServerClient.withPort('myhomie.be', 'flutter_client00', 1883); // TODO ONLINE void _incrementCounter() { setState(() { _counter++; - const pubTopic = 'topic/test'; - final builder = MqttClientPayloadBuilder(); - builder.addString('Hello MQTT'); - client.publishMessage(pubTopic, MqttQos.atLeastOnce, builder.payload); + print("client.connectionStatus !!! ==" + client.connectionStatus.toString()); + if (client.connectionStatus.state == MqttConnectionState.connected) { + const pubTopic = 'topic/test'; + final builder = MqttClientPayloadBuilder(); + builder.addString('Hello MQTT'); + client.publishMessage(pubTopic, MqttQos.atLeastOnce, builder.payload); + } }); } @@ -127,8 +131,8 @@ class _MyHomePageState extends State { //client.securityContext = context; final connMessage = MqttConnectMessage() - /*.authenticateAs('thomas', 'MyCore,1') - .keepAliveFor(60) + .authenticateAs('thomas', 'MyCore,1') // TODO ONLINE + /*.keepAliveFor(60) .withWillTopic('willtopic') .withWillMessage('Will message')*/ .withClientIdentifier("TESSST") @@ -136,6 +140,7 @@ class _MyHomePageState extends State { //.withWillQos(MqttQos.atLeastOnce); //client.secure = true; client.connectionMessage = connMessage; + client.autoReconnect = true; try { await client.connect(); } catch (e) {