1

im trying to send messages over the MQTT protocol to Azure IOT hub. Im using this code in NodeJS:

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

var connectionString = 'HostName=myhostname.azure- 
devices.net;DeviceId=Arsenal;SharedAccessKey=mysharedaccesskey';

var client = clientFromConnectionString(connectionString);


var connectCallback = function (err) {
  if (err) {
    console.error('Could not connect: ' + err);
  } else {
    console.log('Client connected');
    var message = new Message('some data from my device');
    client.sendEvent(message, function (err) {
      console.log("Message sent!");
      if (err) console.log(err.toString());
 });

client.on('message', function (msg) {
  console.log(msg);
  client.complete(msg, function () {
    console.log('completed');
   });
  });
 }
};

client.open(connectCallback);

This prints "Client connected!" and "Message sent!" Maybe this is a really silly question, but where in IoT Hub can i see the message that has been sent?

Hi7651
  • 15
  • 5
  • 2
    Maybe [this](https://stackoverflow.com/questions/35381303/how-to-test-messages-arriving-in-azure-iothub) can help? – Peter Bons Feb 04 '19 at 08:33
  • 1
    You can also use Azure CLI to monitor the events: https://learn.microsoft.com/en-us/cli/azure/ext/azure-cli-iot-ext/iot/hub?view=azure-cli-latest#ext-azure-cli-iot-ext-az-iot-hub-monitor-events – Yi Zhong - MSFT Feb 07 '19 at 19:01

1 Answers1

1

Three suggestions:

Azure CLI

  • At a PowerShell prompt type

    • az login

    The az login command will open a new web browser window and ask your to log into your Azure Subscription account.

  • Next we need to generate a SAS token. We can do this by running this command in the Azure CLI window:

    • az iot hub generate-sas-token --duration -n
  • Now we have our SAS Token, we can start monitoring the hub messages by running the following command:

    • az iot hub monitor-events --hub-name

for more details please read: https://github.com/AzureIoTGBB/How-To-Monitor-Azure-IoT-Hub-with-Azure-CLI-2.0

Azure Function

  • Create a Function App by clicking the below in the Azure Portal “Create New” blade. This is the “container” which holds your functions.
  • Once the Function is created, navigate to the “Create new function from template page”: Select IoT Hub (Event Hub) and Javascript or C# as the language Conveniently, Azure Functions will create the connection with IoT Hub for you after selecting this template,
  • Next you just have to click on the "new" button and select the proper IoT Hub instance:
  • Click "Create" and the Function just created will be triggered by the IoT Hub events
  • The function is created with boilerplate code console logging the messages

Device Explorer for IoT Hub Devices

On the read me of the repo it describes how to download, build and use the Device Explorer tool:

Also there is a another thread that can be useful since it describes how to use it: How to test Messages arriving in Azure IoTHub

Alberto Vega
  • 532
  • 4
  • 13