1
votes

I'm doing a small project where I'm using a Raspberry PI to monitor temperature and controlling a LED using Azure IOT Hub. The temperature is visualized through a dashboard portal, where you also can control the LED. I've read the documentation thoroughly but it I'm still unsure about a couple of things:

Remote Monitoring:

The Raspberry PI currently sends temperature to my IoT Hub (Device2Cloud), everything looks fine on that part. In order to display the values sent from the Raspberry PI I'm reading off the Event bus from my NodeJS backend, in the same manner as they do in this sample: https://github.com/Azure-Samples/web-apps-node-iot-hub-data-visualization/blob/master/IoThub/iot-hub.js

Is this correct way to read device to cloud messages?

Remote Controlling

This is the part I'm very unsure about, I would like to control the LED that's connected to the Raspberry PI through Cloud2Device communication in my dashboard page. I'm not quite sure how to implement this in my Node JS backend, and I really cant find any good examples where this has been done. Any advice would be appreciated.

2

2 Answers

0
votes

Regarding the remote monitoring question: yes this will work although i want to point out that the event hubs SDK for Node is still preview (and might change a bit in the future) so you should expect some quirks.

Regarding "remote controlling": in order to send cloud-to-device messages you should use the Azure IoT Hub Service SDK for Node, and here is an example of how to send a cloud to device message (copied from here)

'use strict';

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

var connectionString = '[IoT Hub Connection String]';
var targetDevice = '[Target device that will receive the message]';

var client = Client.fromConnectionString(connectionString);

client.open(function (err) {
  if (err) {
    console.error('Could not connect: ' + err.message);
  } else {
    console.log('Client connected');

    // Create a message and send it to the IoT Hub every second
    var data = JSON.stringify({ text : 'foo' });
    var message = new Message(data);
    console.log('Sending message: ' + message.getData());
    client.send(targetDevice, message, function (err) {
      if (err) {
        console.error('Could not send: ' + err.message);
      } else {
        console.log('Message sent');
      }
    });
  }
});
0
votes

You have a couple of options for remotely controlling your device. You should review this article (https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-c2d-guidance) to determine which option is most appropriate for your scenario.

You can find a cloud-to-device messages tutorial here: https://docs.microsoft.com/azure/iot-hub/iot-hub-node-node-c2d

You can find a direct methods tutorial here: https://docs.microsoft.com/azure/iot-hub/iot-hub-node-node-direct-methods