Developing Internet of Things Projects with the Intel® XDK

This guide contains background information and steps to create applications for your Internet of Things (IoT) board using the Intel® XDK. The Intel XDK, part of the Intel® IoT Developer Kit, lets you create and run Node.js* applications directly on Intel®-based IoT platforms. This edition of Intel XDK also provides templates for creating new applications and offers the possibility of remotely debugging your application and running it from your own desktop.

Developers do not need to use the Intel XDK, but it’s a great tool to have. For example, since the Intel® Galileo board is intended to run IoT projects, its processing power is somewhat limited. Using the Intel XDK allows bigger or more complex projects to take advantage of a more powerful processor to run a complete developer environment.

For an introduction and steps to get started creating applications with the Intel XDK, see the following:

  • Getting started with the Intel XDK: with steps to connect your IoT board to the application, create a project from a sample template, and run it. This section also covers modifying and debugging code
  • Creating projects: Templates and example projects: This section contains an overview of the project templates and sample code available for the Intel XDK
  • Adding sensors and actuators: See this section for an introduction to adding sensors and actuators to your project and finding example code for compatible sensors
  • Working with your device in the Intel XDK: See this section for a general overview of common tasks, like connecting to your board, updating software libraries, and setting project configuration options
  • Intel XDK Troubleshooting and FAQ: This section contains troubleshooting information for common tasks, such as connecting your board to the Intel XDK and addressing common error messages

For an overview of the Projects tab, including the types of projects available, see Projects Overview.

For general steps to create, import, and manage projects, see Create, Import, and Manage Intel XDK Projects.

For release notes, see Release Notes - Intel XDK.

Requirements

For the Intel® Edison Board

  • A system with Windows*, OS X*, or Linux*
  • You have assembled and connected your Intel® Edison board. See the steps for 64-bit Windows, 32-bit Windows, OS X, or Linux

For the Intel® Galileo Board

  • A system with Windows, OS X, or Linux
  • You have assembled and connected your Intel® Galileo board. See the steps for Windows, OS X, or Linux. Make sure that you follow the steps to create a bootable SD card with the latest Yocto Project*-built Linux image from the Intel IoT Developer Kit. This step is necessary to use the libraries needed to work with the Intel XDK

For Supported Gateways

See Getting Started with Intel® IoT Gateway with Intel IoT Developer Kit 3.5 for steps to set up your gateway device.

For the Intel® Joule™ Development Platform

For detailed steps to set up your Intel® Joule™ development platform to connect to the Intel XDK, see the Intel Joule Module User Guide.

Creating JavaScript*-Based Intel® IoT Projects with the Grove* - Starter Kit

This section contains an introduction to creating JavaScript*-based Intel IoT projects with the Grove* - Starter Kit Plus - Intel IoT Edition. To skip to the example code for sensors and actuators, see the appropriate link below:

  • Digital output
  • Digital input
  • Analog input
  • Analog output with pulse-width modulation (PWM)
  • I2C*

The Grove system is an easy-to-use group of modules that minimize the effort required to get started with microcontroller-based experimentation and learning. Intel has partnered with Seeed Studio*, the creators of Grove*, to distribute the Grove - Starter Kit Plus: Intel IoT Edition, which contains essential sensor and actuator modules to jumpstart your IoT prototyping. Once you install the base shield that comes with the kit, attaching sensors and actuators to your board is a matter of plugging them in to the clearly labeled sockets.

The sensors and actuators in the Grove - Starter Kit Plus - Intel IoT Edition work with both the Intel Galileo board and the Intel Edison board with Arduino* expansion board.

grove starter kit plus intel i o t edition

The table below shows the contents of the Grove - Starter Kit Plus box.

  • Green components: Can be found in the plastic tray inside the green box
  • Gray components: Can be found underneath the plastic tray or elsewhere in the cardboard box

26 a w g grove cables
26 AWG Grove* - Cables (× 10)

barrel jack adapter
9 V to barrel jack adapter - 126 mm

red l e d
Red, green, and blue 3mm LEDs

manual
Starter kit manual

micro s d and s d adapter
8GB micro SD card with an SD Card adapter

Installing the Grove - Base Shield

The Grove - Base Shield is an Arduino shield, an add-on that plugs in to standard Arduino header pin configurations, which includes the Intel Galileo board and Intel Edison board with Arduino expansion board. In general, shields allow for additional functionality without having to use an additional prototyping area such as a breadboard. The base shield lets you attach sensors and actuators to your Intel Galileo board or Intel Edison board by plugging them directly in to the base shield.

  1. Unplug the Arduino expansion board from all power sources, such as a breadboard. Line up the male header pins of the base shield with the female pins on the Arduino expansion board. The pin configuration only fits the Arduino expansion board in one direction, so do not force the pins if they do not line up
  2. Push down firmly and evenly on both sides of the base shield until the shield is securely installed

grove base shield installed

  1. Check the voltage toggle switch (marked VCC, next to A0) on the base shield. It should be set to 5V
  2. Power your Intel Edison board or Intel Galileo board back on. Allow 1 minute for the board to finish booting up

Connecting a Grove Component

You connect all components in the Grove system using a 4-pin cable with JST connectors. This allows you to connect components and prototype without having to individually learn how to wire up each unique component.

  1. Choose a component from the Grove system, such as a light sensor or button
  2. Use a 4-pin Grove Cable and connect one end of the cable to the component. The cable connectors only fit in one direction, so you can be confident that the wiring is correct
  3. Refer to the base shield labels for the correct pin to connect the other end of the cable to. This varies depending on the component and what pin your code expects the component to be attached to. For example, if you are using the light sensor as an analog input and your code expects the sensor to be connected to analog pin 1, you connect the cable to the socket labeled A1

grove system

The following pins are available:

  • Digital pins (D2-D8)
  • Analog pins (A0-A3)
  • I2C (bottom row)
  • UART (top right)

grove system with 4 pin cables attached

Grove Component Types

The simpler components in the starter kit fall into two categories: input or output. These two categories further subdivide into digital or analog. Other components in the starter kit may use more complex communications protocols, such as UART or I2C.

You must know in which category a component is in order to correctly attach the component to the base shield and use the correct code library and commands to control the component.

The following sections contain examples using components available in the Grove - Starter Kit Plus, divided by component type. If available, code examples are provided using both the lower-level MRAA I/O library and the UPM sensor library:

The following code examples are available:

  • Digital output
  • Digital input
  • Analog input
  • Analog output with pulse-width modulation (PWM)
  • I2C

A digital output can write a value of either on (1) or off (0). You connect this type of component to any pin labelled D (for "digital") on the base shield, such as D2 to D8.

The table below contains examples of digital outputs and the require statement needed to add the component to your project.

l e d socket kit
Grove - LED Socket Kit
require('jsupm_grove')

smart relay
Grove - Smart Relay
require('jsupm_grove')

Low-level MRAA-only example:

var mraa = require('mraa');
var led = new mraa.Gpio(6); // set up digital read on digital pin #6
led.dir(mraa.DIR_OUT);  // set the GPIO direction to output
led.write(1);  // set the digital pin to high (1)

Higher-level UPM example:

var groveSensor = require('jsupm_grove');
var led = new groveSensor.GroveLed(6); // set up digital output on digital pin #6
led.on();  // set the digital pin to high

If you want to make the LED turn on and off, add a setTimeout() or setInterval() to toggle between writing 1 or 0 to the digital pin.

Digital Input

A digital input can read a value as either on (1) or off (0).You connect this type of component to any pin labelled D (for "digital") on the base shield, such as D2 to D8.

The table below contains examples of digital inputs, a link to sample code for the components, and the require statement needed to add the component to your project.

grove button
Grove - Button
require('jsupm_grove')

grove touch sensor
Grove - Touch Sensor (TTP223)
require('jsupm_ttp223')

Low-level MRAA-only example:

var mraa = require('mraa');
var button = new mraa.Gpio(5);     // set up digital read on digital pin #5
button.dir(mraa.DIR_IN);           // set the GPIO direction to input
var buttonState = button.read();   // read the value of the digital pin
console.log(buttonState);          // write the value to the console for debugging

Higher-level UPM example:

var groveSensor = require('jsupm_grove');
var button = new groveSensor.GroveButton(5); // set up digital input on pin #5
var buttonState = button.value();  // read the value of the digital pin
console.log(buttonState);          // write the value to the console for debugging

To react to button press beyond application startup, add a setTimeout() or setInterval() to periodically poll the pin state.

Analog Input

An analog input will read a value between 0 and 1024. You connect this type of component to any pin labelled A (for "analog") on the base shield, such as A0 to A3.

The table below contains examples of digital outputs and the require statement needed to add the component to your project.

light sensor
Grove - Light Sensor
require('jsupm_grove')

rotary angle
Grove - Rotary Angle
require('jsupm_grove')

temperature sensor
Grove - Temperature Sensor
require('jsupm_grove')

Low-level MRAA-only example:

var mraa = require('mraa');
var light = new mraa.Aio(0);     // set up analog input on analog pin #0 (A0)
var lightValue = light.read();   // read the value of the analog pin
console.log(lightValue);         // write the value to the console for debugging

Higher-level UPM example:

var groveSensor = require('jsupm_grove');
var light = new groveSensor.GroveLight(0); // set up analog input on pin #0 (A0)
varlightValueRaw = light.raw_value();     // read the raw value of the analog pin
var lightValue = light.value();            // read the converted value of the pin
console.log(lightValueRaw, lightValue);    // write values to console for debugging

To react to changes in light beyond application startup, add a setTimeout() or setInterval() to periodically poll the sensor value.

Analog Output

An analog output is a digital output in disguise. Intel IoT boards are digital microcontrollers that can pretend to be analog using a concept called pulse width modulation (PWM). Analog outputs will accept a floating-point value representing a duty cycle percentage between 0 (always off) and 1.0 (always on). For example, a value of 0.5 will rapidly pulse equally between on and off.

Components will only work when connected to PWM-enabled pins. By factory default, these PWM pins are D3, D5, and D6. (Digital pin #9 is also available on the Arduino expansion board via the standard female header pin.)

The table below contains examples of digital outputs and the require statement needed to add the component to your project.

l e d socket kit
Grove - LED Socket Kit
require("mraa")

grove buzzer
Grove - Buzzer
require("jsupm_buzzer")

servo motor
Grove - Servo Motor
require("jsupm_servo")

Low-level MRAA-only example:

var mraa = require("mraa");
var led = new mraa.Pwm(3);  // initialize PWM on digital pin #3
led.write(0.5);             // make LED half brightness
///--- below example: fade over time ---
var brightness = 1;
var fadeInterval;
function fade(){
  led.write(brightness);
  brightness -= 0.01;
  if (brightness < 0) clearInterval(fadeInterval);
}
fadeInterval = setInterval(fade, 100);

Higher-level UPM example:

Please see intel-iot-devkit / upm

I2C*

I2C (Inter-Integrated Circuit), pronounced I-two-C or I-squared-C, is a multi-master, multi-slave, single-ended, serial computer bus used for attaching lower-speed peripherals to processors on computer motherboards and embedded systems.

lcd rgb backlight
Grove - LCD RGB Backlight
require ('jsupm_i2clcd')

Higher-level UPM example:

var jsUpmI2cLcd = require ('jsupm_i2clcd');
var lcd = new jsUpmI2cLcd.Jhd1313m1(6, 0x3E, 0x62); // Initialize the LCD
lcd.setCursor(0,1); // go to the 1st row, 2nd column (0-indexed)
lcd.write("hello"); // print characters to the LCD screen

Creating a Temperature Monitoring App Using Intel® XDK

This guide contains steps to take temperature readings with the Grove - Temperature Sensor and graph the data on a mobile device. The source code used to read sensor data can be found here: iotapp-local-temperature. This example uses components from the Grove - Starter Kit with the Intel Edison board; if you use different components, your code and setup may vary slightly.

Requirements 

  • You have installed the Intel XDK and connected it to your IoT board. For steps, see Getting started with the Intel XDK
  • Your board must be connected to the Internet
  • To view temperature data with your mobile device, your Android, Apple* iOS*, or Windows mobile device must be connected to the same network as your board
  • You must have a Grove - Base Shield, Grove - Temperature Sensor, and a Grove Cable

components for temperature sensor

Assembling Your Board and Sensor Components

This section contains steps to connect your board to the Grove - Temperature Sensor

  1. Line up the male header pins on the bottom of your base shield with the female pins on the expansion board, as shown in the image below. Once aligned, gently push the base shield down firmly and evenly towards your board until it is securely attached

grove base shield installed

  1. Check that the voltage toggle switch (which is marked VCC and located next to the A0 socket) on the base shield is set to 5V
  2. Slide the connector on one end of the Grove cable into the socket on your temperature sensor until you feel the cable snap into place

attaching the temperature sensor to a cable

  1. Slide the other end of the cable into the A0 socket on your Grove base shield until you feel the cable snap into place

attaching cable to the base shield

Creating a New Project for Reading Temperature Sensor Data

This section contains steps to use sample code to read temperature data from the sensor attached to your board.

  1. Run the Intel XDK
  2. In the top left, click the Projects tab
  3. Click Start a New Project

starting a new project

  1. Under Internet of Things Embedded Application, click Templates

creating a project from a template

  1. From the right, click Local Temperature. Click Continue
  2. Type a name for the project in the Project Name field, then click Create. Your new project opens

naming your project

Note: the IP address of your board, which is displayed in the IoT Device drop-down list at the bottom of the Intel XDK screen.

Uploading the Temperature Sensor Code to Your Board

This section contains steps to build, upload, and run the code to read data from the temperature sensor.

  1. In the bottom toolbar, click the Upload icon to upload the project to your board
  2. Click the Run icon to run your project

Note: If the bottom console window displays a message about being unable to find MRAA, you must update the MRAA library on your board. Do the following:

  1. Open a serial communication session with your board
  2. Type the following commands:
echo "src mraa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/mraa-upm.conf
opkg update
opkg upgrade

Creating the Local Temperature Companion App

This section contains steps to create the Local Temperature Companion app, which allows you to use your mobile device to monitor the temperature read by your temperature sensor.

  1. Click the Projects tab
  2. At the bottom of the left menu, click Start a New Project
  3. Under HTML5 Companion Hybrid Mobile or Web App, expand Samples and Demos. Click General

click general

  1. From the right under General, select HTML5 and Cordova. Select Local Temperature Companion App. Click Continue

creating the local temperature companion app

  1. Type a name for the project in the Project Name field, then click Create. Your new project opens

Testing the Local Temperature Companion App in the Intel XDK

This section contains steps to test your temperature companion app using the Intel XDK's built-in emulator. For steps to test your app directly on a mobile device, see the Testing the Local Temperature Companion app on your mobile device section.

  1. Click the Emulate tab
  2. From the Devices drop-down list, select the mobile device that you want to test on. Your app opens

select a mobile device on emulator tab

  1. In the Enter your IoT dev platform's IP Address field, type the IP address of your board
  2. In the Enter your corresponding port number field, type 1337
  3. Click Submit. The mobile device displays temperature readings from your sensor in a graph

temperature readings

Testing the Local Temperature Companion App on Your Mobile Device

This section contains steps to test the companion app on your mobile device. After completing the steps, you will be able to monitor temperature readings using your mobile device.

  1. Download and install the Intel® App Preview app on your mobile device using the appropriate link below:

Microsoft* Store

  1. In the Intel XDK, click the Test tab
  2. Click I Have Installed App Preview

intel app preview message

  1. If a message is displayed asking you to sync with the testing server, click Sync

syncing with test server

  1. On your mobile device, run Intel App Preview

welcome screen

  1. On the Welcome screen, choose Login
  2. Enter the user name and password for your Intel XDK account, then choose Submit to log in
  3. On the bottom menu bar, choose Server Apps. Select your app, then click the launch button to run the app

launching the app

  1. In the Enter your IoT dev platform's IP Address field, type the IP address of your board
  2. In the Enter your corresponding port number field, type 1337. Choose Submit. The mobile device displays a graph of temperature readings from your sensor

entering i p and port information

Creating a Touch Monitoring App Using Intel XDK

This guide contains steps to create a touch monitoring application using an Intel Galileo board or Intel Edison board. Users of this app can monitor the status of a touch sensor in a remote location with a companion app on their mobile devices. When the sensor is touched, the companion app displays a notification on the mobile device and a buzzer connected to the board sounds. The source code used to read sensor data can be found here: iotapp-touch-notifier.

The Touch Notifier sample application code is distributed with the Intel XDK. It showcases how to read digital data from a Grove - Touch Sensor, start a web server, and periodically send updates to connected mobile clients using WebSockets. A buzzer alerts the user when the sensor has been touched. This sample application communicates with sensors using the MRAA sensor communication library, which is designed 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. For more information on MRAA, see mraa.

The Touch Notifier Companion app is an HTML5 + Cordova app that communicates with the Touch Notifier application using the WebSockets API. It allows a user to monitor the touch sensor using a mobile device and displays notifications when the sensor is touched. This example uses components from the Grove - Starter Kit; if you use different components, your code and setup may vary slightly.

Requirements

  • You have installed the Intel XDK and connected it to your board. For steps, see Getting started with the Intel XDK
  • Your board must be connected to the Internet
  • Your Android, Apple iOS, or Microsoft Windows mobile device must be connected to the same network as your board
  • You must have a Grove - Base Shield, Grove - Touch Sensor, Grove - Buzzer, and two Grove - Cables

Assembling Your Board and Sensor Components

This section contains steps to connect your board to the touch sensor and buzzer.

  1. Line up the male header pins on the bottom of your base shield with the female pins on the expansion board, as shown in the image below. Once aligned, gently push the base shield down firmly and evenly towards your board until it is securely attached

grove base shield installed

  1. Check that the voltage toggle switch (which is marked VCC and located next to the A0 socket) on the base shield is set to 5V
  2. Slide the connector on one end of the Grove cable into the socket on your touch sensor until you feel the cable snap into place

attaching the touch sensor

  1. Slide the other end of the cable into the D2 socket on your base shield until you feel the cable snap into place

attaching the cable to the base shield

  1. Slide the connector on one end of the Grove cable into the socket on your buzzer until you feel the cable snap into place

attaching the buzzer to a cable

  1. Slide the other end of the cable into the D6 socket on your base shield until you feel the cable snap into place

attaching second cable to the base shield

Creating a New Project for Reading Touch Sensor Data

This section contains steps to use sample code to read data from the touch sensor attached to your board. When someone touches the sensor, you will receive a notification on your mobile device.

  1. Run the Intel XDK
  2. In the top left, click the Projects tab
  3. Click Start a New Project

starting a new project

  1. Under Internet of Things Embedded Application, click Templates

creating a project from a template

  1. From the right, click Touch Notifier. Click Continue

select touch notifier.

  1. Type a name for the project in the Project Name field, then click Create. Your new project opens

naming your project

  1. Note the IP address of your board, which is displayed in the IoT Device drop-down list at the bottom of the Intel XDK screen

Uploading the Touch Sensor Code to Your Board

This section contains steps to build, upload, and run the code to monitor data from the touch sensor.

  1. In the bottom toolbar, click the Upload icon to upload the project to your board
  2. Click the Run icon to run your project

Note: If the bottom console window displays a message about being unable to find MRAA, you must update the MRAA library on your board. Do the following:

  1. Open a serial communication session with your board
  2. Type the following commands:
echo "src mraa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/mraa-upm.conf
opkg update
opkg upgrade

Creating the Touch Notifier Companion App

This section contains steps to create the Touch Notifier Companion app, which allows you to use your mobile device to monitor the touch sensor's readings.

  1. Click the Projects tab
  2. At the bottom of the left menu, click Start a New Project
  3. Under HTML5 Companion Hybrid Mobile or Web App, expand Samples and Demos. Click General

creating an h t m l 5

  1. From the right under General, select HTML5 and Cordova. Select Touch Notifier Companion App. Click Continue

creating the touch notifier companion app

  1. Type a name for the project in the Project Name field, then click Create. Your new project opens

Testing the Touch Notifier Companion App in the Intel XDK

This section contains steps to test your companion app using the Intel XDK's built-in emulator. For steps to test your app directly on a mobile device, see the Testing the Touch Notifier app on your mobile device section.

  1. Click the Emulate tab
  2. From the Devices drop-down list, select the mobile device that you want to test on. Your app opens

testing an app on the emulate tab

  1. In the Enter your IoT dev platform's IP Address field, type the IP address of your board
  2. In the Enter your corresponding port number field, type 1337. Click Submit
  3. Apply pressure to the touch sensor. The mobile device displays a notification and the buzzer sounds

Testing the Touch Notification Companion App on your Mobile Device

This section contains steps to test the companion app on your mobile device. After completing the steps, you will be able to monitor touch sensor readings using your mobile device.

  1. Download and install the Intel App Preview app on your mobile device using the appropriate link below:

Microsoft* Store

  1. In the Intel XDK, click the Test tab
  2. Click I Have Installed App Preview

intel app preview message

  1. If a message is displayed asking you to sync with the testing server, click Sync

syncing with test server

  1. On your mobile device, run Intel App Preview

welcome screen

  1. On the Welcome screen, choose Login
  2. Enter the user name and password for your Intel XDK account, then choose Submit to log in
  3. On the bottom menu bar, choose Server Apps. Select your app, then click the launch button to run the app

launching the app

  1. In the Enter your IoT dev platform's IP Address field, type the IP address of your board
  2. In the Enter your corresponding port number field, type 1337. Choose Submit. The mobile device displays a graph of temperature readings from your sensor

 notification when the sensor is touched

Creating an App That Uses Multiple Components: Light Sensor, LED, and LCD

This guide contains steps to create an application that uses multiple input and output components and runs on an Intel Galileo board or Intel Edison board. In the IoT DevKit v1 sample, the LCD acts as the central device for displaying messages. The light sensor senses the brightness of its surroundings in lux, the reading is converted to a meaningful string or message, and the LED turns on or off depending on whether the brightness reaches a threshold value.

The source code for the application that runs on the development board can be found here: iotapp-template-devkit-v1

Requirements

  • You have installed the Intel XDK and connected it to your IoT board. For steps, see Getting started with the Intel XDK
  • Your board must be connected to your system on the same network. Steps to connect your board are included in Getting started with the Intel XDK
  • You must have a Grove - Base Shield, Grove - Light Sensor, Grove - LCD, Grove - LED and three Grove - Cables

intel edison board

Assembling Your Board and Components

This section contains steps to connect your board to the light sensor, LCD and LED.

  1. Line up the pins on the bottom of your base shield to fit the contacts on your board, as shown in the image below. Once aligned, gently push the base shield down towards your board until it is securely attached

grove base shield installed

  1. Slide the connector on one end of the Grove cable into the socket on your light sensor until you feel the cable snap into place

attaching the light sensor

  1. To attach the light sensor to your board, slide the other end of the cable into the A0 socket on your base shield until you feel the cable snap into place
  2. Slide the connector on one end of the Grove cable into the socket on your LED until you feel the cable snap into place

attaching the led

  1. To attach the LED to your board, slide the other end of the cable into the D2 socket on your base shield until you feel the cable snap into place
  2. Slide the connector on one end of the Grove cable into the socket on your LCD until you feel the cable snap into place

attaching the l c d

  1. To attach the LCD to your board, slide the other end of the cable into any I2C socket on your base shield until you feel the cable snap into place

attaching each component to the base shield

Creating Your Project

This section contains steps to set up a sample project that reads data from a light sensor and turns an LED on/off in response to the brightness of its surroundings. When the light sensor detects different light intensities, you will receive a message on LCD device.

  1. Run the Intel XDK
  2. In the top left, click the Projects tab
  3. Click Start a New Project

starting a new project

  1. Under Internet of Things Embedded Application, click Templates

starting a new project

  1. From the right, click IoT Devkit v1. Click Continue

i o t dev kit

  1. Type a name for the project in the Project Name field, then click Create. Your new project opens

starting a new project

Uploading the IoT Devkit V1 Code to Your Board

This section contains steps to build, upload, and run the code to monitor data from the light sensor

  1. In the bottom toolbar of the Intel XDK, click the Upload icon to upload the project to your board
  2. Click the Run icon to run your project

Note: If the bottom console window displays a message about being unable to find MRAA, you must update the MRAA library on your board. Do the following:

echo "src mraa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/mraa-upm.conf
opkg update
opkg upgrade

References

GitHub* - upm

Grove Module

Creating an App That Rotates a Servo Motor

The example app in this guide rotates a servo motor connected to your Intel Edison board when you turn the knob on a rotary angle sensor. This is a simple Node.js application created using the Grove - Starter Kit: Intel IoT Edition. The sample code can be found in the following location: iotapp-servrotary

The Grove - Servo Motor is a fun motion control device that can be located at any position between 0 to 180 degrees. With 4 shafts in different shapes, this servo is ready to drive a small fan, lift an object, or mimic a clock hand.

The Grove - Rotary Angle Sensor is a 10k ohm linear rotary potentiometer. It has an active range of 300 degrees. It is also designed as a HID device and includes a “panel mount” feature.

The knob on the rotary angle sensor turns more dramatically than the servo due to its greater range of motion: 300 degrees, compared to the 180 degrees available to the servo motor. But presumably, you want to have the servo turn for the rotary angle sensor's full range of motion -- not just have the servo stop when the knob on the rotary angle sensor reaches 180 degrees.

For this reason, the app code needs to perform some one-on-one mapping. This ensures that when you turn the knob all the way to its maximum position, 300 degrees, the servo will also be at its maximum extended position.

After getting the absolute raw knob value in radians from the AIO pin attached to the rotary angle sensor, the app code sets the servo angle to the mapping ratio, as follows:

Mapping ratio = (Servorange/ Knobrange) * Knobvalue

In this case:

  • Servorange = 180 degrees
  • Knobrange = 300 degrees
  • Knobvalue is retrieved from the function xgroveRotary.abs_deg()

180 degree range mapped to 300

Requirements

  • >You have installed the Intel XDK and connected it to your Intel Edison board. For steps, see Getting started with the Intel XDK
  • Your board must be connected to the Internet. For steps, see Connecting to a network
  • You must have a Grove - Base Shield, Grove - Servo, Grove - Rotary Angle Sensor, and a Grove - Cable

Assembling Your Board and Grove Kit Components

This section contains steps to connect your board to the servo and rotary angle sensor.

  1. Line up the male header pins on the bottom of your base shield with the female pins on the expansion board, as shown in the image below. Once aligned, gently push the base shield down firmly and evenly towards your board until it is securely attached

grove base shield installed

  1. Check that the voltage toggle switch (which is marked VCC and located next to the A0 socket) on the base shield is set to 5V
  2. Slide the other end of the servo cable into the D5 socket on your base shield until you feel the cable snap into place

attaching servo to the base shield

  1. Slide the connector on one end of the Grove - cable into the socket on your rotary angle sensor until you feel the cable snap into place

attaching cable to the rotary angle sensor

  1. Slide the other end of the cable into the A0 socket on your base shield until you feel the cable snap into place

References

upm - Sensor framework for IoT developers

Creating a Bluetooth* Low Energy App

This guide contains steps to create a Bluetooth* Smart/Low Energy (BLE) peripheral application that runs on an Intel Galileo board or Intel Edison board. Users of this app can advertise the presence of the board as a BLE peripheral. They can also read and write data via a service with corresponding characteristic.

In this scenario, a mobile device acts as the central device; it scans, connects to, reads, and writes data to a BLE peripheral device (your board). The source code for the BLE peripheral application that runs on the board can be found here: iotapp-template-ble-peripheral. The source code for the BLE central mobile application that runs on the mobile device can be found here: sample-ble-central.

Bluetooth Low Energy communication between mobile devices and embedded development boards provides both entities with a delivery system in which you can transfer data from a central to a peripheral and vice versa.

Since all communication between the mobile device and your board is via Bluetooth Low Energy, they do not have to be on the same wireless network. The Intel® XDK and your board, however, must be on the same network.

Requirements

  • You have installed the Intel XDK and connected it to your IoT board. For steps, see Getting started with the Intel XDK
  • Your board must be connected to your system on the same network. Steps to connect your board are included in Getting started with the Intel XDK
  • Your board must have a Bluetooth module:
    • For the Intel Edison board: Your Bluetooth module is already built in to your board
    • For the Intel Galileo board: You will need to buy and install a compatible BLE product, such as the Grove - BLE
  • A mobile device that supports Bluetooth Low Energy

intel edison board

Creating a New Project with BLE Peripheral Functionality

This section contains steps to use sample code to advertise your board’s presence via BLE. Once you have the code up and running, you will also be able to read and write data with a BLE central device.

For design considerations, including in-depth technical information about this project, visit Design Considerations. This project was tested on an Intel Edison board with Arduino expansion board.

  1. Run the Intel XDK
  2. In the top left, click the Projects tab
  3. Click Start a New Project

starting a new project

  1. Under Internet of Things Embedded Application, click Templates

creating a project from a template

  1. From the right, click (BLE) BLE-Peripheral. Click Continue

creating a project with peripheral template

  1. Type a name for the project in the Project Name field, then click Create. Your new project opens

naming your projectb l e peripheral application

Enabling BLE on Your Board

This section contains steps to enable BLE on your board.

In the steps below, you can connect to your board using the SSH Terminal or Serial Terminal tabs within the Intel XDK. For detailed steps to create a serial terminal connection to your board outside of the Intel XDK, see the following:

  • For the Intel Edison board, see Setting up a serial terminal
  • For the Intel Galileo board, see the appropriate steps for Windows, OS X, and Linux
  1. Within an SSH or serial terminal connection, type the following commands:
rfkill unblock bluetooth
hciconfig hci0 up
  1. Enter the following command to open the base-feeds.conf file in the vi text editor:
vi /etc/opkg/base-feeds.conf 
  1. Insert the following lines into base-feeds.conf:
src/gz all http://repo.opkg.net/edison/repo/all
src/gz edison http://repo.opkg.net/edison/repo/edison
src/gz core2-32http://repo.opkg.net/edison/repo/core2-32

Save your changes and exit the editor. For more information on the vi editor, visit Basic vi commands.

  1. To get the bleno Node.js package to work successfully, enter the following commands:
rfkill unblock bluetooth
killall bluetoothd
hciconfig hci0 up 

You should now be able to use BLE in your project.

Uploading and Running the BLE-Peripheral Application on Your Board

This section contains steps to build, upload, and run the Node.js application to advertise, read, and write data via the Bluetooth Low Energy module on your board. At the end of this section, your board will act as a BLE peripheral device.

  1. In the bottom toolbar, click the Upload icon to upload the project to your board
  2. Click the Run icon to run your project

Note: If the bottom console window displays a message about being unable to find MRAA, you must update the MRAA library on your board. To do so, click the Manage your daemon/IoT device icon in the bottom right of the Intel XDK, then select Update libraries on board.

Creating the BLE-Central Mobile Companion App

  1. Click the Projects tab
  2. At the bottom of the left menu, click Start a New Project
  3. Under HTML5 Companion Hybrid Mobile or Web App, expand Samples and Demos. Click General

click general

  1. From the right under General, select HTML5 and Cordova. Select BLE-Centra. Click Continue

creating the companion app

  1. Type a name for the project in the Project Name field, then click Create. Your new project opens

b l e central mobile app

Deploying the BLE-Central App on Your Mobile Device

This section contains steps to deploy the BLE-Central companion app to your mobile device. Since this project uses the Bluetooth Low Energy Cordova plug-in, you must deploy it to your mobile device. It is not testable in the Intel XDK or the Intel® App Preview Legacy mobile app.

After completing the steps, you will be able to scan for BLE-peripheral devices, connect to a device by way of its specified service UUID, and read and write data using a characteristic UUID.

For design considerations, including in-depth technical information about this application, visit sample-ble-central. This project was tested on devices running Android 4.4+ and iOS 8.3+. Results may vary on Android devices versus iOS devices.

build tab

  1. On the Build tab, select your desired build target and click Build
  2. Follow the on-screen steps for your desired mobile platform to build your app

Using the App

This section contains steps to use the BLE central app on your mobile device to connect to and read and write data to your board.

  1. In the app, scan for your board by selecting Scan

b l e central app

A list of Bluetooth devices is displayed, as shown below.

bluetooth devices

  1. To connect to your board, type the Peripheral UUID and Characteristic UUID of your device. Click Done

connecting u u i d

  1. To read data, write data, or disconnect from your board, select the appropriate option

appropriate options

For example, select Read Data to view a "Hello, World!" message from your board.

message in board

Creating a Bluetooth Smart/Low Energy iBeacon* application

This guide contains steps to 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. The source code for the BLE iBeacon application that runs on your board can be found here: iotapp-template-ble-ibeacon.

This example demonstrates the usefulness of BLE iBeacon technology for identifying the location of a BLE device (in this case, your board) in reference to a mobile device's location. For design considerations, including in-depth technical information about this application, visit iotapp-template-ble-peripheral design considerations This project was tested on an Intel Edison board with the Arduino expansion board.

Since all communication between the mobile device and your board is via Bluetooth Low Energy, they do not have to be on the same wireless network. The Intel XDK and your board, however, must be on the same network.

Requirements

  • You have installed the Intel XDK and connected it to your IoT board. For steps, see Getting started with the Intel XDK
  • Your board must be connected to your system on the same network. Steps to connect your board are included in Getting started with the Intel XDK
  • Your board must have a Bluetooth module:
    • For the Intel Edison board: Your Bluetooth module is already built in to your board
    • For the Intel Galileo board: You will need to buy and install a compatible BLE product, such as the Grove - BLE

intel edison board

Creating a New Project with BLE iBeacon Functionality

This section contains steps to use sample code to advertise your board's presence via BLE.

  1. Run the Intel XDK
  2. In the top left, click the Projects tab
  3. Click Start a New Project

starting a new project

  1. Under Internet of Things Embedded Application, click Templates

creating a project from a template

  1. From the right, click (BLE) BLE-iBeacon. Click Continue

creating a new ibeacon app

  1. Type a name for the project in the Project Name field, then click Create. Your new project opens

naming your project

Enabling BLE on Your Board

This section contains steps to enable BLE on your board.

In the steps below, you can connect to your board using the SSH Terminal or Serial Terminal tabs within the Intel XDK. For detailed steps to create a serial terminal connection to your board outside of the Intel XDK, see the following:

  • For the Intel Edison board, see Setting up a serial terminal
  • For the Intel Galileo board, see the appropriate steps for Windows, OS X, and Linux
  1. Within an SSH or serial terminal connection, type the following commands:
rfkill unblock bluetooth
hciconfig hci0 up
  1. Enter the following command to open the base-feeds.conf file in the vi text editor:
vi /etc/opkg/base-feeds.conf 
  1. Insert the following lines into base-feeds.conf:
src/gz all http://repo.opkg.net/edison/repo/all
src/gz edison http://repo.opkg.net/edison/repo/edison
src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32

Save your changes and exit the editor. For more information on the vi editor, visit Basic vi commands.

  1. To get the bleno Node.js package to work successfully, enter the following commands:
rfkill unblock bluetooth
killall bluetoothd
hciconfig hci0 up 

Uploading the BLE-iBeacon Node.js* application to Your board

This section contains steps to build, upload, and run the relevant code to advertise, read and write data via the Bluetooth Low Energy module.

  1. In the bottom toolbar of the Intel XDK, click the Upload icon to upload the project to your board
  2. Click the Run icon to run your project

Note: If the bottom console window displays a message about being unable to find MRAA, you must update the MRAA library on your board. To do so, click the Manage your daemon/IoT device icon in the bottom right of the Intel XDK, then select Update libraries on board.

Testing Your application

The recommended way to test this application is to download and use a mobile application that allows you to view your nearby board (iBeacon), as well as other BLE iBeacon devices. Use Locate Beacon, which lets you find any nearby beacon with real-time distance estimates:

References

What is iBeacon?

About iBeacon on your iPhone, iPad, and iPod touch

bleno

Creating a Web Server

This sample creates a very simple web server on the IoT device using the libmraa and libupm libraries for Node.js. The code uses the light sensor from the Grove - Starter Kit Plus and serves JSON with the value read together with a simple-minded conversion to lux using linear interpolation.

The board will also serve a page that produces a simple display of the lux being received by the detector. The sample code can be found in the following location: iotapp-template-simple-web-server

Running the App

To run the app:

  1. Check the IP address at line 23 of main.js and set it to the IP address of your board if necessary. This is the IP address you used to connect to your board
  2. Start the program, then start a browser on your host. We used Chrome*, but the code should work with any browser
  3. Enter the URL: http:/<ip address>:1337/lightsensor. For example: http:/192.168.2.15:1337/lightsensor. A simple and somewhat ugly thermometer is displayed

Improving the App

The sample code has been deliberately kept simple. Some obvious improvements you might want to make include:

  1. Change the way the thermometer is displayed. HTML5 has other ways to display the thermometer. There are also graphing packages that will no doubt produce a prettier graphical display
  2. Improve the interpolation formula. The sample uses linear interpolation, but a higher-order interpolation may be worthwhile
  3. There are packages that support more sophisticated web server development. You could replace the rough code in the example with such a package
  4. The IP address could be changed to be automatically detected rather than being hard-coded

Creating an LCD Display App

This is an app to drive a JHD1313M1 LCD display connected via the I2C interface, as found, for example, in the Grove - Starter Kit Plus. The sample code can be found in the following location: iotapp-template-lcd-driver

The LCD Features

The JHD1313M1 is a two-line LCD that supports user-defined characters and requires only two pins for data exchange and backlight control.

Using the App with libupm

The app can be run in two ways. One is to use the upm library support for the device. This is the way you would normally use the device in a “real-world” application. In this mode, the application is quite trivial.

Using the App with Direct libmraa Calls

The second method is by driving the I2C interface directly. In effect, the file lcd.js is a device driver written in JavaScript.

While we recommend using libupm in most situations, the direct approach is interesting for several reasons:

  • To learn about the I2C programming in JavaScript using libmraa
  • To test the I2C interfaces in libmraa
  • As a starting point for driving non-supported devices

The lcd.js Implementation

The programming model for JavaScript is somewhat different from what you might see in C, because there is no equivalent of “sleep” in JavaScript. Instead, the JavaScript approach is to set a timer and call a function when it expires.

You could, of course, put your code into a very tight loop waiting for time to pass. For example:

function sleep(time) {
  var millSeconds  = time / 1000;
  var endTime = Date().getTime() + milliSeconds;
  while (Date().getTime() < endTime) {
     // We can’t leave, we’re waiting
  }
}

But this is a bad idea. As JavaScript is single-threaded, any events that might want to occur during this interval will be blocked.

At the same time, having to split up your code across functions that are attached to timers would be cumbersome. So instead, lcd.js implements a small engine that handles the timing for you.

Instead of performing actions immediately, we put them on a queue to be possibly executed at some time in the future. The actual execution is then event-driven.

This leaves the question of how you can tell when something has actually happened. Most of the time this won’t be important and you can just treat the calls as if they were synchronous. This is what we do in the app.

However, a good way to handle this is with the q module. You can find documentation for the module here: API Reference.

The lcd.js code contains the routine waitForQuiescent which will return any error or success when the queued operations are completed, while clearError will clear any errors so that you can continue using the LCD. See the code in main.js for an example of their use.

Creating an Application that Communicates with Your Board Using Web Sockets

This simple Node.js project uses the socket.io Node.js module to enable real-time communication between a client and an Intel Galileo board or Intel Edison board. You can use the client-side application provided with this project to view the number of users connected to the board, send text messages between connected users, and turn an LED on the board on ad off.

For sample code and design considerations, including in-depth technical information about this application, see iotapp-template-websockets.

Requirements

  • You have installed the Intel XDK and connected to your Intel Galileo board or Intel Edison board. For steps, see Getting started with the Intel XDK
  • Your board must be connected to your system on the same network as your computer. For steps, see the appropriate link below:

Creating a Project

  1. Run the Intel XDK
  2. In the top left, click the Projects tab
  3. Click Start a New Project

starting a new project

  1. Under Internet of Things Embedded Application, click Templates

creating a project from a template

  1. From the right, click Web Sockets. Click Continue
  2. Type a name for the project in the Project Name field, then click Create. Your new project opens

Building and Running Your Project

  1. To install the required Node.js modules for this project, click the Build/Install icon. If you are prompted to choose whether to upload or build your project, click Build

choosing to upload or build the project

  1. Once your project finishes building, a build complete message is displayed. Click the Upload icon to upload the project to your board
  2. Click the Run icon to run your project

Testing the Web Sockets Client Console

  1. In a web browser, type the IP address of your board, followed by a colon : and the port number to use to connect to your board, which in this example is 3000. For example, if the IP address of your board is 192.168.1.0, enter http://192.168.1.0:3000. The Web Sockets Client Console application opens, with a list of users currently connected to the board displayed under Connected Users

Note: If you don't know your board's IP address, open a serial or SSH connection with your board and enter the command: ifconfig. The IP address is displayed, as shown below.

board i p address

  1. You can turn an onboard LED on your board on and off by clicking Toggle LED in your browser. Your actions are recorded under the Log heading. To send a message to other users, type a message in the field and click Send

web sockets client

有关编译器优化的更完整信息,请参阅优化通知