Closed sureace closed 3 years ago
Which means there's something wrong with your setup, it can be difficult to connect to amazon, see the various discussions on #246
Hi @shamblett , Thanks for the prompt response. #246 is trying to connect via socket. In my case i am trying to connect using tcp. I followed #196 but no luck. It would be helpful with some example like google iot core and others.
Thanks, Sureace
Yes it would be helpful to have an example for AWS, I've asked AWS users for this before but to no avail, maybe if when you get your code working you could submit an example for inclusion. There is the iot_core.dart example for Google Core use, I did this as I use Google Core, not AWS or others.
Yes it would be helpful to have an example for AWS, I've asked AWS users for this before but to no avail, maybe if when you get your code working you could submit an example for inclusion. There is the iot_core.dart example for Google Core use, I did this as I use Google Core, not AWS or others.
sure @shamblett
Hi @shamblett , When i tried with MQTT fx client, it is connecting fine. I tried different ways, but no luck so far with mqtt_client. By looking at the below snapshot please tell me, am i missing anything ?
`client = MqttServerClient.withPort("
/*ByteData caData = await rootBundle.load('assets/cert/CA.pem');
ByteData certData = await rootBundle.load('assets/cert/cert.pem');
ByteData privateKeyData = await rootBundle.load('assets/cert/private.pem');
SecurityContext context = SecurityContext.defaultContext;
context.setTrustedCertificatesBytes(certData.buffer.asUint8List());
context.setClientAuthoritiesBytes(caData.buffer.asUint8List());
context.usePrivateKeyBytes(privateKeyData.buffer.asUint8List());*/
SecurityContext context = SecurityContext.defaultContext;
context.setTrustedCertificatesBytes(Config.CERT_STR.codeUnits);
context.setClientAuthoritiesBytes(Config.CA_STR.codeUnits);
context.usePrivateKeyBytes(Config.PRIVATE_KEY_STR.codeUnits);
client.securityContext = context;
client.setProtocolV311();
//client.setProtocolV31()
client.logging(on: true);
client.keepAlivePeriod = 20;
client.port = 8883;
client.secure = true;
client.onConnected = onConnected;
client.onDisconnected = onDisconnected;
final MqttConnectMessage connMess = MqttConnectMessage()
.withClientIdentifier(clientId)
.startClean()
.keepAliveFor(30);
client.connectionMessage = connMess;
await client.connect();
if (client != null &&
client.connectionStatus.state == MqttConnectionState.connected) {
print("Connected to AWS Successfully!");
isConnected = true;
}`
Thanks, Sureace
I remember another user having to use port 443 for this with AWS to match the TLS that Dart uses, could be wrong here but its worth checking what TLS versions you can use on Amazon.
Hi @shamblett , I tried with 443 and got error like The broker is not responding to the connection request message (Missing Connection Acknowledgement. Another user had same issue, then he tried with secure port 8883. 443 for socket communication right. I tried with 8443 as well but not working. There is open ssl command to check certificates with 8443. openssl s_client -connect .......-ats.iot.us-east-1.amazonaws.com:8443 -CAfile CA.pem -cert cert.pem -key private.pem
Thanks, Sureace
Ah OK, IIRC the user who used 8883 did get it working but I'm not sure how, I can't see anything obviously wrong, maybe you ned to look at your Amazon broker logs to see what it thinks is happening.
Ah OK, IIRC the user who used 8883 did get it working but I'm not sure how, I can't see anything obviously wrong, maybe you ned to look at your Amazon broker logs to see what it thinks is happening.
Let me check and update you @shamblett
@sureace did you configure AWS IoT policy correctly? Also did you attached the certificate to IoT thing and attached policy to certificate ?
Configure CloudWatch to see the logs.
@sureace did you configure AWS IoT policy correctly? Also did you attached the certificate to IoT thing and attached policy to certificate ?
Configure CloudWatch to see the logs.
Hi @Obaidiaa , I configured policy and attached it to certificate. It is perfectly working when i tested with other mqtt clients like MQTT fx and AWS IOT test client. Thanks, Sureace
@sureace I have the same issue, for device cert file, I changed context.setTrustedCertificates to context.useCertificateChain. Now it works fine,
context.useCertificateChain(currDir + 'awsiot-certificate.pem.crt'); context.setClientAuthorities(currDir + 'AmazonRootCA.pem'); context.usePrivateKey(currDir + 'awsiot-private.pem.key');
Also note, AWS only accept Qos0 (atLeastOnce) and Qos1 (atMostOnce), MqttQos.exactlyOnce is not supported. you won't get the message.
import 'dart:io'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart' show rootBundle; import 'package:mqtt_client/mqtt_client.dart'; import 'package:mqtt_client/mqtt_server_client.dart';
void main() { runApp(MyApp()); }
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'AWS IoT MQTT Example', home: MQTTScreen(), ); } }
class MQTTScreen extends StatefulWidget { @override _MQTTScreenState createState() => _MQTTScreenState(); }
class _MQTTScreenState extends State
@override void initState() { super.initState(); _connectToMQTT(); }
Future
SecurityContext context = SecurityContext.defaultContext;
context.setTrustedCertificatesBytes(certData.buffer.asUint8List());
context.setClientAuthoritiesBytes(caData.buffer.asUint8List());
context.usePrivateKeyBytes(privateKeyData.buffer.asUint8List());
client.securityContext = context;
client.setProtocolV311();
client.port = 8883;
client.secure = true;
client.logging(on: true);
client.onConnected = _onConnected;
client.onDisconnected = _onDisconnected;
await client.connect();
if (client != null &&
client.connectionStatus?.state == MqttConnectionState.connected) {
print("Connected to AWS Successfully!");
setState(() {
isConnected = true;
});
}
} catch (e) {
print('Error connecting to AWS IoT: $e');
}
}
void _onConnected() { print('Connected to AWS IoT'); }
void _onDisconnected() { print('Disconnected from AWS IoT'); setState(() { isConnected = false; }); }
@override void dispose() { client.disconnect(); super.dispose(); }
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AWS IoT MQTT Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children:
A you can read in the above on this issue I'm not an AWS user, your better off contacting the people contributing to this thread as they do use AWS and would know far more about it than me.
Hi shamblett, I am trying to establish a connection with AWS IOT core. When i tried, i am getting error like HandshakeException: Connection terminated during handshake.
Code
`client = MqttServerClient.withPort("awsEndPointURL", clientId, 8883, maxConnectionAttempts: 5);
Thanking You, Sureace