Skip to content

Instantly share code, notes, and snippets.

@WilliamBerryiii
Last active May 2, 2019 01:41
Show Gist options
  • Save WilliamBerryiii/b6001c23247afdbfc31e1a6f28407f09 to your computer and use it in GitHub Desktop.
Save WilliamBerryiii/b6001c23247afdbfc31e1a6f28407f09 to your computer and use it in GitHub Desktop.
Getting Started with Webtask.io for IoT
var connectCallback = function (err) {
if (err) {
logCommand('Could not connect: ' + err);
completed(log);
} else {
logCommand('Client connected');
var msg = new Message('some data from my device');
client.sendEvent(msg, function (err) {
if (err) {
logCommand(err.toString());
completed(log);
} else {
logCommand('Message sent');
completed(log);
}
});
}
};
client.open(connectCallback);
var clientFromConnectionString = require('azure-iot-device-amqp').clientFromConnectionString;
var Message = require('azure-iot-device').Message;
var connectionString = 'HostName={iot_host_name}.azure-devices.net;DeviceId={demo_device_name};SharedAccessKey={device_sas_token}';
var client = clientFromConnectionString(connectionString);
var log = [];
var logCommand = function (statement) {
log.push(Date.Now() + ": " + statement);
};
var completed = function() { cb (null, log); };
module.exports =
function (cb) {
var clientFromConnectionString = require('azure-iot-device-amqp').clientFromConnectionString;
var Message = require('azure-iot-device').Message;
var connectionString = 'HostName=wberry-demo-iot-hub.azure-devices.net;DeviceId=demo_device;SharedAccessKey=QgoivZb0/hzYgn9Fpe4581KzBsZBdLcv+Mwhc5q7g6Q=';
var client = clientFromConnectionString(connectionString);
var log = [];
var logCommand = function (statement) {
log.push(Date.Now() + ": " + statement);
};
var completed = function() { cb (null, log); };
var connectCallback = function (err) {
if (err) {
logCommand('Could not connect: ' + err);
completed(log);
} else {
logCommand('Client connected');
var msg = new Message('some data from my device');
client.sendEvent(msg, function (err) {
if (err) {
logCommand(err.toString());
completed(log);
} else {
logCommand('Message sent');
completed(log);
}
});
}
};
client.open(connectCallback);
};
module.exports =
function (cb) {
var logs = "";
cb(null, logs);
}
C:\source\webtask> wt logs
[18:50:04.289Z] INFO wt: connected to streaming logs (container=wt-{GUID}-0)
[18:50:33.705Z] INFO wt: new webtask request {task_identifier}
[18:50:33.732Z] INFO wt: HostName={iot_hub}.azure-devices.net;DeviceId=demo_device;SharedAccessKey={device_SAS_Key}
[18:50:34.420Z] INFO wt: finished webtask request {task_identifier} with HTTP 200 in 530ms
[18:50:34.433Z] INFO wt: Client connected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment