Skip to content

Commit 30b57d1

Browse files
committed
Fixes URL for HTTP example and adds snippets for MQTT.
1 parent 6a60c8f commit 30b57d1

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

iot/api-client/http_example/README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ The following command summarizes the sample usage:
2020
```
2121
mvn exec:java \
2222
-Dexec.mainClass="com.google.cloud.iot.examples.HttpExample" \
23-
-Dexec.args="-project_id=my-iot-project \
24-
-registry_id=my-registry \
25-
-device_id=my-device \
26-
-private_key_file=rsa_private_pkcs8 \
27-
-algorithm=RS256"
23+
-Dexec.args="-project_id=<your-iot-project> \
24+
-registry_id=<your-registry-id> \
25+
-device_id=<device-id> \
26+
-private_key_file=<path-to-keyfile> \
27+
-message_type=<event|state> \
28+
-algorithm=<RS256|ES256>"
2829
```
2930

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

46+
To publish state messages, run the sample as follows:
47+
48+
```
49+
mvn exec:java \
50+
-Dexec.mainClass="com.google.cloud.iot.examples.HttpExample" \
51+
-Dexec.args="-project_id=blue-jet-123 \
52+
-registry_id=my-registry \
53+
-device_id=my-java-device \
54+
-private_key_file=../rsa_private_pkcs8 \
55+
-message_type=state \
56+
-algorithm=RS256"
57+
```
58+
59+
4560
## Reading the messages written by the sample client
4661

4762
1. Create a subscription to your topic.

iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExample.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public static void publishMessage(String payload, String urlPath, String message
105105

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

108+
109+
urlPath = urlPath + devicePath + ":" + urlSuffix;
108110
URL url = new URL(urlPath);
109111
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
110112
httpCon.setDoOutput(true);

iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.cloud.iot.examples;
1616

17+
// [START cloudiotcore_mqtt_imports]
1718
import io.jsonwebtoken.JwtBuilder;
1819
import io.jsonwebtoken.Jwts;
1920
import io.jsonwebtoken.SignatureAlgorithm;
@@ -26,6 +27,7 @@
2627
import org.eclipse.paho.client.mqttv3.MqttMessage;
2728
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
2829
import org.joda.time.DateTime;
30+
// [END cloudiotcore_mqtt_imports]
2931

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

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

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

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

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

0 commit comments

Comments
 (0)