{"id":13733087,"url":"https://github.com/lucadruda/iotc-nodejs-device-client","last_synced_at":"2025-05-15T17:32:16.693Z","repository":{"id":34050449,"uuid":"167515633","full_name":"lucadruda/iotc-nodejs-device-client","owner":"lucadruda","description":"NodeJS device client for Azure IoTCentral","archived":false,"fork":false,"pushed_at":"2023-04-30T07:53:17.000Z","size":1078,"stargazers_count":2,"open_issues_count":9,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-14T11:49:06.158Z","etag":null,"topics":["azure-iot","iot"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucadruda.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-01-25T08:49:28.000Z","updated_at":"2022-04-14T22:01:12.000Z","dependencies_parsed_at":"2024-01-06T14:04:39.408Z","dependency_job_id":null,"html_url":"https://github.com/lucadruda/iotc-nodejs-device-client","commit_stats":null,"previous_names":["lucadruda/iotc_nodejs_device_client"],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucadruda%2Fiotc-nodejs-device-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucadruda%2Fiotc-nodejs-device-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucadruda%2Fiotc-nodejs-device-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucadruda%2Fiotc-nodejs-device-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucadruda","download_url":"https://codeload.github.com/lucadruda/iotc-nodejs-device-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224717551,"owners_count":17357902,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["azure-iot","iot"],"created_at":"2024-08-03T03:00:37.512Z","updated_at":"2024-11-15T01:31:17.675Z","avatar_url":"https://github.com/lucadruda.png","language":"TypeScript","funding_links":[],"categories":["miscellaneous"],"sub_categories":[],"readme":"# Microsoft Azure IoTCentral SDK for Node.js\n\n[![Join the chat at https://gitter.im/iotdisc/community](https://badges.gitter.im/iotdisc.svg)](https://gitter.im/iotdisc/community?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n[![Licensed under the MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/lucadruda/iotc-nodejs-device-client/blob/master/LICENSE)\n\n\n## Prerequisites\n+ Node.js version 8.x or higher - https://nodejs.org\n\n## Installing `azure-iotcentral-device-client` and types\n\n```\nnpm install azure-iotcentral-device-client\n```\n\n## Types\n\nSource code is written in Typescript so types are bundled with the package, you don't need to install any additional package.\n\n\n## Samples\n\nA couple of samples in Javascripts can be found [here](https://github.com/lucadruda/iotc-samples)\n\n\n## Instructions\n\nWhen connecting a device to an IoT Central application an IoTCClient is initialized.\nSDK supports X509 and SymmetricKey authentication;\n\n#### X509\n```js\nconst iotCentral = require('azure-iotcentral-device-client');\n\nconst scopeId = '';\nconst deviceId = '';\nconst passphrase = ''; //optional\nconst cert = {\n    cert: \"public cert\"\n    key: \"private key\",\n    passphrase: \"passphrase\"\n}\n\nconst iotc = new iotCentral.IoTCClient(deviceId, scopeId, 'X509_CERT', cert);\n```\n\n#### SAS\n```js\nconst iotCentral = require('azure-iotcentral-device-client');\n\nconst scopeId = 'scopeID';\nconst deviceId = 'deviceID';\nconst sasKey = 'masterKey';\n\nconst iotc = new iotCentral.IoTCClient(deviceId, scopeId, 'symm_key', sasKey);\n```\n\n### Connect\n```ts\nawait iotc.connect([timeout]);\n```\nAfter successfull connection, IOTC context is available for further commands.\n\n_connect_ accepts an optional timeout in seconds for connection operation.\n\n\u003cbr/\u003e\n\n### Send telemetry\n\nSend telemetry every 3 seconds\n```js\nsetInterval(async () =\u003e {\n            await iotc.sendTelemetry({\n                field1: value1,\n                field2: value2,\n                field3: value3\n            }, [properties]);\n})\n```\nAn optional *properties* object can be included in the send methods, to specify additional properties for the message (e.g. timestamp, content-type etc... ).\nProperties can be custom or part of the reserved ones (see list [here](https://github.com/Azure/azure-iot-sdk-csharp/blob/master/iothub/device/src/MessageSystemPropertyNames.cs#L36)).\n\n\n### Send property update\n```js\nawait iotc.sendProperty({fieldName:'fieldValue'},[properties]);\n```\nAn optional *properties* object can be included in the send methods, to specify additional properties for the message (e.g. timestamp, content-type etc... ).\n\n\n### Listen to properties update\n```js\niotc.on('Properties', callback);\n```\ne.g.\n```js\nconst onPropertyChange = async (prop)=\u003e{\n    console.log(`New value ${prop.value} for property ${prop.name}`);\n    await prop.ack(); // sync property value with the cloud\n}\n\niotc.on('Properties',onPropertyChange);\n```\n\n### Listen to commands and offline commands\n```js\niotc.on('Commands', callback);\n```\ne.g.\n```js\nconst onCommandReceived = async (cmd) =\u003e {\n    console.log(`Received command '${cmd.name}'${cmd.requestPayload ? ` with payload ${cmd.requestPayload}` : '.'}`);\n    // command has been successfully executed\n    await cmd.reply(IIoTCCommandResponse.SUCCESS, 'Completed');\n}\n\niotc.on('Commands', callback);\n\n```\n\nFor offline command, callback will trigger both when device is connected and when a command is enqueued and device re-connect after disconnession.\n\n\n## One-touch device provisioning and approval\nA device can send custom data during provision process: if a device is aware of its IoT Central template Id, then it can be automatically provisioned.\n\n### How to set IoTC template ID in your device\nDevice template id (a.k.a Model Id) is used when obtaining authorization codes for new devices and automatically assign them to the right template. By providing template id during credentials generation, user doesn't need to manually migrate or assign device from IoT Central site.\n\nIn order to get the unique identifier, open configuration page for required model under \"Device templates\" section.\n![Img](assets/modelId.png)\n\nClick on \"View Identity\" and in next screen copy model urn.\n![Img](assets/modelId_2.png)\n\n\nThen call this method before connect():\n\n```js\niotc.setModelId('\u003cmodelId\u003e');\n```\n\n### Automatic approval (default)\nBy default device auto-approval in IoT Central is enabled, which means that administrators don't need to approve device registration to complete the provisioning process when device is not already created.\n\n![Img](assets/auto_approval.jpg)\n\n### Manual approval\nIf auto-approval is disabled, administrators need to manually approve new devices.\nThis can be done from explorer page after selecting the device\n![Img](assets/manual_approval.jpg)\n\n\n## Generate x509 certificates\nIoT Central SDK comes with an handy tool to generate self-signed x509 certificates to be used when testing device connection.\n\n```bash\nnpm run generate-certificates\n```\n\nThis example wait for a validation code which is provided by IoTCentral in the device configuration page when uploading primary or secondary root certificate.\nResulting device certificates can be used in connection example above.\n\nInstructions on connecting devices using x.509 on IoT Central [here.](https://docs.microsoft.com/en-us/azure/iot-central/core/concepts-get-connected#connect-devices-using-x509-certificates)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucadruda%2Fiotc-nodejs-device-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucadruda%2Fiotc-nodejs-device-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucadruda%2Fiotc-nodejs-device-client/lists"}