Google Cloud IoT Core は 2023 年 8 月 16 日に廃止されます。詳細については、担当の Google Cloud アカウント チームにお問い合わせください。

デバイスを接続する

コレクションでコンテンツを整理 必要に応じて、コンテンツの保存と分類を行います。

ゲートウェイにデバイスを接続します。

コードサンプル

Go

詳細については、Cloud IoT Core Go の API のリファレンス ドキュメントをご覧ください。


// attachDevice attaches a device to a gateway.
func attachDevice(deviceID string, client mqtt.Client, jwt string) error {
	attachTopic := fmt.Sprintf("/devices/%s/attach", deviceID)
	fmt.Printf("Attaching device: %s\n", attachTopic)

	attachPayload := "{}"
	if jwt != "" {
		attachPayload = fmt.Sprintf("{ 'authorization' : %s }", jwt)
	}

	if token := client.Publish(attachTopic, 1, false, attachPayload); token.Wait() && token.Error() != nil {
		return token.Error()
	}
	return nil
}

Java

詳細については、Cloud IoT Core Java の API のリファレンス ドキュメントをご覧ください。

final String attachTopic = String.format("/devices/%s/attach", deviceId);
System.out.println(String.format("Attaching: %s", attachTopic));
String attachPayload = "{}";
MqttMessage message = new MqttMessage(attachPayload.getBytes(StandardCharsets.UTF_8.name()));
message.setQos(1);
client.publish(attachTopic, message);

Python

詳細については、Cloud IoT Core Python の API のリファレンス ドキュメントをご覧ください。

attach_topic = "/devices/{}/attach".format(device_id)
attach_payload = '{{"authorization" : "{}"}}'.format(auth)
client.publish(attach_topic, attach_payload, qos=1)

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。