Create An Azure IoT Hub To Connect To Cameras

Introduction

 
The aim of this tutorial is to connect numerous virtual remote cameras, placed at different locations, to a single IoT hub.
 
Create An Azure IoT Hub To Connect To Cameras
 
Prerequisites
 
You require an Azure subscription to perform these steps. If you don't have one, you can create one by following the steps outlined on the Create your Azure free account today webpage.
 

Road Map

  1. Create an Azure IoT Hub
  2. Register sample devices with the IoT hub

    1. Define the data for the simulated cameras
    2. Create a set of simulated camera devices

  3. Verify the registered devices 

Create an Azure IoT Hub

  • Open your browser and log into portal.azure.com.
  • Click on "create a resource".

    Create An Azure IoT Hub To Connect To Cameras
  • Click on the "Internet of Things" and select "IoT Hub".

    Create An Azure IoT Hub To Connect To Cameras

    Subscription: < select your own subscription >

    Resource group: Test-IOT-RG

    Region: < select a Datacenter location nearest to you. Note: All subsequent resources that you create must be in the same location.>

    IoT hub name: 0<Select a name for your Hub>

    Create An Azure IoT Hub To Connect To Cameras
  • Select the free tier to save useful resources.

    Create An Azure IoT Hub To Connect To Cameras
  • Wait for the validation and click on "Create" on the same window after the validation. This will start the deployment process.

    Create An Azure IoT Hub To Connect To Cameras

Register sample devices with the IoT hub

Note
  1. Click to open the scripting panel.
  2. You will not see the 2nd button, i.e., "Create storage" if you already have a storage created.

    Create An Azure IoT Hub To Connect To Cameras
  • Select "Bash" as displayed below.

    Create An Azure IoT Hub To Connect To Cameras

    Now, you have entered the Bash Shell.
  • Create a new directory and access it through the following commands.

    mkdir photoproc //creates a directory
    cd photoproc //enters the directory
  • Execute the following commands in sequence to initialize the project directory to host a Node project and install the required packages.

  • Npm is a software package manager and installer.

    npm init -y //initialize the project directory
    npm install azure-iothub –save // install packages that Node can use to communicate with Azure IoT hubs
    npm install azure-iot-device azure-iot-device-mqtt –save //install a trio of packages that Node can use to communicate with Azure IoT hubs
Note
Try downloading and installing Node.js from https://nodejs.org/ if the above lines don’t execute.
 
Create An Azure IoT Hub To Connect To Cameras
 

Define the data for the simulated cameras

  • After successful execution, the following screen should appear.
  • Using the Cloud Shell, create a new file in the project directory you created earlier and name it as "devices".

    code devices.json //create a file

    Create An Azure IoT Hub To Connect To Cameras
Copy the following JSON in the file you created above and save it. This file defines ten virtual cameras that will transmit events to the IoT hub.
  1. [  
  2.     {  
  3.         "deviceId" : "cam_0001",  // device ID assigned                
  4.         "latitude" : 75.401451,   // location information     
  5.         "longitude" : -95.722518,  
  6.         "key" : ""           //key will be assigned when the device is registered  
  7.     },{  
  8.         "deviceId" : "cam_0002",  
  9.         "latitude" : 75.027715,  
  10.         "longitude" : -96.041859,  
  11.         "key" : ""  
  12.     },{  
  13.         "deviceId" : "cam_0003",  
  14.         "latitude" : 74.996653,  
  15.         "longitude" : -96.601780,  
  16.         "key" : ""  
  17.     },{  
  18.         "deviceId" : "cam_0004",  
  19.         "latitude" : 75.247701,  
  20.         "longitude" : -96.074436,  
  21.         "key" : ""  
  22.     },{  
  23.         "deviceId" : "cam_0005",  
  24.         "latitude" : 75.044926,  
  25.         "longitude" : -93.651951,  
  26.         "key" : ""  
  27.     },{  
  28.         "deviceId" : "cam_0006",  
  29.         "latitude" : 75.601571,  
  30.         "longitude" : -95.294407,  
  31.         "key" : ""  
  32.     },{  
  33.         "deviceId" : "cam_0007",  
  34.         "latitude" : 74.763102,  
  35.         "longitude" : -95.091160,  
  36.         "key" : ""  
  37.     },{  
  38.         "deviceId" : "cam_0008",  
  39.         "latitude" : 75.473988,  
  40.         "longitude" : -94.069432,  
  41.         "key" : ""  
  42.     },{  
  43.         "deviceId" : "cam_0009",  
  44.         "latitude" : 75.232307,  
  45.         "longitude" : -96.277683,  
  46.         "key" : ""  
  47.     },{  
  48.         "deviceId" : "cam_0010",  
  49.         "latitude" : 74.658811,  
  50.         "longitude" : -93.783787,  
  51.         "key" : ""  
  52.     }  

Create a set of simulated camera devices

  • Execute the following command in the project directory to create a file and run it in the online code editor.

    code deploy.js //creates a file
  • Copy an save the following code in the file created above. This code parses and registers all the simulated devices defined in the JSON with the IoT hub that you created earlier.
    1. var fs = require('fs'); //Required to use the filesystem Ron your computer  
    2. var iothub = require('azure-iothub'); //Requires Azure-IoT Hub   
    3. var registry = iothub.Registry.fromConnectionString('CONNECTION_STRING');  
    4.   
    5. console.log('Reading devices.json...');  
    6. var devices = JSON.parse(fs.readFileSync('devices.json''utf8'));  
    7.   
    8. console.log('Registering devices...');  
    9. registry.addDevices(devices, (err, info, res) => {  
    10.     registry.list((err, info, res) => {  
    11.         info.forEach(device => {  
    12.             devices.find(o => o.deviceId === device.deviceId).key = device.authentication.symmetricKey.primaryKey;  
    13.         });  
    14.   
    15.         console.log('Writing cameras.json...');  
    16.         fs.writeFileSync('cameras.json', JSON.stringify(devices, null, 4), 'utf8');  
    17.         console.log('Done');  
    18.     });  
    19. });  
  • Now, leave the editor open and execute the following command in Azure CLI.

    az iot hub show-connection-string --name $HUB_NAME

    Note
    type the name of the IoT Hub you created instead of $HUB_NAME
  • Copy the connection string from the output and replace line 3 of deploy.js file with it.

    Create An Azure IoT Hub To Connect To Cameras
  • Execute the following command to run deploy.js.

    node deploy.js
  • The following output will be generated.

    Reading devices.json...
    Registering devices...
    Writing cameras.json...
    Done

Verify the registered devices

  • Type the following command in the Azure Shell

    az extension add --name azure-cli-iot-ext
  • To get a list of registered devices, execute the following command.

    az iot hub device-identity list -n $HUB_NAME | grep deviceId

    Note
    type the name of the IoT Hub you created instead of $HUB_NAME
  • Verify the file named camera.json through the following command.

    ls cameras.json

Use Cases

  1. Can be used as remote access for security cameras.
  2. Can be used with motion detection sensors for wildlife photography or traffic monitoring.
  3. IoT hub can be used to connect numerous devices other than cameras.
  4. Can be used to monitor employee activities at a workstation remotely.


Similar Articles