Comm Lab Web Week 6

Other Platforms

Processing

Processing can make normal HTTP GET requests using the loadStrings method. It will return the HTML of a page in an array of strings, each line is determined by line breaks. Of course, you can pass in GET request variables by passing them in a query string, just like in a browser.

loadStrings

String[] response;
void setup() {
 	response = loadStrings("http://www.google.com/");
	println(response);    
}
		
More Information: http://processing.org/reference/loadStrings_.html

Processing also has a JSON parser and set of objects which can load JSON via a URL:

Parsing JSON

JSONObject jsonObj;

void setup() {
  jsonObj = loadJSONObject("http://api.openweathermap.org/data/2.5/weather?id=5128581&units=imperial");
  print(jsonObj);
}
  
void draw() {
    background(125 + jsonObj.getJSONObject("main").getInt("temp"), 0, 125 - jsonObj.getJSONObject("main").getInt("temp")); 
}
		
More Information: http://processing.org/reference/loadJSONObject_.html

Generate JSON with PHP

To work with Processing in this manner, using our own databases and PHP, we can generate JSON using PHP. Here is an example that pulls a single field of data out of a database using phpDataMapper and outputs it in an array of JSON objects:

<?
	require 'phpDataMapper/Base.php';

	ini_set('display_errors', true);
	ini_set('display_startup_errors', true);
	error_reporting(E_ALL);

	class DataSaver extends phpDataMapper_Base
	{
		protected $_datasource = "data";
	
		// Define your fields as public class properties
    	public $id = array('type' => 'int', 'primary' => true, 'serial' => true);
		public $data = array('type' => 'int', 'required' => true);
	}
	
	$databaseAdapter = new phpDataMapper_Adapter_Mysql('database server', 
		'database name', 'username', 'password');
	
	$dataSaver = new DataSaver($databaseAdapter);
	$dataSaver->migrate();

	// Save any sent in data into the database
	if (isset($_GET['data'])) {
		$newData = $dataSaver->get();	
		$newData->data = $_GET['data'];
		$dataSaver->save($newData);
	}

	// Return JSON of all of the previous data
	$previousData = $dataSaver->all();
	
	// Create JSON array
	print("{thedata:[");
	
	foreach ($previousData as $pd) {
		print("{data:" . $pd->data . "},");
	}
	
	print("]}");
?>
		

Parsing that with Processing

To parse the above with Processing, you would use code such as the following:

JSONArray jsonArray;

void setup() {
 size(500, 500);
 JSONObject jsonObj =
loadJSONObject("http://walking-productions.com/datasaver/datastorage.php");
 print(jsonObj);
 jsonArray = jsonObj.getJSONArray("thedata");
}

void draw() {
 int pyval = 0;
 int yval = 0;
 for (int i = 0; i < jsonArray.size(); i++)
 {
   pyval = yval;
   yval = jsonArray.getJSONObject(i).getInt("data");
   line((i-1), pyval, i, yval);
 }
}		

Arduino Yun

The Arduino Yun is a hybrid linux/arduino device. It is one half normal Arduino and one half linux computer. It has capabilities to exist online and act as a web server or client.

The example below reads data from a sensor and sends it via an HTTP GET request to a PHP script.

#include <Process.h>

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // Initialize Bridge
  Bridge.begin();

  // Initialize Serial
  Serial.begin(9600);

  // Wait until a Serial Monitor is connected.
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }  
   establishContact();
}

void loop() {
  Serial.print('B');
    delay(300);

  sensorValue = analogRead(sensorPin);  

  String parameter = "data=" + String(sensorValue);
  Serial.println(parameter);

  Process p;
  p.begin("curl");
  p.addParameter("http://walking-productions.com/datasaver/datastorage.php");
  p.addParameter("-G");
  p.addParameter("-d");
  //p.addParameter("data=5");
  p.addParameter(parameter);
  p.addParameter("-i");
  p.run();

  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }

  while(true);
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A');   // send a capital A
    delay(300);
  }
}