Google Cloud IoT Core Service

Google Cloud Internet of Things (IoT) Core is a fully managed service for securely connecting and managing IoT devices, from a few to millions. Ingest data from connected devices and build rich applications that integrate with the other big data services of Google Cloud Platform.

Reference

For detailed information on this service, see the reference documentation for the Google Cloud IoT Core API. Like all advanced services in Apps Script, the Google Cloud IoT Core service uses the same objects, methods, and parameters as the public API.

To report issues and find other support, see the Google Cloud IoT Core support documentation.

Sample Code

The sample code below uses version 1 of the API. Each example uses the Google Cloud variables projectId and cloudRegion.

You can find the projectId variable in the Project Info section of the Google Cloud Console.

The cloudRegion corresponds to the region the device registry is located in and can be us-central1, asia-east1, or europe-west1.

List Device Registies

The following example shows you how to list the devices contained within a given device registry.

See the Sample Code section for a description of the variables commonly used in the Cloud IoT Core examples.

function listRegistries() {
  Logger.log(response);
  var projectId = 'your-project-id';
  var cloudRegion = 'us-central1';
  var parent = 'projects/' + projectId + '/locations/' + cloudRegion;

  var response = CloudIoT.Projects.Locations.Registries.list(parent);
  if (response.deviceRegistries){
    response.deviceRegistries.forEach(
      function(registry){
        Logger.log(registry.id);
      });
  }
}

Create Device Registy

The following example function creates a new device registry.

See the Sample Code section for a description of the variables commonly used in the Cloud IoT Core examples.

function createRegistry() {
  var cloudRegion = 'us-central1';
  var name = 'your-registry-name';
  var projectId = 'your-project-id';
  var topic = 'your-pubsub-topic';

  var pubsubTopic = 'projects/' + projectId + '/topics/' + topic;

  var registry = {
    eventNotificationConfigs: [{
      // From - https://console.cloud.google.com/cloudpubsub
      pubsubTopicName : pubsubTopic
    }],
    'id': name
  };
  var parent = 'projects/' + projectId + '/locations/' + cloudRegion;

  var response = CloudIoT.Projects.Locations.Registries.create(registry, parent)
  Logger.log('Created registry: ' + response.id);
}

After you have created the registry, you can verify that it was created with the Cloud SDK command and the region you used:

gcloud iot registries list --region=us-central1

Describe a Registry

The following example function retrieves a device registry and lists the name for it.

See the Sample Code section for a description of the variables commonly used in the Cloud IoT Core examples.

function getRegistry() {
  var cloudRegion = 'us-central1';
  var name = 'your-registry-name';
  var projectId = 'your-project-id';

  var parent = 'projects/' + projectId + '/locations/' + cloudRegion;
  var registryName = parent + '/registries/' + name;

  var response = CloudIoT.Projects.Locations.Registries.get(registryName)
  Logger.log('Retrieved registry: ' + response.id);
}

You should see the given registry details in the logger output under View > Logs or by pressing "Ctrl + Enter".

Delete a registry

The following function demonstrates how to remove a device registry from your project's registries. The registry must be empty of devices before you can delete it.

See the Sample Code section for a description of the variables commonly used in the Cloud IoT Core examples.

function deleteRegistry() {
  var cloudRegion = 'us-central1';
  var name = 'your-registry-name';
  var projectId = 'your-project-id';

  var parent = 'projects/' + projectId + '/locations/' + cloudRegion;
  var registryName = parent + '/registries/' + name;

  var response = CloudIoT.Projects.Locations.Registries.remove(registryName)
  // Successfully removed registry if exception was not thrown.
  Logger.log('Deleted registry: ' + name);
}

After you have deleted the registry, you can verify that it was removed with the Cloud SDK command and the region you used:

gcloud iot registries list --region=us-central1

List devices in a registry

The following function lists the devices that have been created in a device registry.

See the Sample Code section for a description of the variables commonly used in the Cloud IoT Core examples.

function listDevicesForRegistry() {
  var cloudRegion = 'us-central1';
  var name = 'your-registry-name';
  var projectId = 'your-project-id';

  var parent = 'projects/' + projectId + '/locations/' + cloudRegion;
  var registryName = parent + '/registries/' + name;

  var response = CloudIoT.Projects.Locations.Registries.Devices.list(registryName);

  Logger.log('Registry contains the following devices: ');
  if (response.devices) {
    response.devices.forEach(
      function(device){
        Logger.log('\t' + device.id);
      });
  }
}

Create a device without credentials

This function creates a device without credentials. This is useful when you are using a Gateway to connect devices without the ability to connect over the Cloud IoT Core device bridge.

See the Sample Code section for a description of the variables commonly used in the Cloud IoT Core examples.

function createDevice() {
  var cloudRegion = 'us-central1';
  var name = 'your-device-name';
  var projectId = 'your-project-id';
  var registry = 'your-registry-name';

  Logger.log('Creating device: ' + name + ' in Registry: ' + registry);
  var parent = 'projects/' + projectId + '/locations/' + cloudRegion + '/registries/' + registry;

  var device = {
    id: name,
    gatewayConfig: {
      gatewayType: 'NON_GATEWAY',
      gatewayAuthMethod: 'ASSOCIATION_ONLY'
    }
  };

  var response = CloudIoT.Projects.Locations.Registries.Devices.create(device, parent)
  Logger.log('Created device:' + response.name);
}

Create device with RSA credentials

Devices connect to Cloud IoT Core using a JSON Web Token (JWT) that contains a cryptographic signature that is either encoded using an Eliptic Curve (EC) algorithm or using the Rivest, Shamir, Adelman (RSA) algorithm. The most commonly used device type is one with RSA authentication credentials. The RSA credential consists of a public and private key used to authenticate devices when they connect.

Before you create the device, generate a device credential pair with the command:

openssl req -x509 -newkey rsa:2048 -days 3650 -keyout rsa_private.pem \
  -nodes -out rsa_cert.pem -subj "/CN=unused"

The public portion of the credential is output to rsa_cert.pem. Set this as your cert variable in the example below so that Google can use it to verify your private key. Keep your private key secure; it must only be visible to the device that you are connecting.

See the Sample Code section for a description of the variables commonly used in the Cloud IoT Core examples. See the Authentication section of the Cloud IoT Core documentation for more information about the details of device security.

function createRsaDevice() {
  // Create the RSA public/private keypair with the following OpenSSL command:
  //    openssl req -x509 -newkey rsa:2048 -days 3650 -keyout rsa_private.pem \
  //      -nodes -out rsa_cert.pem -subj "/CN=unused"
  //
  // **NOTE** Be sure to insert the newline charaters in the string varant.
  var cert =
      '-----BEGIN CERTIFICATE-----\n' +
      'your-PUBLIC-certificate-b64-bytes\n' +
      '...\n' +
      'more-PUBLIC-certificate-b64-bytes==\n' +
      '-----END CERTIFICATE-----\n';

  var cloudRegion = 'us-central1';
  var name = 'your-device-name';
  var projectId = 'your-project-id';
  var registry = 'your-registry-name';

  var parent = 'projects/' + projectId + '/locations/' + cloudRegion + '/registries/' + registry;
  var device = {
    id: name,
    gatewayConfig: {
      gatewayType: 'NON_GATEWAY',
      gatewayAuthMethod: 'ASSOCIATION_ONLY'
    },
    credentials: [{
      publicKey: {
        format: 'RSA_X509_PEM',
        key: cert
      }
    }],
  };

  var response = CloudIoT.Projects.Locations.Registries.Devices.create(device, parent);
  Logger.log('Created device:' + response.name);
}

Delete device from registry

The following example shows you how to delete a device from a registry. If the device is associated or bound to another device, you must remove the association or binding before you can delete the device.

See the Sample Code section for a description of the variables commonly used in the Cloud IoT Core examples.

function deleteDevice() {
  var cloudRegion = 'us-central1';
  var name = 'your-device-name';
  var projectId = 'your-project-id';
  var registry = 'your-registry-name';

  var parent = 'projects/' + projectId + '/locations/' + cloudRegion + '/registries/' + registry;
  var deviceName = parent + '/devices/' + name;

  var response = CloudIoT.Projects.Locations.Registries.Devices.remove(deviceName)
  // If no exception thrown, device was successfully removed.
  Logger.log('Successfully deleted device: ' + deviceName);
}

다음에 대한 의견 보내기...

도움이 필요하시나요? 지원 페이지를 방문하세요.