Demo - Controlling Arduino from iOS - Arduino Development for OSX and iOS (2015)

Arduino Development for OSX and iOS (2015)

4. Controlling Arduino from iOS

4.3 Demo

In this demo, we use three LEDs as actuator devices on Arduino. We will control these LEDs from iOS program. For hardware implementation, we use the same wiring from section 3.4.


4.3.1 Firmata Program on Arduino Board

You can install Firmata on Arduino board. Please read section 3.2 to deploy Firmata on Arduino.

4.3.2 Writing Arduino Server

To implement Arduino server, we use Node.js, . You can install it via brew.

$ brew install node

To access Firmata from Node.js, we can use Firmata library for Node.js from this, .

Now we can build an Arduino Server. Firstly, create a folder, called ArduinoServer.

$ mkdir ArduinoServer

We create package.json file to load required libraries. Write these scripts.


"name": "arduinoserver",

"version": "0.0.1",


"firmata": "latest",

"async": "latest",

"express": "4.x",

"body-parser": "latest"



Then, type this command to install.

$ npm install


To test the library, we build blinking program using LED on digital 13. Write these scripts.

console.log("blink start ...");

var ledPin = 13;

var firmata = require('firmata');

var board = new firmata.Board("/dev/cu.usbmodem1411", function(err) {

if (err) {





console.log("Firmware: " + + "-" + board.firmware.version.major + "." + board.firmware.version.minor);

var ledOn = true;

board.pinMode(ledPin, board.MODES.OUTPUT);

setInterval(function() {

if (ledOn) {


board.digitalWrite(ledPin, board.HIGH);

} else {


board.digitalWrite(ledPin, board.LOW);


ledOn = !ledOn;

}, 500);


Save these scripts into a file, called blinking.js. Change Arduino serial /dev/cu.usbmodem1411 if you have different port address.

Now you can run the program.

$ sudo node blinking.js

You should see blinking LED on your Arduino board.


If success, we continue to develop our Arduino server.

Create a file, server.js, and write these scripts.

var firmata = require('firmata');

var express = require('express');

var app = express();

var bodyParser = require('body-parser');

var port = 8099;

var led1 = 10;

var led2 = 9;

var led3 = 8;

app.use(bodyParser.urlencoded({ extended: false }));


var board = new firmata.Board("/dev/cu.usbmodem1411", function(err) {

if (err) {




console.log("connected to Arduino board.");

board.pinMode(led1, board.MODES.OUTPUT);

board.pinMode(led2, board.MODES.OUTPUT);

board.pinMode(led3, board.MODES.OUTPUT);


console.log('Server was started on ' + port);


function execute_led(led,val){


board.digitalWrite(led, board.HIGH);


board.digitalWrite(led, board.LOW);


app.get('/led1/:id', function (req, res) {

//console.log("request /led1/",led1,


var data ={status:"ok",led:1,};



app.get('/led2/:id', function (req, res) {

//console.log("request /led3/",led1,


var data ={status:"ok",led:2,};



app.get('/led3/:id', function (req, res) {

//console.log("request /led3/",led1,


var data ={status:"ok",led:3,};



This program uses ExpressJS, , to implement web server.

Now you can run your program.

$ sudo node server.js

This program runs on port 8099 and serves on the following requests:

· http://<ip_computer>:8099/led1/1 to run on LED 1

· http://<ip_computer>:8099/led1/0 to run off LED 1

· http://<ip_computer>:8099/led2/1 to run on LED 2

· http://<ip_computer>:8099/led2/0 to run off LED 2

· http://<ip_computer>:8099/led3/1 to run on LED 3

· http://<ip_computer>:8099/led3/0 to run off LED 3

Now open a browse on you iOS devices and navigate to turn on/off LEDS using commands above.

Sample outputs can be seen in the Figure below.