|
| 1 | +# Google Cloud IoT Core Java Samples |
| 2 | + |
| 3 | +<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iot/api-client/manager/README.md"> |
| 4 | +<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a> |
| 5 | + |
| 6 | +This directory contains samples for Google Cloud IoT Core. [Google Cloud IoT Core](https://cloud.google.com/iot/docs/ "Google Cloud IoT Core") allows developers to easily integrate Publish and Subscribe functionality with devices and programmatically manage device authorization. |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +### Java |
| 11 | + |
| 12 | +We recommend using [Java 8 JDK](https://java.com/en/download) for this example. |
| 13 | + |
| 14 | +### Download Maven |
| 15 | + |
| 16 | +These samples use the [Apache Maven][maven] build system. Before getting |
| 17 | +started, be sure to [download][maven-download] and [install][maven-install] it. |
| 18 | +When you use Maven as described here, it will automatically download the needed |
| 19 | +client libraries. |
| 20 | + |
| 21 | +[maven]: https://maven.apache.org |
| 22 | +[maven-download]: https://maven.apache.org/download.cgi |
| 23 | +[maven-install]: https://maven.apache.org/install.html |
| 24 | + |
| 25 | +### Create a Project in the Google Cloud Platform Console |
| 26 | + |
| 27 | +If you haven't already created a project, create one now. Projects enable you to |
| 28 | +manage all Google Cloud Platform resources for your app, including deployment, |
| 29 | +access control, billing, and services. |
| 30 | + |
| 31 | +1. Open the [Cloud Platform Console][cloud-console]. |
| 32 | +2. In the drop-down menu at the top, select **Create a project**. |
| 33 | +3. Give your project a name. |
| 34 | +4. Make a note of the project ID, which might be different from the project |
| 35 | + name. The project ID is used in commands and in configurations. |
| 36 | + |
| 37 | +[cloud-console]: https://console.cloud.google.com/ |
| 38 | + |
| 39 | +## Setup |
| 40 | + |
| 41 | +Note that before you can run the sample, you must configure a Google Cloud |
| 42 | +PubSub topic for Cloud IoT as described in [the parent README](../README.md). |
| 43 | + |
| 44 | +Before running the samples, you can set the `GOOGLE_CLOUD_PROJECT` and |
| 45 | +`GOOGLE_APPLICATION_CREDENTIALS` environment variables to avoid passing them to |
| 46 | +the sample every time you run it. |
| 47 | + |
| 48 | +For example, on most *nix systems you can do this as: |
| 49 | + |
| 50 | + export GOOGLE_CLOUD_PROJECT=your-project-id |
| 51 | + export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json" |
| 52 | + |
| 53 | + |
| 54 | +### Authentication |
| 55 | + |
| 56 | +This sample requires you to have authentication setup. Refer to the [Authentication Getting Started Guide](https://cloud.google.com/docs/authentication/getting-started "Google Cloud IoT Core") for instructions on setting up credentials for applications. |
| 57 | + |
| 58 | +Run the following command to install the libraries and build the sample with Maven: |
| 59 | + |
| 60 | + mvn clean compile assembly:single |
| 61 | + |
| 62 | + |
| 63 | +Before you begin, you will need to create the Google Cloud PubSub message queue, create a subscription to it, create a device registry, and add a device to the registry. |
| 64 | + |
| 65 | +1. Create the PubSub topic as: |
| 66 | + |
| 67 | + gcloud pubsub topics create java-e2e |
| 68 | + |
| 69 | +2. Create the subscription: |
| 70 | + |
| 71 | + gcloud pubsub subscriptions create java-e2e-sub --topic=java-e2e |
| 72 | + |
| 73 | +3. Create the device registry: |
| 74 | + |
| 75 | + gcloud iot registries create java-ed2e-registry --event-notification-config=topic=java-e2e --region=us-central1 |
| 76 | + |
| 77 | +4. Run the `generate_keys.sh` shell script: |
| 78 | + |
| 79 | + ../generate_keys.sh |
| 80 | + |
| 81 | +5. Add a device to the registry using the keys you generated: |
| 82 | + |
| 83 | + gcloud iot devices create device-id --registry=java-ed2e-sub --region=us-central1 --public-key=path=./rsa_cert.pem,type=RS256 |
| 84 | + |
| 85 | + |
| 86 | +## Samples |
| 87 | + |
| 88 | +### Server |
| 89 | + |
| 90 | +<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iot/api-client/manager/README.md"> |
| 91 | +<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a> |
| 92 | + |
| 93 | +To run this sample: |
| 94 | + |
| 95 | + mvn exec:java \ |
| 96 | + -Dexec.mainClass="com.example.cloud.iot.endtoend.CloudiotPubsubExampleServer" \ |
| 97 | + -Dexec.args="-project_id=<your-iot-project> \ |
| 98 | + -pubsub_subscription=<your-pubsub-subscription>" |
| 99 | + |
| 100 | +### Device |
| 101 | + |
| 102 | +<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iot/api-client/manager/README.md"> |
| 103 | +<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a> |
| 104 | + |
| 105 | +To run this sample: |
| 106 | + |
| 107 | + mvn exec:java \ |
| 108 | + -Dexec.mainClass="com.example.cloud.iot.endtoend.CloudiotPubsubExampleMqttDevice" \ |
| 109 | + -Dexec.args="-project_id=<your-iot-project> \ |
| 110 | + -registry_id=<your-registry-id> \ |
| 111 | + -device_id=<device-id> \ |
| 112 | + -private_key_file=<path-to-keyfile> \ |
| 113 | + -algorithm=<RS256|ES256>" |
| 114 | + |
| 115 | +For example, if your project ID is `blue-jet-123`, your device registry id is |
| 116 | +`my-registry`, your device id is `my-device` and you have generated your |
| 117 | +credentials using the [`generate_keys.sh`](../generate_keys.sh) script |
| 118 | +provided in the parent folder, you can run the sample as: |
| 119 | + |
| 120 | + mvn exec:java \ |
| 121 | + -Dexec.mainClass="com.example.cloud.iot.endtoend.CloudiotPubsubExampleMqttDevice" \ |
| 122 | + -Dexec.args="-project_id=blue-jet-123 \ |
| 123 | + -registry_id=my-registry \ |
| 124 | + -device_id=my-device \ |
| 125 | + -private_key_file=../rsa_private_pkcs8 \ |
| 126 | + -algorithm=RS256" |
0 commit comments