Physical

The physical and virtual worlds are colliding like never before. Sometimes the outcome is marvelous with amazing new interfaces, sometimes bug ridden and difficult to use.

One platform that doesn't need an introduction around here, the Arduino, has been blazing the path on several fronts. The latest of which is the Arduino Yún

Arduino Yún

The Yún is actually a hybrid device, 1 part Arduino and 1 part linux/wifi router. It has the normal Arduino interface and in addition it has a separate chip set that runs linux (Linino/openwrt) and enables connectivity via WiFi or Ethernet.

Guide to the Arduino Yun

The easiest way to work with a Yun from our perspective is to use the REST API and to communication with the Arduino portion via the Bridge API. This tutorial from Arduino gives us a nice starting point. The key to it is that we are creating a "YunServer" and responding to specific REST style queries

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

// Listen on default port 5555, the webserver on the Yun
// will forward there all the HTTP requests for us.
YunServer server;

void setup() {

  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.listenOnLocalhost();
  server.begin();
}

void loop() {
  // Get clients coming from server
  YunClient client = server.accept();

  // There is a new client?
  if (client) {
    // Process request
    process(client);

    // Close connection and free resources.
    client.stop();
  }

  delay(50); // Poll every 50ms
}

void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');

  // is "digital" command?
  if (command == "digital") {
    digitalCommand(client);
  }

  // is "analog" command?
  if (command == "analog") {
    analogCommand(client);
  }

  // is "mode" command?
  if (command == "mode") {
    modeCommand(client);
  }
}		
		(example code truncated)
		
To make requests to this interface, we simply need to construct HTTP requests. We can use the same WebServices infrastructure that we developed last week in Node to do so.
var http = require('http');

var makeRequest = function() {
        var httprequestoptions = {
                host: '128.122.98.22',
                path: '/arduino/analog/0'
        };

        var callback  = function(response) {
                // This string will contain everything back from the server but it will come in chunks
                var str = '';

                // Got a chunk
                response.on('data', function (chunk) {
                        str += chunk;
                });

                response.on('end', function () {
                		// We are done
                		console.log(str);
                		// Request it again after a timeout
					var request = setTimeout(makeRequest,1000);
                });
        }

		// This is the actual request for the page
        http.request(httprequestoptions, callback).end();       
}; 

makeRequest();		
		
Of course, to do something with this data, we should combine it with a socket server so that we can share it with clients:
        	// Broadcast it
            io.sockets.emit('sensor', str);
		
Here is a full example