user1295308
user1295308

Reputation: 445

How to integrate Azure IOT Hub with Ionic Mobile app?

I need to implement Notifications in my Ionic Application for Android. For that we have set up Azure IOT Hub where the device should get registered and from there, we can send messages to the device and vice versa (from the device to the cloud).

Now, I have gone through lots of links in the google regarding plugins for azure in Ionic, i tried them but in vain. I need to do two things

  1. Register the device ID with the Azure IOT Hub
  2. Send Notification in the Azure IOT Hub using that device ID, so that I can receive it in the device.

I have gone through

  1. https://github.com/Azure/azure-iot-sdk-node
  2. https://github.com/derek82511/cordova-azure-notification-hubs

None of them helped me to register device to the IOT Hub. Can some one guide me in the right direction. I need to know the process how to connect the Azure IOT Hub with my Ionic mobile app for Android.

Code:

var iothub = require('azure-iothub'); 
var connectionString = config.hubConnectionString;

      var registry = iothub.Registry.fromConnectionString(connectionString);

      // Create a new device
      var device = {
      deviceId: uuid
      };

      registry.create(device, function(err, deviceInfo, res) {
          if (err) {
            console.log( ' error: ' + err.toString());
            this.showAlert('Create Device Error',err.toString());
          }
          if (res) {
            console.log(' status: ' + res.statusCode + ' ' + res.statusMessage);
            this.showAlert('Create Device Success Response',`status: ${res.statusCode} ${res.statusMessage}`);
          }
          if (deviceInfo) {
            console.log(' device info: ' + JSON.stringify(deviceInfo));
            this.showAlert('Device Info',JSON.stringify(deviceInfo));
          }
      });

      //var clientFromConnectionString = require('azure-iot-device-http').clientFromConnectionString;
      //var Message = require('azure-iot-device').Message;

      var connectionString = config.hubConnectionString;

      var client = clientFromConnectionString(connectionString);

      var connectCallback = function (err) {
        if (err) {
          console.error('Could not connect: ' + err);
          this.showAlert('HTTP Connection Error',err.toString());

        } else {
          console.log('Client connected');
          var message = new Message('some data from my device');
          client.sendEvent(message, function (err) {
            if (err) {
              console.log(err.toString());
              this.showAlert('HTTP Send IOT Message Error',err.toString());
            }
          });

          client.on('message', function (msg) { 
            console.log(msg); 
            this.showAlert('IOT Received Message',msg);
            client.complete(msg, function () {
              console.log('completed');
            });
          }); 
        }
      };

      client.open(connectCallback);

Upvotes: 0

Views: 349

Answers (1)

silent
silent

Reputation: 16128

You need to use the IoT Hub Service SDK: https://www.npmjs.com/package/azure-iothub

This example is from that link and shows how to use the device registry to create a new device.

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

var connectionString = '[IoT Connection String]';

var registry = iothub.Registry.fromConnectionString(connectionString);

// Create a new device
var device = {
deviceId: 'sample-device-' + Date.now()
};

registry.create(device, function(err, deviceInfo, res) {
    if (err) console.log('error: ' + err.toString());
    if (res) console.log('status: ' + res.statusCode + ' ' + res.statusMessage);
    if (deviceInfo) console.log('device info: ' + JSON.stringify(deviceInfo));
});

Upvotes: 0

Related Questions