Skip to content

Instantly share code, notes, and snippets.

@Sneezry
Created March 22, 2017 18:00
Show Gist options
  • Save Sneezry/cf9a2a0c63a6c18c783e5bb8d0fbf009 to your computer and use it in GitHub Desktop.
Save Sneezry/cf9a2a0c63a6c18c783e5bb8d0fbf009 to your computer and use it in GitHub Desktop.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
"use strict";
var Registry = require('azure-iothub').Registry;
var connectionString = "HostName=zhe-iot-hub-temperature-demo.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=1vzPlymC....4vGoc=";
var deviceId = 'new-device';
var registry = Registry.fromConnectionString(connectionString);
registry.getTwin(deviceId, function(err, twin) {
if (err) {
console.error(err.message);
} else {
console.log(JSON.stringify(twin, null, 2));
var twinPatch = {
tags: {
city: "Redmond"
},
properties: {
desired: {
telemetryInterval: 1000
}
}
};
// method 1: using the update method directly on the twin
twin.update(twinPatch, function(err, twin) {
if (err) {
console.error(err.message);
} else {
console.log(JSON.stringify(twin, null, 2));
// method 2: using the updateTwin method on the Registry object
registry.updateTwin(twin.deviceId, { properties: { desired: { telemetryInterval: 2000 }}}, twin.etag, function(err, twin) {
if (err) {
console.error(err.message);
} else {
console.log(JSON.stringify(twin, null, 2));
}
});
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment