import mjs.processing.mobile.mlocation.*; //set up the variables we'll use for this sketch PClient client; PRequest request; PFont font; String msg; double[] coords = new double[2]; //this array stores the lat and lon coordinates from the GPS radio on the phone void setup() { // instantiate a new PClient object to connect to a webserver client = new PClient(this, "www.nodesnoop.com"); // set up the font for displaying text font = loadFont(); textFont(font); //get the coordinates MLocation.location(coords); //start the request to the web service //you can use the second one of these lines to toggle between using the location from the phone //and a default location (los angeles) request = client.GET("/clients/405/weather.php?lat=" + coords[0] + "&lon=" + coords[1]); //request = client.GET("/clients/405/weather.php?lat=34.052187&lon=-118.243425"); //we're not animating, so turn off draw loop noLoop(); } /* libraryEvent ** ** Don't worry too much about this function. Basically, the client library sets ** this as a "callback" function. As it's loading the requested file from the web server ** it calls this function, along with certain events such as the state of the request ** e.g. EVENT_CONNECTED, etc. ** for our purposes, we are only concerned about the PRequest.EVENT_DONE event ** which signifies that the file has loaded completely. ** when this happens, we set our msg string to the result of the web query. In this case ** the request returns the temperature (e.g. 65) in degrees F for today. */ void libraryEvent(Object library, int event, Object data) { if (library == request) { if (event == PRequest.EVENT_CONNECTED) { // connected, start reading the data request.readBytes(); redraw(); } else if (event == PRequest.EVENT_DONE) { //done reading msg = new String((byte[]) data); println(msg); //print out in the mobile processing debug window for testing purposes request.close(); //close the request redraw(); //call the draw() function once } else if (event == PRequest.EVENT_ERROR) { // an error occurred, get the error message msg = (String) data; redraw(); } } } void draw() { //check to see if we successfully loaded valid info into the msg variable if(msg == null) { //if we don't draw out a plain white background background(255); } else { //if we do, do something with that information. //in this example, we compare the integer value of msg (the integer temperature) //with a range of values to change the background color /*YOU CAN EDIT THIS CODE TO MAYBE DO SOMETHING MORE INTERESTING if(int(msg) > 60 && int(msg) <= 70) { background(255,255,0); } else if(int(msg) > 70 && int(msg) <= 100) { background(255,0,0); } else { background(0,0,255); } */ } //set the text color to black fill(0); //Again, if the message is valid, print the temperature if (msg != null) { // show the temperature text("The Temperature is: " + msg + " Degrees", 4, 4, width - 8, height - 8); } else if (request == null) { // prompt for the network request text("Press GET to start.", 4, 4, width - 8, height - 8); } else { // handle the network states, giving the user an indication of what's going on switch (request.state) { case PRequest.STATE_OPENED: text("Connecting...", 4, 4, width - 8, height - 8); break; case PRequest.STATE_FETCHING: text("Fetching...", 4, 4, width - 8, height - 8); break; case PRequest.STATE_ERROR: text("An error has occurred: " + msg, 4, 4, width - 8, height - 8); break; } } }