Category Archives: Windows IoT

node.js on Windows IoT

Hello World application using node.js on a Windows IoT-capable Raspberry Pi 2

I’m a big fan of node.js on embedded devices and was pleased to hear that Microsoft would be making node available for Windows IoT.

What makes node great for embedded devices is that it is asynchronous, enables tight/small applications, has a rich ecosystem for extensions and offers built-in web services. It’s trivial to set up a RESTful API and web server, which both come in handy for talking to and managing devices.

For Windows on Arm (WoA), like the Raspberry Pi 2 (RP2), Microsoft’s implementation of node.js is based on the Chakra Javascript engine. As a result, there is a different process to get node.js on a Windows IoT device, as compared to downloading and building node for a Linux target device.

In the following steps below, we’ll work towards running a Hello World node app on a Windows IoT-enabled RP2. I’ve used the Microsoft developer guide to run your first node application which in addition showing how to load node on a device, outlines how you might interface to the underlying Windows IoT system via a C++ extension.

Getting node.js on to your Windows IoT device

The first thing that we must do is download the “Node.js Tools for Windows IoT“, which includes the Chakra version of Node.js and tools to help manage node for devices. You may need to scroll down a bit to find the Node.js Tools, but once you do, run the installer to install this to your Windows development machine.

Install Node.js Tools for Windows IoT

Install Node.js Tools for Windows IoT

You’ll also need to download Python 2.7 for your Windows development machine. There are multiple versions available, but to be consistent with Microsoft’s documentation, I pressed the Python 2.7.X button.

With the necessary tools installed, you need to build and copy the Chakra node engine for the RP2. To do this you must first gain access to the device. Use Windows Explorer and enter:

\\<IP Address of the RP2>\C$

You will be prompted for the username (<IP Address of the device>\Administrator) and password (p@ssw0rd). This step is important because it allows your development machine to have access to the necessary target locations on the RP2 where node is to be loaded.

Access Windows IoT device

Access Windows IoT device

With that in place, we’re ready to build and load node.js. To do this, we’ll run a PowerShell script called CopyNodeChakra.ps1, using the following command:

& 'C:\Program Files (x86)\Node.js (chakra)\CopyNodeChakra.ps1' -arch ARM -ip <IP Address of the RP2>

If the security policy limits successful completion of this command, open PowerShell as Admin or run via a PowerShell ByPass. If successful, node will be copied to the C:\Node.js (Chakra) directory. (In the picture below you can see that I used the ByPass to complete the script.)

Copy Chakra to IoT Device

Copy Chakra to IoT Device

To verify that node is properly installed, you can start a PuTTY SSH session to the RP2 and navigate to the install location then issue the familiar node and npm version commands:

Check node and npm versions

Check node and npm versions

Run Hello World on a Windows IoT RP2

With node on the RP2, we’re ready to run our hello world application. I copy-pasta’d a very simple application using a text editor, but if it’s a more complex application, you can use Visual Studio to manage development. Since I had a Windows Explorer session opened, I just copied my hello.js application to the RP2 and put it in the directory in which node was installed – of course, as the node application matures, you’ll want to create a separate deployment directory.

Hello World node application (Hello.js)

Hello World node application (Hello.js)

Before we run the application, we need to ensure that the node.exe can communicate through the firewall. We’ll need to connect to the device, using PowerShell and issue the following commands:

' Start the WinRM service for remote connections
net start WinRM
' Set as trusted host
Set-Item WSMan:\localhost\Client\TrustedHosts -Value <IP Address of the RP2>
' Initiate a session with the RP2
Enter-PSSession -ComputerName  -Credential <IP Address of the RP2>\Administrator
' With the remote session established, allow the firewall exception for node.exe
netsh advfirewall firewall add rule name="Node.js" dir=in action=allow program="C:\Node.js (Chakra)\Node.exe" enable=yes

Finally, we’ll use our PuTTY session to run our node Hello World application and then connect to our Windows IoT device using a browser to see the results:

Hello World on Node for Windows IoT

Hello World on Node for Windows IoT

Advertisements

Connecting a device to Azure IoT Suite

It makes sense to have a cloud-based aggregation system for my embedded “IoT” device(s), so I wanted to explore Microsoft’s Azure IoT suite. There’s a lot of marketing fluff, but I wanted to see how quickly I could get up and running. So let’s try …

What is the Azure IoT Suite?

The Azure IoT Suite includes a host of services, which combined allow for the large-scale collection of data from IoT devices (Microsoft marketing speak: “Cloud-scale telemetry ingestion from website, apps and devices”). Other services allow for the (a) analysis of the incoming data, (b) out-going control messaging to the devices and (c) presentation of the data in real-time.

Azure IoT Services

List of services available as a part of the Azure IoT Suite

Perhaps more important for developers is understanding how the data flows from devices through the gateway to the presentation layer. In our example, we’ll be using an IP-capable device which will send messages directly to the Azure IoT Hub (“Cloud Gateway”) and we’ll view the information in the presentation layer.

Azure IoT Data Flow Architecture

Azure IoT Data Flow Architecture

To help us quickly get up and running, the Azure IoT Suite offers:

  • An Azure IoT SDK toolset which will allow us to quickly integrate the device’s connection to Azure. While there is support for multiple languages and platforms, we’ll use the node.js tool set.
  • Azure offers a set of predefined presentation views which are based on the type of usage they intend to provide. The presentation view is an auto-generated website in the azurewebsites.net domain based on the selected name of the IoT Hub.
Example of a Pre-Defined Azure IoT Presentation Website

Example of a Pre-Defined Azure IoT Presentation Website

Seting up our Azure IoT Suite

The first step that you have to go through is to create an Azure account. I started the process at the Microsoft Azure IoT Suite page’s ‘Try Now’ button. There is a 30-day free trial option, but you still need to give a credit card. The overall procedure is pretty quick and takes about 5 minutes from the time you start to the time you are connected to your Azure portal manager.

Azure IoT Home Page

This next step of the two step procedure creates a presentation website based on the type of business model that best represents your enterprise. At the time of this blog entry, the only option was ‘Remote Monitoring’ though ‘Predictive Maintenance’ is planned for the future.

Azure IoT Pre-Configured Solutions

Azure IoT Pre-Configured Solutions

Once the Azure account is set up, I recommend using the newer management portal, called Azure Preview Portal, to view and manage the Azure iOT services. I favorited the ‘IoT Hub’ to allow me to easily access the various services available as part of the Azure IoT Suite.

Azure Preview Portal with IoT Suite

Azure Preview Portal with IoT Suite

Connecting a Device to Azure IoT

With our IoT suite set up, we are ready to code. While I plan to use an embedded Linux device which will be running a node, I decided to use Visual Studio 2015 Community Edition as my IDE in this project. By default, node.js project templates aren’t included in VS2015, so you need to install them.

Node.js Tools 1.1 RC2 for Visual Studio 2015

Microsoft has provided a bunch of sample code – so you’ll want to clone the repo at https://github.com/Azure/azure-iot-sdks, and then start a Node.js project based on existing code. (Since I’m going to be transferring the JavaScript source to my device from my Windows development environment using WinSCP (which I can’t recommend highly enough – please donate I did), I’m not too particular on the directory structure because the work will happen on the target, I’m mostly focusing on having good access to the provided source from Microsoft.)

VS2015 Node.js Project

VS2015 Node.js Project

I’ll be connecting my device based on the sample code provided in the remote-monitoring.js file. Looking in this source, we’ll see that we need some key pieces of information from our Azure IoT Suite, specifically information on the deviceID, deviceKey, hubName and hubSuffix.

Adding a device to Azure IoT Suite

Adding a device to Azure IoT Suite

To get this information, we’ll need to create a device using the presentation view – at the bottom of the page, select the Add Device icon and follow the wizard to generate the necessary device identification parameters.

Add Device to Azure IoT Suite

Add Device to Azure IoT Suite

After I updated the remote-monitoring.js file, I used WinSCP to move it to my target device.

Transfer Node.js Files to Debian Target

We’ll then SSH into our target, use npm to install the ‘azure-iot-device’ dependency and then start the node application. Within 5 seconds you should see telemetry data coming in from our device. The sample code auto-generates temperature values and we can see them being updated every second.

Azure IoT Ingesting Data from our Node Device

Azure IoT Ingesting Data from our Node Device

So it took me a couple of hours from the time I started to seeing data being reported live on my Azure presentation layer. The fact that I can quickly add disparate IP devices to such a power back end is pretty remarkable. In a future post I’ll investigate more things that we can do now that we’re ingesting data.

Installing Windows IoT on a Raspberry Pi 2

With Windows 10, Microsoft is reintroducing their embedded systems strategy as part of their Windows 10 roll-out under the Windows IoT umbrella. From a seminar that I was at, Microsoft introduced three Windows IoT editions which are dependent on the platform/functionality target: from small embedded solutions (Windows 10 IoT Core), through mobile solutions (Windows 10 IoT for Mobile Devices) to more powerful industrial applications (Windows 10 IoT for Industry Devices).

Windows IoT Editions

Windows 10 IoT Editions

In this post, we’ll explore the Windows IoT Core by first installing it on a Raspberry Pi 2 and then running a small application.

Installing Windows 10 IoT Core

Download the Windows SD Card Image File

For the first step, Microsoft has provided instructions on how to install on a Raspberry Pi 2, so we’re not breaking new ground, just trying to replicate their guidelines.

After downloading the ISO for Raspberry Pi 2 from the Microsoft Download Center, run the Windows_10_IoT_Core_RPi2.msi to install the necessary files and tools on your system.

The first step is to sign in to your Windows Connect and download the Windows IoT Core preview for Raspberry Pi 2. After filling out the surveys (EULA agreements), proceed to download link for the Windows IoT Raspberry Pi 2 Preview. Once downloaded, extract the flash.ffu file, which will be used to prepare the SD card with a Windows 10 IoT Core image.

As of mid-June 2015, Microsoft now has simplified the process greatly. There is now a nice installer, which comes with a tool to flash the SD card auto-magically.

Install Microsoft IoT for Raspberry Pi 2

Install Microsoft IoT for Raspberry Pi 2

As per Microsoft’s recommendation, I purchased a Class 10 16GB Micro-SD.

Class 10 16GB Micro-SD card

Class 10 16GB Micro-SD card

Next, we’ll use the IoTCoreImageHelper.exe utility as part of the install package to flash the SD card – you can find it by searching for IoTCoreImageHelper. Previous incarnations of this process used DISM, but there were complaints of frequent errors, which I also saw. This new tool finds the SD card, allows you specify the location of the flash.ffu file and then flashes the SD card for you – much simpler now!

Windows IoT Core Image Helper

Windows IoT Core Image Helper

With the SD card ready, we need to hookup the Raspberry Pi 2 – you’ll need power, a display monitor, HDMI to a monitor, a USB mouse, an Ethernet connection and a power supply. Fortunately, I got most of it in the RP2 kit I bought – but it not always easy to hook all of this up in a compact area (as you’ll see in my video). The SD card slot is at the bottom of the card. Once you power up, you’ll see the LEDs flashing and about 15 seconds later the monitor light up with the initial screen. Then things go black and the LED is red for what seems like an awfully long time but if you’re patient – I power cycled twice before I decided to wait – you’ll eventually get a Windows IoT splash screen and finally you’ll end up in the start screen.

As part of the Windows IoT install, there is also another utility which monitors the network for attached Windows IoT devices. We see now that there appears to be a minwinpc online!

Windows IoT Core Watcher

Windows IoT Core Watcher

In my next post, we’ll see if we can’t write our first program for Windows IoT.