1
votes

I'm fiddling around with Azure IoT central and I configured a device. Now I want to send data using MQTT from a real device (no code). I can't seemed to find information if this is possible for IoT central.

For IoT hub I found: Azure Iot Hub MQTT

I want to use IoT Central because of the built-in dashboards. Those do not seem to exist for IoT hub. If I can't send data directly to IoT central, is there a way to use the IoT hub devices in IoT central?

1
Hi Sebastian, when you say "real device (no code)", what do you mean exactly? Like in Peter's answer, you can program a device to connect to IoT Central's in-built hub.Matthijs van der Veer
It's a device you cannot program. You can only configure the MQTT settings. My device works when using IoT hub, And I thought the same was possible for IoT central. A quote from the page I mentioned "If a device cannot use the device SDKs, it can still connect to the public device endpoints using the MQTT protocol on port 8883" and this is possible for IoT hubSebastian S

1 Answers

1
votes

Azure IoT Central uses an IoT Hub in the background, so you can still connect to the public device endpoints using the MQTT protocol on port 8883.

To get the address of the hub you can use the script below on any machine based on the device information in the Azure IoT Central app (see the docs)

enter image description here

// npm install azure-iot-device azure-iot-device-mqtt azure-iot-provisioning-device-mqtt azure-iot-security-symmetric-key --save

"use strict";

// Use the Azure IoT device SDK for devices that connect to Azure IoT Central.
var iotHubTransport = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').Client;
var Message = require('azure-iot-device').Message;
var ProvisioningTransport = require('azure-iot-provisioning-device-mqtt').Mqtt;
var SymmetricKeySecurityClient = require('azure-iot-security-symmetric-key').SymmetricKeySecurityClient;
var ProvisioningDeviceClient = require('azure-iot-provisioning-device').ProvisioningDeviceClient;

var provisioningHost = 'global.azure-devices-provisioning.net';
var idScope = '{your Scope ID}';
var registrationId = '{your Device ID}';
var symmetricKey = ''{your Primary Key}';
var provisioningSecurityClient = new SymmetricKeySecurityClient(registrationId, symmetricKey);
var provisioningClient = ProvisioningDeviceClient.create(provisioningHost, idScope, new ProvisioningTransport(), provisioningSecurityClient);

provisioningClient.register((err, result) => {
    if (err) {
      console.log('Error registering device: ' + err);
    } else {
      console.log('Registration succeeded');
      console.log('Assigned hub=' + result.assignedHub);
      console.log('DeviceId=' + result.deviceId);
      var connectionString = 'HostName=' + result.assignedHub + ';DeviceId=' + result.deviceId + ';SharedAccessKey=' + symmetricKey;
      console.log(connectionString);
    }
  });

Output:

Registration succeeded
Assigned hub=iotc-xxx.azure-devices.net
DeviceId=xxx
HostName=xxx.azure-devices.net;DeviceId=xxx;SharedAccessKey=xxx=

In addition, as stated by Matthijs van der Veer, do note that IoT Central uses the Device Provisioning Service to enable your device to connect to an IoT hub. It assigns an IoT hub to the device when registering but if the device gets reassigned to a different hub, the device will lose connection.