How to use the azure-iothub.Client function in azure-iothub

To help you get started, we’ve selected a few azure-iothub examples, based on popular ways it is used in public projects.

github Azure / iothub-diagnostics / iothubtests / testService.js View on Github external
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

'use strict';

const logger = require('../lib').logger;
const config = require('../config');
const EventHubClient = require('@azure/event-hubs').EventHubClient;
const EventPosition = require('@azure/event-hubs').EventPosition;
const ServiceClient = require('azure-iothub').Client;
var serviceConnection = null;
var eventHubConnection = null;

// Called whenever an error occurs in either the message callback or the  eventhub connection setup
function errorCb(err) {
  logger.crit(err.message);
};

// Called whenever we receive a telemetry message from the client
function messageReceivedCb(message) {
  logger.debug('Service successfully received telemetry from client.');
  logger.trace(JSON.stringify(message));
  var targetDevice = message.body.toString();

  if(!targetDevice) {
    logger.crit('Client telemetry message did not contain the device, unable to respond.');
github microsoft / devkit-sdk / AZ3166 / AZ3166-1.0.0 / libraries / AzureIotHub / examples / ShakeShake / azureFunction / index.js View on Github external
'use strict';
// This function is triggered each time a message is revieved in the IoTHub.
// The message payload is persisted in an Azure Storage Table
const Message = require('azure-iot-common').Message;
const iotHubConnectionString = process.env['iotHubConnectionString'];
const cloudClient = require('azure-iothub').Client.fromConnectionString(iotHubConnectionString);
const request = require('request');

function truncateByDot(text, limit){
    return text.length < limit ? text : text.substr(0, limit - 3) + '...';
}

module.exports = function (context, myEventHubMessage) {
    // The right way to retrieve the device id is to get it from Azure Function proerpy bag
    // But seems the property bag cannot be stably retrieved from Azure so we choose to hard code the device id here for stability.
    /*
    if (!context.bindingData || !context.bindingData.systemProperties || !context.bindingData.systemProperties['iothub-connection-device-id']) {
        context.log('no device id');
        context.done();
        return;
    }
    var deviceId = context.bindingData.systemProperties['iothub-connection-device-id'];
github nebrius / aquarium-control / ingester / src / index.js View on Github external
Aquarium Control is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Aquarium Control is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Aquarium Control.  If not, see <http://www.gnu.org/licenses/>.
*/

const Registry = require('azure-iothub').Registry;
const Client = require('azure-iothub').Client;
const Message = require('azure-iot-common').Message;

module.exports = {
  run
};

function run() {
  const IOT_HUB_CONNECTION_STRING = process.env.IOT_HUB_CONNECTION_STRING;
  if (typeof IOT_HUB_CONNECTION_STRING !== 'string') {
    throw new Error('Environment variable IOT_HUB_DEVICE_CONNECTION_STRING is not defined');
  }
  console.log('Connecting to IoT Event Hub');

  const client = Client.fromConnectionString(IOT_HUB_CONNECTION_STRING);
  client.open((err) => {
    if (err) {
github Azure-Samples / iot-hub-node-ping / iothubpingfunction / index.js View on Github external
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;

var connectionString = process.env.AzureIoTHubConnectionString;

module.exports = function (context, myEventHubTrigger) {
    context.log("Received raw message from device " + JSON.stringify(myEventHubTrigger));
    //var jsonMessage = JSON.parse(myEventHubTrigger.toString());
    var jsonMessage = myEventHubTrigger;
    context.log("After parsing message, reading :");
    context.log("    deviceId : " + jsonMessage.deviceId);
    context.log("    message : " + jsonMessage.message);

    var client = Client.fromConnectionString(connectionString);
    
    client.open(function (err) {
      if (err) {
github Azure-Samples / iot-hub-node-intel-edison-getstartedkit / command_center_node / server.js View on Github external
'use strict';

var express = require('express');
var bodyParser = require('body-parser');

var ServiceClient = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
var EventHubClient = require('azure-event-hubs').Client;
var azure = require('azure-storage');
var nconf = require('nconf');

nconf.argv().env().file('./config.json');
var eventHubName = nconf.get('eventHubName');
var ehConnString = nconf.get('ehConnString');
var storageAcountName = nconf.get('storageAcountName');
var storageAccountKey = nconf.get('storageAccountKey');
var storageTable = nconf.get('storageTable');
var iotHubConnString = nconf.get('iotHubConnString');
var deviceId = nconf.get('deviceId');

var iotHubClient = ServiceClient.fromConnectionString(iotHubConnString);
github Azure-Samples / iot-hub-c-huzzah-getstartedkit / command_center_node / server.js View on Github external
'use strict';

var express = require('express');
var bodyParser = require('body-parser');

var ServiceClient = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
var EventHubClient = require('azure-event-hubs').Client;
var DeviceConnectionString = require('azure-iot-device').ConnectionString;
var azure = require('azure-storage');
var nconf = require('nconf');

nconf.argv().env().file('./config.json');
var eventHubName = nconf.get('eventHubName');
var ehConnString = nconf.get('ehConnString');
var deviceConnString = nconf.get('deviceConnString');
var storageAcountName = nconf.get('storageAcountName');
var storageAccountKey = nconf.get('storageAccountKey');
var storageTable = nconf.get('storageTable');
var iotHubConnString = nconf.get('iotHubConnString');

var deviceId = DeviceConnectionString.parse(deviceConnString).DeviceId;
github rachelnicole / pigallery / app.js View on Github external
require('dotenv').config();
var IotClient = require('azure-iothub').Client;
var IotMessage = require('azure-iot-common').Message;
var iotClient = IotClient.fromConnectionString(process.env.IOT_CONN_STRING);
var fs = require('fs');
var express = require('express');
var app = module.exports.app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var port = process.env.PORT || 3000;
var azure = require('azure-storage');
var blobService = azure.createBlobService(process.env.AZURE_STORAGE_CONNECTION_STRING);
var tmp = require('tmp');
var path = require('path');

var baseUrlPath = 'https://tinygallery.blob.core.windows.net/pixelart/';
github nebrius / johnny-five-iot-edge / examples / call-method-on-device / index.js View on Github external
'use strict';

var Client = require('azure-iothub').Client;

var connectionString = '{iothub connection string}';
var methodName = 'write';
var deviceId = '{devicdId{}}';
var moduleId = '{Johnny5IoTEdgeModuleId}';

const write = {
    peripheral: {
      name: "alarm",
      type: "Led",
    },
    state: {
      method: "on"
    }
  };
github noopkat / electric-io / lib / liveHub.js View on Github external
function startService(options) {
  const { connectionString } = options;
  if (!connectionString) {
    throw new Error(
      "oops, you're missing a CONNECTION_STRING entry in ./.env!"
    );
  }

  const registry = iothub.Registry.fromConnectionString(connectionString);
  const client = iothub.Client.fromConnectionString(connectionString);
  return startHub(options).then(() => {
    return {
      listDevices: registry.list.bind(registry),
      callDeviceMethod: client.invokeDeviceMethod.bind(client),
      callDeviceMessage: client.send.bind(client)
    };
  });
}
github microsoft / devkit-sdk / AZ3166 / Test / VoiceToTwitter / examples / VoiceToTwitter / azureFunction / index.js View on Github external
'use strict';

const request = require('request');
const uuidV4 = require('uuid/v4');
const async = require('async');
const moment = require('moment');
const config = require('./config.json');
const connectionString = process.env['speechRecognitionIothub'];
const Message = require('azure-iot-common').Message;
const client = require('azure-iothub').Client.fromConnectionString(connectionString);

module.exports = function (context, myBlob) {
    const fileName = context.bindingData.name;
    async.waterfall([
        callback => {
            context.log('getting speech service api token...');
            const option = {
                method: 'POST',
                url: config.tokenRequestUrl,
                headers: {
                    'Ocp-Apim-Subscription-Key': process.env['speechServiceKey'],
                }
            };
            request(option, function (err, response, body) {
                if (err) {
                    callback(err);