Introduction
The aim of this tutorial is to connect numerous virtual remote cameras, placed at different locations, to a single IoT hub.
Prerequisites
Road Map
- Create an Azure IoT Hub
- Register sample devices with the IoT hub
- Define the data for the simulated cameras
- Create a set of simulated camera devices
- Verify the registered devices
Create an Azure IoT Hub
- Open your browser and log into portal.azure.com.
- Click on "create a resource".
- Click on the "Internet of Things" and select "IoT Hub".
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>
- Select the free tier to save useful resources.
- Wait for the validation and click on "Create" on the same window after the validation. This will start the deployment process.
Register sample devices with the IoT hub
Note
- Click to open the scripting panel.
- You will not see the 2nd button, i.e., "Create storage" if you already have a storage created.
- Select "Bash" as displayed below.
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.
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
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.
- [
- {
- "deviceId" : "cam_0001",
- "latitude" : 75.401451,
- "longitude" : -95.722518,
- "key" : ""
- },{
- "deviceId" : "cam_0002",
- "latitude" : 75.027715,
- "longitude" : -96.041859,
- "key" : ""
- },{
- "deviceId" : "cam_0003",
- "latitude" : 74.996653,
- "longitude" : -96.601780,
- "key" : ""
- },{
- "deviceId" : "cam_0004",
- "latitude" : 75.247701,
- "longitude" : -96.074436,
- "key" : ""
- },{
- "deviceId" : "cam_0005",
- "latitude" : 75.044926,
- "longitude" : -93.651951,
- "key" : ""
- },{
- "deviceId" : "cam_0006",
- "latitude" : 75.601571,
- "longitude" : -95.294407,
- "key" : ""
- },{
- "deviceId" : "cam_0007",
- "latitude" : 74.763102,
- "longitude" : -95.091160,
- "key" : ""
- },{
- "deviceId" : "cam_0008",
- "latitude" : 75.473988,
- "longitude" : -94.069432,
- "key" : ""
- },{
- "deviceId" : "cam_0009",
- "latitude" : 75.232307,
- "longitude" : -96.277683,
- "key" : ""
- },{
- "deviceId" : "cam_0010",
- "latitude" : 74.658811,
- "longitude" : -93.783787,
- "key" : ""
- }
- ]
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.
- var fs = require('fs');
- var iothub = require('azure-iothub');
- var registry = iothub.Registry.fromConnectionString('CONNECTION_STRING');
-
- console.log('Reading devices.json...');
- var devices = JSON.parse(fs.readFileSync('devices.json', 'utf8'));
-
- console.log('Registering devices...');
- registry.addDevices(devices, (err, info, res) => {
- registry.list((err, info, res) => {
- info.forEach(device => {
- devices.find(o => o.deviceId === device.deviceId).key = device.authentication.symmetricKey.primaryKey;
- });
-
- console.log('Writing cameras.json...');
- fs.writeFileSync('cameras.json', JSON.stringify(devices, null, 4), 'utf8');
- console.log('Done');
- });
- });
- 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.
- 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
- Can be used as remote access for security cameras.
- Can be used with motion detection sensors for wildlife photography or traffic monitoring.
- IoT hub can be used to connect numerous devices other than cameras.
- Can be used to monitor employee activities at a workstation remotely.