mirror of
https://bitbucket.org/myhomie/myhomie_app.git
synced 2025-12-06 00:51:19 +00:00
Mqtt client test - local connexion
This commit is contained in:
parent
b60fbceece
commit
67ff05e28b
@ -1,4 +1,8 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mqtt_client/mqtt_client.dart';
|
||||
import 'package:mqtt_client/mqtt_server_client.dart';
|
||||
|
||||
import 'constants.dart';
|
||||
|
||||
@ -26,16 +30,17 @@ class MyApp extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: 'MyHomie App Demo',
|
||||
initialRoute: widget.initialRoute,
|
||||
supportedLocales: [
|
||||
/*supportedLocales: [
|
||||
const Locale('en', 'US'),
|
||||
const Locale('fr', 'FR'),
|
||||
],
|
||||
//const Locale('fr', 'FR'),
|
||||
],*/
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
scaffoldBackgroundColor: kBackgroundColor,
|
||||
@ -61,15 +66,91 @@ class MyHomePage extends StatefulWidget {
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
int _counter = 0;
|
||||
final MqttServerClient client = MqttServerClient.withPort('192.168.31.140', 'flutter_client00', 1883); //192.168.31.140
|
||||
|
||||
void _incrementCounter() {
|
||||
setState(() {
|
||||
_counter++;
|
||||
|
||||
const pubTopic = 'topic/test';
|
||||
final builder = MqttClientPayloadBuilder();
|
||||
builder.addString('Hello MQTT');
|
||||
client.publishMessage(pubTopic, MqttQos.atLeastOnce, builder.payload);
|
||||
});
|
||||
}
|
||||
|
||||
// connection succeeded
|
||||
void onConnected() {
|
||||
print('Connected !!!!!!!!!!!! ----------------------------------');
|
||||
|
||||
client.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
|
||||
final MqttPublishMessage message = c[0].payload;
|
||||
final payload = MqttPublishPayload.bytesToStringAsString(message.payload.message);
|
||||
print('Received message:$payload from topic: ${c[0].topic}>');
|
||||
});
|
||||
}
|
||||
|
||||
// unconnected
|
||||
void onDisconnected() {
|
||||
print('Disconnected');
|
||||
}
|
||||
|
||||
// subscribe to topic succeeded
|
||||
void onSubscribed(String topic) {
|
||||
print('Subscribed topic: $topic');
|
||||
}
|
||||
|
||||
// subscribe to topic failed
|
||||
void onSubscribeFail(String topic) {
|
||||
print('Failed to subscribe $topic');
|
||||
}
|
||||
|
||||
// unsubscribe succeeded
|
||||
void onUnsubscribed(String topic) {
|
||||
print('Unsubscribed topic: $topic');
|
||||
}
|
||||
|
||||
Future<MqttServerClient> connect() async {
|
||||
client.logging(on: false);
|
||||
client.keepAlivePeriod = 20;
|
||||
client.onDisconnected = onDisconnected;
|
||||
client.onConnected = onConnected;
|
||||
client.onSubscribed = onSubscribed;
|
||||
|
||||
/// Security context
|
||||
/*SecurityContext context = new SecurityContext()
|
||||
..useCertificateChain('path/to/my_cert.pem')
|
||||
..usePrivateKey('path/to/my_key.pem', password: 'key_password')
|
||||
..setClientAuthorities('path/to/client.crt', password: 'password');*/
|
||||
//client.setProtocolV31();
|
||||
//client.secure = true;
|
||||
//client.securityContext = context;
|
||||
|
||||
final connMessage = MqttConnectMessage()
|
||||
/*.authenticateAs('thomas', 'MyCore,1')
|
||||
.keepAliveFor(60)
|
||||
.withWillTopic('willtopic')
|
||||
.withWillMessage('Will message')*/
|
||||
.withClientIdentifier("TESSST")
|
||||
.startClean();
|
||||
//.withWillQos(MqttQos.atLeastOnce);
|
||||
//client.secure = true;
|
||||
client.connectionMessage = connMessage;
|
||||
try {
|
||||
await client.connect();
|
||||
} catch (e) {
|
||||
print('Exception: $e');
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
client.subscribe("#", MqttQos.atLeastOnce);
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
connect();
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.title),
|
||||
|
||||
28
pubspec.lock
28
pubspec.lock
@ -43,6 +43,20 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.15.0-nullsafety.4"
|
||||
convert:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: convert
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.1"
|
||||
crypto:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: crypto
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.5"
|
||||
cupertino_icons:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@ -50,6 +64,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.0.0"
|
||||
event_bus:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: event_bus
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.1"
|
||||
fake_async:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -81,6 +102,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.3.0-nullsafety.5"
|
||||
mqtt_client:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: mqtt_client
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "8.1.0"
|
||||
path:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@ -24,6 +24,8 @@ dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
mqtt_client: ^8.1.0
|
||||
|
||||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user