how to add multiple devices in Google cloud Iot Core?

146 Views Asked by At

I am looking for a way to connect multiple devices about 1000 to the IoT Core at once. I am to adding one device at a time via the dashboard.

I read the documentation here.

I found the code below inside the documentation, but I don't know how to make use of it.

const iot = require('@google-cloud/iot');

const iotClient = new iot.v1.DeviceManagerClient({
  // optional auth parameters.
});

async function createDevice() {
  // Construct request
  const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
  const device = {
    id: deviceId,
    credentials: [
      {
        publicKey: {
          format: 'RSA_X509_PEM',
          key: readFileSync(rsaCertificateFile).toString(),
        },
      },
    ],
  };

  const request = {
    parent: regPath,
    device,
  };

  const [response] = await iotClient.createDevice(request);
  console.log('Created device', response);
}

createDevice();

I want every device with it's own credentials and the device_Id should be something like: "tag-some_numbers" -> tag-00001, tag-00002, etc...

is this possible and can you guide me in the right directions.

2

There are 2 best solutions below

0
Mehmet Karakose On

You can add device own credentials and the device_id. It may seem complicated at first. It will help if you do the demos in the link I shared below.

https://www.cloudskillsboost.google/quests/49?locale=en

0
Ricco D On

To be able to add multiple devices, I designed my code to create a list from the certificate files (certPath). So the code will iterate through the list and create a device per iteration. The code will also assign device_id in this manner that you have described (tag-00001,tag-00002,...).

const cloudRegion = 'asia-east1'; //define your region here
const deviceId = 'tag-';
const projectId = 'your-project-id';
const registryId = 'your-registry';
const certPath = '/home/riccod/cert_file/'; //add certificate full path

const iot = require('@google-cloud/iot');
const fs = require('fs');
const {readFileSync} = require('fs');

var i = 1;
const iotClient = new iot.v1.DeviceManagerClient({});

function getCert() {
        var certArr = [];
        fs.readdirSync(certPath).forEach(file => {
                if (file.match(/rsa_cert_\d+\.pem/g) != null) { //modify regex based on your certificate filenames
                        certArr.push(`${certPath}${file}`);
                }
        });
        return certArr;
}

async function createDevice() {
  // Construct request
  const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
  var certArr = getCert();

  for (const element of certArr) {
    const device = {
      id: `${deviceId}${i.toString().padStart(5,'0')}`,
      credentials: [
        {
          publicKey: {
            format: 'RSA_X509_PEM',
            key: readFileSync(element).toString(),
          },
        },
     ],
    };

    const request = {
    parent: regPath,
    device,
    };
    i++;
    const [response] = await iotClient.createDevice(request);
    console.log('Created device', response);
  }
}

getCert();
createDevice();

See result after code run:

enter image description here

NOTE: I only have 2 cert files, thus 2 devices created.