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.

ch3-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, http://www.nodejs.org . 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, https://github.com/jgautier/firmata .

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",

"dependencies":{

"firmata": "latest",

"async": "latest",

"express": "4.x",

"body-parser": "latest"

}

}

Then, type this command to install.

$ npm install

ch4-6

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(err);

return;

}

console.log("connected");

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

var ledOn = true;

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

setInterval(function() {

if (ledOn) {

console.log("+");

board.digitalWrite(ledPin, board.HIGH);

} else {

console.log("-");

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.

ch4-5

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 }));

app.use(bodyParser.json());

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

if (err) {

console.log(err);

return;

}

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

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

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

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

app.listen(port);

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

});

function execute_led(led,val){

if(val==1)

board.digitalWrite(led, board.HIGH);

if(val==0)

board.digitalWrite(led, board.LOW);

}

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

//console.log("request /led1/",led1,req.params.id)

execute_led(led1,req.params.id);

var data ={status:"ok",led:1,val:req.params.id};

res.json(data);

});

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

//console.log("request /led3/",led1,req.params.id)

execute_led(led2,req.params.id);

var data ={status:"ok",led:2,val:req.params.id};

res.json(data);

});

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

//console.log("request /led3/",led1,req.params.id)

execute_led(led3,req.params.id);

var data ={status:"ok",led:3,val:req.params.id};

res.json(data);

});

This program uses ExpressJS, http://expressjs.com , 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.

ch4-7ch4-8