AWS-MQTT Connection

0 votes
Hello.
I'm using Amazon Web Services and I need to send/receive information from the AWS IoT Platform. But I'm unable to send or receive messages. It seems to me that there some issue with my connection to the platform. And, after double-checking my codes, I finally narrowed my problem down to the MQTT. Since I'm relatively new to the Internet of Things and Networking, I could've messed that part of the code. Could anybody help me with the script for the message broker connecting to the platform?
TIA!
Aug 24, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points
1,619 views

2 answers to this question.

0 votes

Well, this is a code I had running for my connection to the AWS IoT Platform. I think it might be what you're looking for. Should solve your problem, try it:  

credentialsProvider = new CognitoCachingCredentialsProvider(
            getApplicationContext(), // context
            COGNITO_POOL_ID, // Identity Pool ID
            MY_REGION // Region);

    Region region = Region.getRegion(MY_REGION);

    // MQTT Client
    mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);

    // Set keepalive to 10 seconds.  Will recognize disconnects more quickly but will also send
    // MQTT pings every 10 seconds.
    mqttManager.setKeepAlive(10);
    mIotAndroidClient = new AWSIotClient(credentialsProvider);
    mIotAndroidClient.setRegion(region);
    try {
            mqttManager.connect(clientKeyStore, new AWSIotMqttClientStatusCallback() {
                @Override
                public void onStatusChanged(final AWSIotMqttClientStatus status,
                        final Throwable throwable) {
                    Log.d(LOG_TAG, "Status = " + String.valueOf(status));

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (status == AWSIotMqttClientStatus.Connecting) {
                                                               } else if (status == AWSIotMqttClientStatus.Connected) {
                                tvStatus.setText("Connected");

                            } else if (status == AWSIotMqttClientStatus.Reconnecting) {
                                if (throwable != null) {
                                    Log.e(LOG_TAG, "Connection error.", throwable);
                                }
                                tvStatus.setText("Reconnecting");
                            } else if (status == AWSIotMqttClientStatus.ConnectionLost) {
                                if (throwable != null) {
                                    Log.e(LOG_TAG, "Connection error.", throwable);
                                }
                                                                                    }
                        }
                    });
                }
            });
        } catch (final Exception e) {
                }

Let's check out this AWS Certification training to learn more.

answered Aug 24, 2018 by DataKing99
• 8,250 points
how did you get the "clientKeyStore" and where is mIotAndroidClient being used?
Hi@Sweety,

Check the above code properly. Also when you use the methods or class check their document. You will get an idea of how to use them.
0 votes

Link --> https://docs.aws.amazon.com/greengrass/latest/developerguide/module3-I.html

This is Very Helpful to you.If Your Coding is in Python.Its too much easy to You.

answered Apr 3, 2019 by KanaguRaj
• 160 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer
0 votes
1 answer

MQTT protocol connection error

Ok so you need two libraries to ...READ MORE

answered Jul 6, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points

reshown Jul 6, 2018 by Vardhan 3,067 views
0 votes
0 answers

MQTT-Google Cloud pub/sub connection status!

Please help! So, I have a couple of ...READ MORE

Jul 20, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points
826 views
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
2,714 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,034 views
0 votes
1 answer

Changing message format in KURA MQTT cloud client

The Eclipse Kura communicates with a MQTT ...READ MORE

answered Jul 29, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
1,172 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP