Node.js* templates for the Intel® XDK

The source code for these templates can be found here: https://github.com/gomobile?query=iotapp or download the Intel® XDK to check out all the Node.js* IoT application templates.

IoT Projects Panel Sept. 2014

Introduction

Intel® XDK is a HTML5 hybrid and Node.js application development environment that you can use to deploy, run, and debug apps for various IoT platforms, such as the Intel® Galileo board and Intel® Edison board. With the Linux* image distributed as part of the Intel® IoT Developer Kit and the Grove* Starter Kit Plus – Intel® IoT Edition, your development platform is ready to connect to the Intel XDK and run your Node.js applications. Along with its development features, the Intel XDK provides a variety of Node.js templates and samples intended for running on Intel IoT platforms. For more information on getting started, see Getting started with the Intel® XDK.

Purpose

The templates distributed with the Intel XDK provide compelling functionality, such as ways to handle analog and digital data transmitted to and received from sensors connected to I/O pins on your board. To communicate with sensors, each of the relevant templates uses the MRAA sensor communication library. The intent of this library is to make it easier for developers and sensor manufacturers to map their sensors and actuators on top of supported hardware and to allow control of low-level communication protocol by high-level languages and constructs.

Design considerations

Each of the templates require the MRAA library and Intel® XDK daemon to be installed on your board. These two requirements are included in the Linux image distributed with the Intel IoT Developer Kit, which enables communication between your board and Intel XDK and allows access to the I/O pins.

Development and testing

Every template has been tested on Intel® Galileo Generation 1 and 2 boards, as well as the Intel® Edison board.

Input/output templates

Templates for applications with mobile companion apps

Bluetooth templates

Templates for the web

Node.js templates for the Intel® XDK

OnBoard LED Blink

This simple Node.js application blinks the onboard LED on your Intel® IoT Platform.

Source: https://github.com/gomobile/iotapp-template-onboard-led-blink

var mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console
var myOnboardLed = new mraa.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Galileo Gen1 & Gen2 plus Edison Arduino Breakout board)
myOnboardLed.dir(mraa.DIR_OUT); //set the gpio direction to output

Analog Read

This simple Node.js application reads data from analog pins on your Intel® IoT Platform.

Source: https://github.com/gomobile/iotapp-template-analog-read

var mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
var analogPin0 = new mraa.Aio(0); //setup access analog input Analog pin #0 (A0)
var analogValue = analogPin0.read(); //read the value of the analog pin
console.log(analogValue); //write the value of the analog pin to the console

Digital Read

This simple Node.js application reads data from digital pins on your Intel® IoT Platform.

Source: https://github.com/gomobile/iotapp-template-digital-read

var mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
var myDigitalPin6 = new mraa.Gpio(6); //setup digital read on Digital pin #6 (D6)
myDigitalPin6.dir(mraa.DIR_IN); //set the gpio direction to input

Digital Write

This simple Node.js application writes data to digital pins on your Intel® IoT Platform.

Source: https://github.com/gomobile/iotapp-template-digital-write

var mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
var myDigitalPin5 = new mraa.Gpio(5); //setup digital read on Digital pin #5 (D5)
myDigitalPin5.dir(mraa.DIR_OUT); //set the gpio direction to output
myDigitalPin5.write(1); //set the digital pin to high (1)

PWM

This simple Node.js application reads and writes analog values to fade an LED from digital pins (PWM) on your Intel® IoT Platform.

Source: https://github.com/gomobile/iotapp-template-pwm

var mraa = require("mraa"); //require mraa
//Initialize PWM on Digital Pin #3 (D3) and enable the pwm pin
var pwm3 = new mraa.Pwm(3, -1, false);
pwm3.enable(true);

I2C - LCD with backlight driver and demonstrator

This simple Node.js application demonstrates the use of an I2C device: connecting to and driving the I2C bus directly from JavaScript*. Specifically, the example drives a JHD1313m1 LCD, as found in the Grove* - Starter Kit. Though the code you need to use for your specific device may vary depending on the device, just as it needs to depend on the bus, this example contains some useful code to modify for your own purposes.

Note that you need delays between some of the bus transactions. If you were writing the code in C, you could use the wait system call, but there is no direct equivalent to "wait" in JavaScript. Instead, we set up a callback on a timer to create the delay. i2c.js thus creates a queue-driven execution engine for I2C devices.

Source: https://github.com/gomobile/iotapp-template-i2c

ISR - Interrupt Service Routine

This simple Node.js application calls an Interrupt Service Routine (ISR) when an interrupt occurs.

Source: https://github.com/gomobile/iotapp-template-isr

var mraa = require('mraa'); 
function a() { 
var d = new Date(); 
var time = d.getTime(); 
console.log("The time is " + time); 
} 
var x = new mraa.Gpio(14); //GPIO Pin 14 
//Note: Intel Galileo Gen1, Gen 2 and Intel Edison only supports EDGE_BOTH with mraa javascript implementation. 
x.isr(mraa.EDGE_BOTH, a());

SPI - Serial Peripheral Interface

This simple Node.js application demonstrates how to communicate with SPI devices using an Intel® IoT Platform, such as the Intel® Edison board, as the master device. Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. With an SPI connection, there is always one master device (usually a microcontroller) which controls the peripheral devices. There are a few lines common to all devices:

  • Master In Slave Out (MISO): The slave line for sending data to the master.
  • Master Out Slave In (MOSI): The master line for sending data to the peripherals.
  • Serial Clock (SCK): The clock used to synchronize data transmission generated by the master.
  • Slave Select (SS): The pin on each device that the master can use to enable and disable specific devices. When a device's slave select pin is low, it communicates with the master. When it's high, it ignores the master. This allows you to have multiple SPI devices sharing the same MISO, MOSI, and CLK lines.

This sends 4 bytes on MOSI. If connected correctly, the example yields a 4 buffer object with the result on MISO and prints both the sent bytes and the result. Connect MOSI to MISO and the input should match the result. Acceptable parameters to the SPI constructor depend on the amount of SPI buses and chip selects you have. MRAA considers every chip select on every bus to be represented by a SPI object, but 0 is always the default. The SPI pins are 10/11/12/13, as follows:

  • Pin 10 >> SS
  • Pin 11 >> MOSI
  • Pin 12 >> MISO
  • Pin 13 >> SCK

Source: https://github.com/gomobile/iotapp-template-spi

var m = require('mraa'); //require mraa
// helper function to go from hex val to decfunction char(x) { return parseInt(x, 16); }
var x = new m.Spi(0);
var buf = new Buffer(4)
buf[0] = char('0xf4')
buf[1] = char('0x2e')
buf[2] = char('0x3e')
buf[3] = char('0x4e')
var buf2 = x.write(buf);
coconsole.log("Sent: " + buf.toString('hex') + ". Received: " + buf2.toString('hex'));

UART Serial Read/Write

This simple Node.js application demonstrates reading and writing data via the serial port(s) on Intel-based development boards, such as the Intel® Galileo board and the Intel® Edison board board with Arduino* expansion board. This project uses the SerialPort Node.js module to enable writing and reading via the serial ports provided.

Source: https://github.com/gomobile/iotapp-template-uart

var mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //print out the mraa version in IoT XDK console//Intel® Edison & Intel® Galileo
u = new mraa.Uart(0); //Default
//Name: UART1, the general-purpose TTL-level port (Arduino shield compatibility)
//Location: Pins 0 (RX) and 1 (TX) on the Arduino shield interface headers
var serialPath = u.getDevicePath(); //Default general purpose port "/dev/ttyMFD1" - Intel® Edison; "/dev/ttyS0" - Intel® Galileo
//Name: “Multi-gadget” or “Firmware Programming” or "Arduino Serial console" or "OTG" port
//Location: USB-micro connector near center of Arduino board. - Intel® Edison
//var serialPath = "/dev/ttyGS0";
//Name: UART2
//Location: USB-micro connector near edge of Arduino board. - Intel® Edison
//var serialPath = "/dev/ttyMFD2";
//Serialport NodeJS module declared in package.json
var SerialPort = require("serialport").SerialPort;
var serialPort = new SerialPort(serialPath, {
baudrate: 115200
});
serialPort.on("open",function() {
console.log("open");
console.log("Connected to "+serialPath);
serialPort.on("data", function(data) { //Read available data from serial port
console.log("data received: " + data);
});
serialPort.write("This is a test.\n", function(err, results) { //Write data
console.log("err " + err);
console.log("results " + results);
});
});

Local Temperature

This application lets you take temperature readings with the Grove* - Temperature Sensor and graph the data on a mobile device. For steps to set up the project, see Creating a temperature monitoring app using the Intel® XDK.

Source: https://github.com/gomobile/iotapp-local-temperature

Touch Notifier

This application lets you monitor the status of a touch sensor in a remote location with a companion app on a mobile device. When the sensor is touched, the companion app displays a notification on the mobile device and a buzzer connected to the board sounds. For steps to set up the project, see Creating a touch monitoring app using Intel® XDK.

Source: https://github.com/gomobile/iotapp-touch-notifier

Servrotary

This application rotates a servo motor connected to your IoT board when you turn the knob on a rotary angle sensor. For steps to set up the project, see Creating an app that rotates a servo motor.

Source: https://github.com/gomobile/iotapp-servrotary

(BLE) BLE-Peripheral

You can create a Bluetooth* Smart/Low Energy (BLE) peripheral application that runs on an Intel® Galileo board or Intel® Edison board. Users of this application can advertise the presence of the board as a BLE peripheral. They can also read and write data via a service with corresponding characteristic. For steps to set up the project, see Creating a Bluetooth* Low Energy app.

Source: https://github.com/gomobile/iotapp-template-ble-peripheral

(BLE) BLE-iBeacon

You can create a Bluetooth* Smart/Low Energy (BLE) iBeacon* application that runs on an Intel® Galileo board or Intel® Edison board, advertising the board's presence as a BLE device. Once the application is running, you can use a mobile app to scan for and measure the distance between the mobile device and your board. For steps to create both applications, see Creating a Bluetooth* Smart/Low Energy iBeacon application.

Source: https://github.com/gomobile/iotapp-template-ble-ibeacon

Simple Web Server

This sample creates a very simple web server on your IoT device using the libmraa and libupm libraries for Node.js. For a summary of how to run the app, see Creating a web server.

Source: https://github.com/gomobile/iotapp-template-simple-web-server

LCD with backlight driver and demonstrator

This application drives a JHD1313M1 LCD display connected via the I2C interface, as found, for example, in the Grove* - Starter Kit Plus. For a description and important considerations when using the app, see Creating an LCD display app.

Source: https://github.com/gomobile/iotapp-template-lcd-driver

Web Sockets

This simple Node.js project uses the socket.io Node.js module to enable real-time communication between a client and your board via a web browser. You can use a client-side web application to turn an LED on your board on and off. For steps to set up the project, see Creating an application that communicates with your board using Web Sockets.

Source: https://github.com/gomobile/iotapp-template-websockets

IoT DevKit v1.0

This application demonstrates the use of multiple components in a single project: a light sensor, LED, and LCD.

For steps to set up this project, see Creating an app that uses multiple components: Light sensor, LED, and LCD.

Source: https://github.com/gomobile/iotapp-template-devkit-v1

For more complete information about compiler optimizations, see our Optimization Notice.