// web proxy // This example demonstrates the basic usage of mobileProcessing // to relay information received from the phone serial port to a website and back import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.*; PFont font; String ports; // port used for io from the phone - usually COM0 String rmessage; // info received from the arduino; payload byte[] smessage; // data received from web String statusmsg = "any key to start"; // process updates on screen for debugging String appstatus = ". . ."; // var to track states on the application boolean serialstatus = false; int endbyte = 35; //# end of serial data -> carriage return 10 // data received from the controller String domain = "none"; // domain info for gprs connection String path = "none"; // file path on server starting with a / String querystring; // IO InputStream m_is; OutputStream m_os; CommConnection cc; // net PClient client; PRequest request; void setup() { noLoop(); font = loadFont(FACE_SYSTEM, STYLE_PLAIN, SIZE_SMALL); textFont(font); ports = System.getProperty("microedition.commports"); } void draw() { background(255); fill(0); if (ports != null) { if (serialstatus == false) { //create connection try { // the speed of the connection may have to change to match that of your phone. cc = (CommConnection) Connector.open("comm:COM0;baudrate=9600;autocts=off;autorts=off",Connector.READ_WRITE,true); m_is = cc.openInputStream(); m_os = cc.openOutputStream(); appstatus = "Connected: " + ports; serialstatus = true; } catch( Throwable t ) { text("Error " + t, 0, 8, width, 20); } } text(new String(appstatus), 0, 8, width, 20); text(":: " + new String(statusmsg), 0, 26, width, height); } else { text("No serial ports found", 0, 26, width, height); } } // app will only start when users presses any key void keyPressed() { getserialdata(); } void getserialdata() { try { //wait on the serial for data until we get the endbyte int ch = 0; // null char rmessage = "nill,"; // start the string with a nill value while(ch !=endbyte ) { if (m_is.available() > 0) { ch = m_is.read(); if (ch != endbyte) { rmessage = rmessage + (char) ch; // append data each char received to build the query string } } } statusmsg = rmessage; redraw(); sendWebData(rmessage); // call the web get routine } // catch any errors catch( Throwable t ) { statusmsg = "Error: " + t; redraw(); } } void sendWebData (String args) { // append data collected from serial to the query string and send the GET request String info[] = split(args,","); //item 0 is nill, items 1 and 2 are the domain and the file if (info.length >= 3) { domain = info[1]; path = info[2]; querystring = "?"; if (info.length ==4) { // this means we are sending something to the website querystring = "?" + info[3]; } } client = new PClient(this, domain); request = client.GET(path + querystring); } // the web library will generate events that will be caught and handled by the cases below void libraryEvent(Object library, int event, Object data) { if (library == request) { if (event == PRequest.EVENT_CONNECTED) { // connected, start reading the data request.readBytes(); statusmsg = "Fetching. . ."; redraw(); } else if (event == PRequest.EVENT_DONE) { // done reading smessage = (byte[]) data; request.close(); statusmsg = "Got data!"; redraw(); // pipe data received down the serial port writeToSerial(smessage); } else if (event == PRequest.EVENT_ERROR) { // an error occurred, get the error message statusmsg = (String) data; redraw(); } } } void writeToSerial(byte[] smessage) { try { //write the message bytes m_os.write(smessage); // go back to listen for more data on the serial port getserialdata(); } catch( Throwable t ) { statusmsg = "Error: " + t; redraw(); } }