Created
May 11, 2012 18:55
-
-
Save memetical/2661690 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.*; | |
import processing.serial.*; | |
import cc.arduino.*; | |
Arduino arduino; | |
int pin = 13; | |
int myDay = 0; | |
int sw = 0; | |
String [] query = {"syria"}; | |
void setup() | |
{ | |
frameRate(1); | |
//println(Arduino.list()); | |
arduino = new Arduino(this, Arduino.list()[0], 57600); | |
arduino.pinMode(pin, Arduino.OUTPUT); | |
//noLoop(); | |
// for (int i=0; i<100; i++){ | |
// waterGO(1000); | |
// } | |
} | |
void draw() | |
{ | |
int d = day(); | |
int h = hour(); | |
int m = minute(); | |
int s = second(); | |
//if (d == 11 && h == 20 && m == 20 && s==0){ | |
// println("We are in the if"); | |
// waterGO(1000); | |
// myDay = d; | |
getData(); | |
//} | |
} | |
float getData(){ | |
// String term = ""; | |
// //populate the query array | |
// if (terms.length > 0){ | |
// term = terms[0]; | |
// for (int i = 1; i < terms.length; i++){ | |
// term = term + "+" + terms[i]; | |
// } | |
// } | |
String baseURL = "http://socialmention.com/search?"; | |
//String query = term+"&f=json&t=microblogs"; //"hope+syria&f=json&t=microblogs"; | |
String query = "syria&f=json&t=microblogs"; | |
String sources = "t[]=blogs&t[]=microblogs"; | |
int day = 86400; | |
int fromTS = day; | |
String request = baseURL + "q="+query+"&f=json&"+sources+"&lang=en&from_ts="+fromTS+"&sentiment"; | |
println(request); | |
float midval = .0; | |
//int midval = 0; | |
try { | |
JSONObject data = new JSONObject(join(loadStrings(request), "")); | |
JSONArray items = data.getJSONArray("items"); | |
println("items: "+ items.length()); | |
int sum = 0; | |
for (int j = 0; j < items.length(); j++) { | |
JSONObject item = items.getJSONObject(j); | |
println("sentiment: " + item.getInt("sentiment")); | |
sum = sum + item.getInt("sentiment"); | |
println("sum :" + sum); | |
} | |
//println("finalsum "+float(sum)+"/"+float(items.length())+" :"+sum); | |
midval = float(sum) / float(items.length()); | |
int amount = int(midval*100000); | |
println("midval: " + midval); | |
println(items.length()); | |
if(midval > 0) { | |
waterGO(amount); | |
} | |
} | |
catch (JSONException e) { | |
println ("There was an error parsing the JSONObject."); | |
} | |
return midval; | |
} | |
void waterGO(int amount) { | |
arduino.digitalWrite(pin, Arduino.HIGH); | |
delay(amount); | |
arduino.digitalWrite(pin, Arduino.LOW); | |
delay(amount); | |
println("WATER GOOOOOOOO!!!!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment