CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-arduino--arduino-iot-client

JavaScript client library for the Arduino IoT Cloud REST API enabling programmatic management of devices, things, properties, and time-series data

Overview
Eval results
Files

network-credentials.mddocs/

Network Credentials

Network credentials management for device connectivity configuration. Manage Wi-Fi credentials and network connection information for IoT devices.

Capabilities

Network Credentials Operations

Retrieve and manage network credentials for device connectivity.

class NetworkCredentialsV1Api {
  /**
   * Get network credentials by type
   * @param type - Network credential type (e.g., 'wifi', 'cellular')
   * @param opts - Optional parameters including organization ID
   * @returns Promise<ArduinoCredentialsv1> - Network credentials information
   */
  networkCredentialsV1Show(type: string, opts?: any): Promise<ArduinoCredentialsv1>;

  /**
   * Get network credentials by device association
   * @param type - Network credential type
   * @param opts - Optional parameters including organization ID and device filters
   * @returns Promise<ArduinoCredentialsv1[]> - Network credentials with device connections
   */
  networkCredentialsV1ShowByDevice(type: string, opts?: any): Promise<ArduinoCredentialsv1[]>;
}

Template Management

Apply templates for creating standardized things and devices configurations.

class TemplatesApi {
  /**
   * Apply a template to create things and devices from predefined configurations
   * @param template - Template configuration with things, devices, and properties
   * @param opts - Optional parameters including organization ID
   * @returns Promise<ArduinoTemplate> - Applied template result with created entities
   */
  templatesApply(template: Template, opts?: any): Promise<ArduinoTemplate>;
}

Data Models

interface ArduinoCredentialsv1 {
  connectionType?: string;
  createdAt?: Date;
  id?: string;
  name?: string;
  organizationId?: string;
  password?: string;
  ssid?: string;
  type?: string;
  updatedAt?: Date;
  userId?: string;
}

interface ArduinoTemplate {
  createdAt?: Date;
  devices?: ArduinoDevicev2templatedevice[];
  href?: string;
  id?: string;
  name?: string;
  organizationId?: string;
  things?: ArduinoThingtemplate[];
  updatedAt?: Date;
  userId?: string;
}

interface Template {
  devices?: ArduinoLinkedDeviceTemplate[];
  name: string;
  things?: ArduinoThingtemplate[];
}

interface ArduinoLinkedDeviceTemplate {
  deviceId?: string;
  fqbn?: string;
  name?: string;
  serial?: string;
  type?: string;
}

interface ArduinoDevicev2templatedevice {
  fqbn?: string;
  name?: string;
  type?: string;
}

interface ArduinoThingtemplate {
  name?: string;
  properties?: ArduinoTemplateproperty[];
  timezone?: string;
  webhookUri?: string;
}

interface ArduinoTemplateproperty {
  maxValue?: number;
  minValue?: number;
  name?: string;
  permission?: PropertyPermission;
  persist?: boolean;
  type?: PropertyType;
  updateParameter?: number;
  updateStrategy?: PropertyUpdateStrategy;
  variableName?: string;
}

Network Credentials and Template Examples:

import ArduinoIotClient from '@arduino/arduino-iot-client';

const networkCredentialsApi = new ArduinoIotClient.NetworkCredentialsV1Api();
const templatesApi = new ArduinoIotClient.TemplatesApi();

// Retrieve Wi-Fi credentials
async function getWiFiCredentials() {
  try {
    const wifiCredentials = await networkCredentialsApi.networkCredentialsV1Show('wifi');

    console.log('Wi-Fi Credentials:');
    console.log(`- SSID: ${wifiCredentials.ssid}`);
    console.log(`- Type: ${wifiCredentials.type}`);
    console.log(`- Connection Type: ${wifiCredentials.connectionType}`);
    console.log(`- Created: ${wifiCredentials.createdAt}`);

    return wifiCredentials;
  } catch (error) {
    console.error('Failed to retrieve Wi-Fi credentials:', error);
    throw error;
  }
}

// Get network credentials by device
async function getNetworkCredentialsByDevice() {
  try {
    const deviceCredentials = await networkCredentialsApi.networkCredentialsV1ShowByDevice('wifi');

    console.log('Network credentials by device:');
    deviceCredentials.forEach((credential, index) => {
      console.log(`Device ${index + 1}:`);
      console.log(`  - Name: ${credential.name}`);
      console.log(`  - SSID: ${credential.ssid}`);
      console.log(`  - Type: ${credential.type}`);
    });

    return deviceCredentials;
  } catch (error) {
    console.error('Failed to retrieve network credentials by device:', error);
    throw error;
  }
}

// Apply a comprehensive IoT template
async function applySensorNetworkTemplate() {
  try {
    const template = {
      name: "Environmental Monitoring Network",
      things: [
        {
          name: "Indoor Climate Monitor",
          timezone: "America/New_York",
          properties: [
            {
              name: "Temperature",
              type: "TEMPERATURE",
              permission: "READ_ONLY",
              updateStrategy: "ON_CHANGE",
              variableName: "temperature",
              persist: true,
              minValue: -10,
              maxValue: 50
            },
            {
              name: "Humidity",
              type: "HUMIDITY",
              permission: "READ_ONLY",
              updateStrategy: "TIMED",
              updateParameter: 60,
              variableName: "humidity",
              persist: true,
              minValue: 0,
              maxValue: 100
            },
            {
              name: "Air Quality Index",
              type: "INT",
              permission: "READ_ONLY",
              updateStrategy: "TIMED",
              updateParameter: 300,
              variableName: "airQuality",
              persist: true,
              minValue: 0,
              maxValue: 500
            }
          ]
        },
        {
          name: "Outdoor Weather Station",
          timezone: "America/New_York",
          properties: [
            {
              name: "Temperature",
              type: "TEMPERATURE",
              permission: "READ_ONLY",
              updateStrategy: "ON_CHANGE",
              variableName: "outdoorTemp",
              persist: true,
              minValue: -40,
              maxValue: 60
            },
            {
              name: "Humidity",
              type: "HUMIDITY",
              permission: "READ_ONLY",
              updateStrategy: "TIMED",
              updateParameter: 120,
              variableName: "outdoorHumidity",
              persist: true,
              minValue: 0,
              maxValue: 100
            },
            {
              name: "Pressure",
              type: "PRESSURE",
              permission: "READ_ONLY",
              updateStrategy: "TIMED",
              updateParameter: 300,
              variableName: "pressure",
              persist: true,
              minValue: 950,
              maxValue: 1050
            },
            {
              name: "Wind Speed",
              type: "VELOCITY",
              permission: "READ_ONLY",
              updateStrategy: "ON_CHANGE",
              variableName: "windSpeed",
              persist: true,
              minValue: 0,
              maxValue: 200
            }
          ]
        }
      ],
      devices: [
        {
          name: "Indoor Sensor Node",
          fqbn: "arduino:samd:nano_33_iot",
          type: "nano_33_iot"
        },
        {
          name: "Outdoor Weather Station",
          fqbn: "arduino:samd:mkrwifi1010",
          type: "mkrwifi1010"
        }
      ]
    };

    console.log('Applying environmental monitoring template...');
    const appliedTemplate = await templatesApi.templatesApply(template);

    console.log('Template applied successfully:');
    console.log(`- Template ID: ${appliedTemplate.id}`);
    console.log(`- Created Things: ${appliedTemplate.things?.length || 0}`);
    console.log(`- Created Devices: ${appliedTemplate.devices?.length || 0}`);

    // List created entities
    if (appliedTemplate.things) {
      console.log('\nCreated Things:');
      appliedTemplate.things.forEach((thing, index) => {
        console.log(`${index + 1}. ${thing.name}`);
        console.log(`   Properties: ${thing.properties?.length || 0}`);
        console.log(`   Timezone: ${thing.timezone}`);
      });
    }

    if (appliedTemplate.devices) {
      console.log('\nCreated Devices:');
      appliedTemplate.devices.forEach((device, index) => {
        console.log(`${index + 1}. ${device.name}`);
        console.log(`   FQBN: ${device.fqbn}`);
        console.log(`   Type: ${device.type}`);
      });
    }

    return appliedTemplate;

  } catch (error) {
    console.error('Failed to apply template:', error);
    throw error;
  }
}

// Create a reusable sensor template
async function createStandardSensorTemplate() {
  try {
    const standardSensorTemplate = {
      name: "Standard Sensor Template",
      things: [
        {
          name: "Generic Sensor Thing",
          timezone: "UTC",
          properties: [
            {
              name: "Value",
              type: "FLOAT",
              permission: "READ_ONLY",
              updateStrategy: "ON_CHANGE",
              variableName: "sensorValue",
              persist: true
            },
            {
              name: "Battery Level",
              type: "PERCENT",
              permission: "READ_ONLY",
              updateStrategy: "TIMED",
              updateParameter: 3600, // Every hour
              variableName: "batteryLevel",
              persist: true,
              minValue: 0,
              maxValue: 100
            },
            {
              name: "Status",
              type: "STRING",
              permission: "READ_ONLY",
              updateStrategy: "ON_CHANGE",
              variableName: "status",
              persist: false
            }
          ]
        }
      ],
      devices: [
        {
          name: "Generic Sensor Device",
          fqbn: "arduino:samd:nano_33_iot",
          type: "nano_33_iot"
        }
      ]
    };

    const template = await templatesApi.templatesApply(standardSensorTemplate);
    console.log(`Created standard sensor template: ${template.id}`);

    return template;

  } catch (error) {
    console.error('Failed to create standard sensor template:', error);
    throw error;
  }
}

// Network setup workflow
async function setupNetworkAndTemplate() {
  try {
    console.log('Setting up network credentials and template workflow...');

    // 1. Check network credentials
    console.log('\n=== Network Credentials ===');
    await getWiFiCredentials();
    await getNetworkCredentialsByDevice();

    // 2. Apply comprehensive template
    console.log('\n=== Template Application ===');
    const envTemplate = await applySensorNetworkTemplate();

    // 3. Create reusable standard template
    console.log('\n=== Standard Template Creation ===');
    await createStandardSensorTemplate();

    console.log('\nNetwork and template setup completed successfully');
    return envTemplate;

  } catch (error) {
    if (error.status === 400) {
      console.error('Bad request - check template configuration');
    } else if (error.status === 404) {
      console.error('Network credentials not found');
    } else {
      console.error('Network setup error:', error.detail || error.message);
    }
  }
}

Install with Tessl CLI

npx tessl i tessl/npm-arduino--arduino-iot-client

docs

automation-triggers.md

dashboard-management.md

device-management.md

index.md

lora-device-management.md

network-credentials.md

property-management.md

thing-management.md

timeseries-analytics.md

tile.json