Skip to content

Commit

Permalink
Fixes URL for HTTP example and adds snippets for MQTT.
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss committed Oct 9, 2017
1 parent 6a60c8f commit 30b57d1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
25 changes: 20 additions & 5 deletions iot/api-client/http_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ The following command summarizes the sample usage:
```
mvn exec:java \
-Dexec.mainClass="com.google.cloud.iot.examples.HttpExample" \
-Dexec.args="-project_id=my-iot-project \
-registry_id=my-registry \
-device_id=my-device \
-private_key_file=rsa_private_pkcs8 \
-algorithm=RS256"
-Dexec.args="-project_id=<your-iot-project> \
-registry_id=<your-registry-id> \
-device_id=<device-id> \
-private_key_file=<path-to-keyfile> \
-message_type=<event|state> \
-algorithm=<RS256|ES256>"
```

For example, if your project ID is `blue-jet-123`, your service account
Expand All @@ -42,6 +43,20 @@ provided in the parent folder, you can run the sample as:
-algorithm=RS256"
```

To publish state messages, run the sample as follows:

```
mvn exec:java \
-Dexec.mainClass="com.google.cloud.iot.examples.HttpExample" \
-Dexec.args="-project_id=blue-jet-123 \
-registry_id=my-registry \
-device_id=my-java-device \
-private_key_file=../rsa_private_pkcs8 \
-message_type=state \
-algorithm=RS256"
```


## Reading the messages written by the sample client

1. Create a subscription to your topic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public static void publishMessage(String payload, String urlPath, String message

String encPayload = encoder.encodeToString(payload.getBytes("UTF-8"));


urlPath = urlPath + devicePath + ":" + urlSuffix;
URL url = new URL(urlPath);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.cloud.iot.examples;

// [START cloudiotcore_mqtt_imports]
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
Expand All @@ -26,6 +27,7 @@
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.joda.time.DateTime;
// [END cloudiotcore_mqtt_imports]

/**
* Java sample of connecting to Google Cloud IoT Core vice via MQTT, using JWT.
Expand All @@ -52,7 +54,8 @@
* </pre>
*/
public class MqttExample {
/** Create a Cloud IoT Core JWT for the given project id, signed with the given private key. */
// [START cloudiotcore_mqtt_createjwt]
/** Create a Cloud IoT Core JWT for the given project id, signed with the given RSA key. */
private static String createJwtRsa(String projectId, String privateKeyFile) throws Exception {
DateTime now = new DateTime();
// Create a JWT to authenticate this device. The device will be disconnected after the token
Expand All @@ -71,6 +74,7 @@ private static String createJwtRsa(String projectId, String privateKeyFile) thro
return jwtBuilder.signWith(SignatureAlgorithm.RS256, kf.generatePrivate(spec)).compact();
}

/** Create a Cloud IoT Core JWT for the given project id, signed with the given ES key. */
private static String createJwtEs(String projectId, String privateKeyFile) throws Exception {
DateTime now = new DateTime();
// Create a JWT to authenticate this device. The device will be disconnected after the token
Expand All @@ -88,8 +92,11 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw

return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact();
}
// [END cloudiotcore_mqtt_createjwt]

/** Parse arguments, configure MQTT, and publish messages. */
public static void main(String[] args) throws Exception {
// [START cloudiotcore_mqtt_configuremqtt]
MqttExampleOptions options = MqttExampleOptions.fromFlags(args);
if (options == null) {
// Could not parse.
Expand Down Expand Up @@ -131,7 +138,9 @@ public static void main(String[] args) throws Exception {
throw new IllegalArgumentException(
"Invalid algorithm " + options.algorithm + ". Should be one of 'RS256' or 'ES256'.");
}
// [END cloudiotcore_mqtt_configuremqtt]

// [START cloudiotcore_mqtt_publish]
// Create a client, and connect to the Google MQTT bridge.
MqttClient client = new MqttClient(mqttServerAddress, mqttClientId, new MemoryPersistence());
try {
Expand Down Expand Up @@ -171,5 +180,6 @@ public static void main(String[] args) throws Exception {
client.disconnect();
}
System.out.println("Finished loop successfully. Goodbye!");
// [END cloudiotcore_mqtt_publish]
}
}

0 comments on commit 30b57d1

Please sign in to comment.