AZURE:Control a device connected to an IoT hub

3 min readOct 23, 2018



  1. Sign in to the Azure portal.
  2. Select Create a resource > Internet of Things > IoT Hub.

3. In the IoT hub pane, enter the following information for your IoT hub:

  • Subscription: Choose the subscription that you want to use to create this IoT hub.
  • Resource group: Create a resource group to contain the IoT hub or use an existing one. By putting all related resources in a group together, such as TestResources, you can manage them all together. For example, deleting the resource group deletes all resources contained in that group. For more information, see Use resource groups to manage your Azure resources.
  • Region: Select the closest location to your devices.
  • Name: Create a unique name for your IoT hub. If the name you enter is available, a green check mark appears.

4. Select Next: Size and scale to continue creating your IoT hub.

5. Choose your Pricing and scale tier. For this article, select the F1 — Free tier if it’s still available on your subscription. For more information, see the Pricing and scale tier.

6. Select Review + create.

7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You can monitor the progress in the Notifications pane.

Register a device:

click on add new device:

fill deviceId Name:

code for communicating with azure

‘use strict’;

const chalk = require(‘chalk’);

var connectionString = ‘’;

var Mqtt = require(‘azure-iot-device-mqtt’).Mqtt;
var DeviceClient = require(‘azure-iot-device’).Client
var Message = require(‘azure-iot-device’).Message;

var client = DeviceClient.fromConnectionString(connectionString, Mqtt);

// Timeout created by setInterval
var intervalLoop = null;

// Function to handle the hellotele direct method call from IoT hub
function on hellotele(request, response) {
// Function to send a direct method reponse to your IoT hub.
function directMethodResponse(err) {
if(err) {
console.error(‘An error ocurred when sending a method response:\n’ + err.toString()));
} else {
console.log(‘Response to method \’’ + request.methodName + ‘\’ sent successfully.’ ));

console.log(‘Direct method payload received:’));

// Check that a numeric value was passed as a parameter
if (isNaN(request.payload)) {
console.log(‘Invalid interval response received in payload’));
// Report failure back to your hub.
response.send(400, ‘Invalid direct method parameter: ‘ + request.payload, directMethodResponse);

} else {

// Reset the interval timer
intervalLoop = setInterval(sendMessage, request.payload * 1000);

// Report success back to your hub.
response.send(200, ‘Telemetry interval set: ‘ + request.payload, directMethodResponse);

// Send a telemetry message to your hub
function sendMessage(){
// Simulate telemetry.
var temperature = 20 + (Math.random() * 15);
var message = new Message(JSON.stringify({
temperature: temperature,
humidity: 60 + (Math.random() * 20)
}));‘temperatureAlert’, (temperature > 30) ? ‘true’ : ‘false’);

console.log(‘Sending message: ‘ + message.getData());

// Send the message.
client.sendEvent(message, function (err) {
if (err) {
console.error(‘send error: ‘ + err.toString());
} else {
console.log(‘message sent’);

// Set up the handler for the hellotele direct method call.
client.onDeviceMethod(‘hellotele’, onhellotele);

// Create a message and send it to the IoT hub, initially every second.
intervalLoop = setInterval(sendMessage, 1000);




DevOps/Cloud | 2x AWS Certified | 1x Terraform Certified | 1x CKAD Certified | Gitlab