Blinky Node.js (Windows Universal) Sample with Lightning
In this sample, we will use the Lightning GPIO provider to blink an LED attached to a Raspberry Pi 2.
It also includes steps to reference a custom winmd file in your Node.js project. This sample is based on the Blinky sample
and shares the same setup steps as well as most of the code. The key difference is setting the Lightning provider as the default controllers provider.
This sample only works with the Windows 10 IoT Core Anniversary Update (Build 14393) release with Visual Studio 2015 and does not currently work with any newer Windows releases or Visual Studio 2017. We are looking into adding Node.js support to UWP in a future release of Windows 10 IoT Core.
Install the latest Node.js Tools for Windows IoT from here.
Download nuget.exe to your machine and add its path the to ‘Path’ environment variable.
Set up your hardware
The setup for this sample is the same as the C# ‘Blinky’ sample.
Follow the steps on this page to set up Lightning on your Raspberry Pi 2.
Create a new Node.js (Windows Universal) project
Use the Basic Node.js Web Server (Windows Universal) template.
When the project has been created, open up server.js and replace the existing code with the code shown below:
Here’s what the code above is doing:
We use the node-uwp npm package (included in your project by default) to allow the code to use UWP APIs (within Windows and Microsoft namespaces).
Check if Lightning is enabled and set it as the default provider.
GpioController.getDefault() is called to get the GPIO controller.
Then we attempt to open the pin by calling GpioController.openPin() with the LED pin value.
Once we have the pin, we set it to be off (high) by default using the GpioController.write() function.
Every 1000 milliseconds (1 second), the value of the LED is checked and then set to the opposite of the current value.
Add Microsoft.IoT.Lightning.Providers library to your project
In the Solution Explorer in your project, right click on the project node (with server.js), select ‘Open Command Prompt Here…’
Run nuget install Microsoft.IoT.Lightning -Pre
In the Solution Explorer, right click on your Node.js project again, select ‘Add->Existing Item…’. then add the following files:
Open the Package.appxmanifest file. Add the capabilities below:
Then add the extension below:
Build the solution.
Enable the Lightning Direct Memory Mapped driver on your Windows IoT Core device
Go to this link to see how to enable your device to use Lightning.
Deploy the server to your Windows IoT Core device
Go to the Project menu and select ‘<Your project name> Properties’ (You could also right-click on the project node in solution explorer to access Properties). Enter the IP Address in the Remote Machine text box. If you’re building for Minnowboard Max, select x86 in the dropdown. If you’re building for Raspberry Pi 2 or 3, select ARM.
Now we’re ready to deploy to the remote Windows IoT Core device. Simply press F5 (or select Debug | Start Debugging) to start the app.