shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

Connect Google IOT mqtt demo #136

Closed bhagujava closed 4 years ago

bhagujava commented 5 years ago

I am newbie on google IOT. do you have any documentation to connect Google IOT mqtt client using private key and pem file ?. I tried https://github.com/BitKnitting/flutter_adafruit_mqtt but it has half information and the jsonfile expecting username and key which i don't have.

I have folowing params to connect 1) private key 2) client_id 3) client_x509_cert_url 4) auth_provider_x509_cert_url 5) auth_uri 6) private_key_id 7) project_id

as long as i know mqtt is protocol which follows the certain standard. Looking for to connect mqtt client using above parameter.

DhavalRKansara commented 5 years ago

I am also facing the same issue.

shamblett commented 5 years ago

Ok, yes I have some examples, I use Googles cloud IOT core and have sent some sensor data to it using the client, your first port of call is the iot_core.dart file in the examples directory of the client, this shows what you need to do to connect to IOT core, there's also this project I've put together, this does the same thing but is a bit more real in that it generates the JWT token, connects to IOT core and sends some data, there's a write up for it here.

bhagujava commented 4 years ago

Ok, yes I have some examples, I use Googles cloud IOT core and have sent some sensor data to it using the client, your first port of call is the iot_core.dart file in the examples directory of the client, this shows what you need to do to connect to IOT core, there's also this project I've put together, this does the same thing but is a bit more real in that it generates the JWT token, connects to IOT core and sends some data, there's a write up for it here.

@shamblett Thanks for reply. I have seen your code but sound confused me. I am looking straightforward integration with the parameter which i have mentioned above. In your example there is no such parameter mentioned. am i in right direction ?

shamblett commented 4 years ago

I take it you have looked here, which explains how google says you should connect to its MQTT service, which is basically what the iot-home project does.

bhagujava commented 4 years ago

@shamblett Thanks for your prompt reply....


  final MqttConnectMessage connMess = MqttConnectMessage()
      .withClientIdentifier('Mqtt_MyClientUniqueId')
      .keepAliveFor(20) // Must agree with the keep alive set above or not set
      .withWillTopic('willtopic') // If you set this you must set a will message
      .withWillMessage('My Will message')
      .startClean() // Non persistent session for testing
      .withWillQos(MqttQos.atLeastOnce);
  print('EXAMPLE::Mosquitto client connecting....');
  client.connectionMessage = connMess;

  /// Connect the client, any errors here are communicated by raising of the appropriate exception. Note
  /// in some circumstances the broker will just disconnect us, see the spec about this, we however eill
  /// never send malformed messages.
  try {
    await client.connect();
  } on Exception catch (e) {
    print('EXAMPLE::client exception - $e');
    client.disconnect();
  }

This is the snapshot which i have observed to connect from the lib but as per link which you have shared has expose some parameters which is ...


const connectionArgs = {
  host: mqttBridgeHostname,
  port: mqttBridgePort,
  clientId: mqttClientId,
  username: 'unused',
  password: createJwt(projectId, privateKeyFile, algorithm),
  protocol: 'mqtts',
  secureProtocol: 'TLSv1_2_method',
};

// Create a client, and connect to the Google MQTT bridge.
const iatTime = parseInt(Date.now() / 1000);
const client = mqtt.connect(connectionArgs);

Do we have such way to expose and assign value in same way the node.js did ?

shamblett commented 4 years ago

You don't need all the values above, the protocol and secureprotocol options don't exist in Dart/mqtt-client, the iot-core.dart file shows you how to do all this in Dart world, there's no direct mapping from nodejs, its done slightly differently. What part of the iot-core.dart example are you having trouble with? You should be able to edit the file and substitute your specific parameters such as projectId etc.