Comm Lab Web Week 6

Other Platforms

Processing

loadStrings

String[] response;
void setup() {
 	response = loadStrings("http://www.google.com/");
	println(response);    
}
		

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

Generate JSON with PHP

<?
	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', 
		'tablename', '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("]}");
?&g;
		

Parsing that with Processing

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

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