Skip to main content

bedaya_mqtt 0.2.1 copy "bedaya_mqtt: ^0.2.1" to clipboard
bedaya_mqtt: ^0.2.1 copied to clipboard

Reliable MQTT for Flutter & Dart IoT apps. Auto reconnect, offline queue, typed topics, device and controller roles.

example/example.dart

// A device and a controller in one program, on a public broker.
//
//   dart run example/example.dart
//
// For two separate programs, see simulated_device.dart and controller.dart.

import 'dart:math';

import 'package:bedaya_mqtt/bedaya_mqtt.dart';

Future<void> main() async {
  // The public broker is shared, so use a prefix nobody else uses.
  final prefix = 'bedaya-example-${Random().nextInt(1 << 32)}';
  ClientConfig config() => ClientConfig(
    host: 'broker.hivemq.com',
    topicPrefix: prefix,
    retryFirstConnect: true,
  );

  // --- Device: publishes telemetry and answers commands. ---
  final fanClient = BedayaClient(config());
  // asDevice before connect: it sets the Last Will.
  final livingRoomFan = fanClient.asDevice('living-room-fan');
  await fanClient.connect();

  livingRoomFan.commands.listen((command) {
    print('device got: ${command.name} ${command.params}');
    livingRoomFan.ack(command);
  });

  // --- Controller: watches the device and sends it a command. ---
  final controllerClient = BedayaClient(config());
  await controllerClient.connect();
  final livingRoomFanRemote = controllerClient.device('living-room-fan');

  livingRoomFanRemote.status.listen(
    (s) => print('status: ${s.online ? 'online' : 'offline'}'),
  );
  final firstReading = livingRoomFanRemote.on(BedayaTopics.telemetry).first;

  await Future<void>.delayed(const Duration(milliseconds: 500));
  await livingRoomFan.publish(
    BedayaTopics.telemetry,
    const Telemetry({'temp': 24.5}),
  );
  print('controller got: ${await firstReading}');

  final ack = await livingRoomFanRemote.send(Command.relay(on: true));
  print('ack: ${ack.ok ? 'ok' : ack.error}');

  await controllerClient.close();
  await fanClient.close();
}
0
likes
160
points
546
downloads

Documentation

API reference

Publisher

verified publisherbedaya.dev

Weekly Downloads

Reliable MQTT for Flutter & Dart IoT apps. Auto reconnect, offline queue, typed topics, device and controller roles.

Repository (GitHub)
View/report issues

Topics

#mqtt #iot #esp32 #websocket #messaging

License

BSD-3-Clause (license)

Dependencies

mqtt_client, typed_data

More

Packages that depend on bedaya_mqtt