Run a simple NodeJS sample on B&R APC device running Windows 10
- Introduction
- Step 1: Sign Up To Azure IoT Hub
- Step 2: Register Device
- Step 3: Build and Validate the Sample using NodeJS Client Libraries
- Step 4: Troubleshooting
Introduction
About this document
This multi-step process includes:
- Configuring Azure IoT Hub
- Registering your IoT device
- Build and deploy Azure IoT SDK on device
- Packaging and sharing the logs
Prepare
Before executing any of the steps below, read through each process, step by step to ensure end to end understanding.
You should have the following items ready before beginning the process:
- Computer with Windows 10 having Git client installed and access to the azure-iot-sdk-node GitHub public repository.
-
Install Visual Studio 2015 and Tools. You can install any edition of Visual Studio, including the free Community edition.
Make sure to select the “Universal Windows App Development Tools”, the component required for writing apps Windows 10:
- Install the latest Node.js Tools for Visual Studio from here
-
Install the latest Node.js Tools for Windows IoT from here
-
Required hardware running Windows 10 IoT Core to certify.
Note: If you need assistance installing Windows 10 IoT Core , please visit https://www.windowsondevices.com or contact us at iotcert@microsoft.com
Step 1: Sign Up To Azure IoT Hub
Follow the instructions here on how to sign up to the Azure IoT Hub service.
As part of the sign up process, you will receive the connection string.
-
IoT Hub Connection String: An example of IoT Hub Connection String is as below:
HostName=[YourIoTHubName];SharedAccessKeyName=[YourAccessKeyName];SharedAccessKey=[YourAccessKey]
Step 2: Register Device
- Follow the instructions here on how to provision your device and get its credentials.
Step 3: Build and Validate the Sample using Node.js Client Libraries
This section walks you through the steps to create a UWP Node.js wrapper over existing Node.js client SDK using Visual Studio. Once done, you will build and deploy the IoT Client SDK, and validate the sample tests required for IoT certification with the Azure IoT SDK.
3.1 Connect the Device
-
Connect the board to your network using an Ethernet cable. This step is required, as the sample depends on internet access.
-
Power the board using a micro-USB cable or power adapter.
3.2 Build the Samples
-
Start a new instance of Visual Studio 2015.
-
Create a new project (**File New Project…**). In the New Project dialog, navigate to Node.js as shown below (in the left pane in the dialog: Templates JavaScript Node.js). Select the Node.js template (shown below). Enter a name for your project, for example NodeJsUWPSample. -
Select app.js and package.json file in Solution Explorer, Right click and select Delete.
-
Clone Azure IoT SDK repository to your Windows 10 machine.
-
In File explorer, Copy package.json and simple_sample_device.js files available under Node.js sample of the repository on your machine. For example if you cloned the azure-iot-sdk-node repository under C:\IOT directory, Go to (C:\azure-iot-sdk-node\device\samples)
-
Go back to Visual Studio, Right click your project in Solution Explorer and click Open Folder in File Explorer.
-
Paste the files you copied earlier in this folder.
-
Go back to Visual Studio, in Solution Explorer on the top context menu, click Show All Files.
-
(Optional for some Visual Studio) Right click both package.json and simple_sample_device.js files and choose Include in project option.
-
Right click the npm node and click Install Missing npm Packages to install the required packages for the solution.
-
Expand the npm node in Solution Explorer and right click all package(s) that shows not listed in package and click Uninstall npm Package(s).
-
Select simple_sample_device.js file and locate the following code in the simple_sample_device.js file:
var connectionString = ‘[IoT device connection string]’;
-
Replace
[IoT device connection string]
with the connection string for your device and Save the changes. You can get the connection string from DeviceExplorer as explained in Step 2. -
Right click the simple_sample_device.js file and from context menu choose “Set as Node.JS Startup File”.
-
Choose the right architecture (x86 or ARM, depending on your device) and set the debugging method to “Remote Machine”, or local machine.
-
To deploy the binaries on your device, right-click on the NodeJsUWPSample project in the Solution Explorer, select Properties and navigate to the General tab:
Type in the name or IP of your device.
- Build the solution.
3.3 Run and Validate the Samples
In this section you will run the Azure IoT client SDK samples to validate the communication between your device and Azure IoT Hub. You will send the messages to the Azure IoT Hub service and validate that IoT Hub has successfully receive the data. You will also monitor any messages sent from the Azure IoT Hub to client.
Note: Take screenshots of all the operations you will perform in this section. These will be needed in Step 4.
3.3.1 Send Device Events to IoT Hub
-
Launch the DeviceExplorer as explained in Step 2 and navigate to Data tab. Select the device name you created from the drop-down list of device IDs and click Monitor button.
-
DeviceExplorer is now monitoring data sent from the selected device to the IoT Hub.
-
In Visual Studio, from Solution Explorer, right-click the NodeJsUWPSample project, click Debug −> Start new instance to build and run the sample.
-
You should be able to see the events received in the DeviceExplorer’s data tab.
3.3.2 Receive messages from IoT Hub
-
To verify that you can send messages from the IoT Hub to your device, go to the Messages to Device tab in DeviceExplorer.
-
Select the device you created using Device ID drop down.
-
Add some text to the Message field, then click Send.
-
You should be able to see the message received in the Visual Studio Output window.
Step 4: Troubleshooting
Please contact engineering support on support@br-automation.com for help with troubleshooting.
Reference: Some information in this document is obtained from here.