GithubHelp home page GithubHelp logo

Comments (1)

jsuofga avatar jsuofga commented on August 16, 2024

We narrowed the problem down.

It appears for some reason that the var myData does not get updated inside the client.updates.listen()

class MQTTClientWrapper extends ChangeNotifier{

MqttServerClient client;
MqttCurrentConnectionState connectionState = MqttCurrentConnectionState.IDLE;
MqttSubscriptionState subscriptionState = MqttSubscriptionState.IDLE;

var myData = 'TestValue' ; //Need to pass updates to widget, using Provider

// using async tasks, so the connection won't hinder the code flow
void prepareMqttClient() async {
_setupMqttClient();
await _connectClient();
_subscribeToTopic('Dart/Mqtt_client/testtopic');
_publishMessage('HelloWorld');
}

// waiting for the connection, if an error occurs, print it and disconnect
Future _connectClient() async {
try {
print('client connecting....');
connectionState = MqttCurrentConnectionState.CONNECTING;
await client.connect('octava', 'mqtt.7078');
} on Exception catch (e) {
print('client exception - $e');
connectionState = MqttCurrentConnectionState.ERROR_WHEN_CONNECTING;
client.disconnect();
}

// when connected, print a confirmation, else print an error
if (client.connectionStatus.state == MqttConnectionState.connected) {
  connectionState = MqttCurrentConnectionState.CONNECTED;
  print('client connected');

} else {
  print(
      'ERROR client connection failed - disconnecting, status is ${client.connectionStatus}');
  connectionState = MqttCurrentConnectionState.ERROR_WHEN_CONNECTING;
  client.disconnect();
}

}

void _setupMqttClient() {
client = MqttServerClient.withPort('5f06cdd1ba6f41858989ad188f4478bb.s2.eu.hivemq.cloud', 'octavaClient', 8883);
// the next 2 lines are necessary to connect with tls, which is used by HiveMQ Cloud
client.secure = true;
client.securityContext = SecurityContext.defaultContext;
client.keepAlivePeriod = 20;
client.onDisconnected = _onDisconnected;
client.onConnected = _onConnected;
client.onSubscribed = _onSubscribed;
}

void _subscribeToTopic(String topicName) {
print('Subscribing to the $topicName topic');
client.subscribe(topicName, MqttQos.atMostOnce);

// print the message when it is received
client.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
  final MqttPublishMessage recMess = c[0].payload;
  var message = MqttPublishPayload.bytesToStringAsString(recMess.payload.message);

  print('YOU GOT A NEW MESSAGE:');

  print(message);  // Displays message correctly
  myData = message;
  print(myData);
  // notifyListeners();
});

from mqtt.dart-client-example.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.