Code Monkey home page Code Monkey logo

cloudiotcoremqttexample's Introduction

Cloud IoT Core MQTT Communication Example

Found it hard to understand the docs here: https://cloud.google.com/iot/docs/how-tos/mqtt-bridge and no examples online, so wrote my own.

Using MQTT to talk between an AndroidThings board and Cloud IoT Core Pub/Sub

Based on the example here:

https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java

but written for AndroidThings instead of Java. Also tried to keep it simpler and separated.

See MainActivity.java for use.

You connect like this

communicator = new IotCoreCommunicator.Builder()
        .withContext(YourActivity.this)
        .withCloudRegion("your-region") // ex: europe-west1
        .withProjectId("your-project-id")   // ex: supercoolproject23236
        .withRegistryId("your-registry-id") // ex: my-devices
        .withDeviceId("a-device-id") // ex: my-test-raspberry-pi
        .withPrivateKeyRawFileId(R.raw.rsa_private)
        .build();
communicator.connect();

and publish a message like this:

communicator.publishMessage("subtopic", "Hello World");

finally when finished:

communicator.disconnect();

cloudiotcoremqttexample's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

cloudiotcoremqttexample's Issues

Help me plss ! publish message on android studio

I have created a button for a publis message with a function when clicked. Send a message to the device, but when I click it, the program doesn't work, it bounced off the program as if it couldn't connect

Screenshot (289)

Screenshot (290)
Screenshot (291)

mqtt java.lang.IllegalStateException: Timed out waiting for a response from the server (32000)

this specific error point at :
` private void clientConnect() {
try {
MqttConnectOptions connectOptions = new MqttConnectOptions();
// Note that the the Google Cloud IoT Core only supports MQTT 3.1.1, and Paho requires that we explicitly set this.
// If you don't, the server will immediately close its connection to your device.
connectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);

        // With Google Cloud IoT Core, the username field is ignored, however it must be set for the
        // Paho client library to send the password field. The password field is used to transmit a JWT to authorize the device.
        connectOptions.setUserName("unused-but-necessary");
        connectOptions.setPassword(passwordGenerator.createJwtRsaPassword());

        IMqttToken iMqttToken = client.connect(connectOptions);
        iMqttToken.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                Log.d("TUT", "success, connected");
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                Log.e("TUT", "failure, not connected", exception);
            }
        });
        iMqttToken.waitForCompletion(TimeUnit.SECONDS.toMillis(30));
        Log.d("TUT", "IoT Core connection established.");
    } catch (MqttException e) {
        throw new IllegalStateException(e);  // this exception is thrown 
    }
}`

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.