// 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 int endbyte = 35; //# end of serial data -> carriage return 10 int ackback = 33; //! ack char 6 int endtransmission = 35; //# end of transmission 4 boolean serialstatus = false; // data received from the controller // they have to be passed at the start of the app // as a string - separated by a comma: domain,/path/filename.ext String domain = "none"; // domain info for gprs connection String path = "none"; // file path on server starting with a / // 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 the sketch detected a serial port, it will show up here // My is hardcoded to COM0 but one could change that to match their system // some phone may have more than one port - like a serial and a infrared port. . . 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() { if (domain == "none") { statusmsg = "getting server info"; redraw(); getconnectioninfo(); } } void getconnectioninfo() { try { //query port for domain and path by sending the endbyte m_os.write(endbyte); int ch = 0; // null char rmessage = "nill,"; // this initial value wont be used. . . while(ch !=endbyte ) { // the endbyte tells the phone that the data transfer is over 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 } } } // once the transmission is over we pick the server info and the path to the file from the list String info[] = split(rmessage,","); if (info.length == 3) { domain = info[1]; path = info[2]; client = new PClient(this, domain); // this message never shows up because the phone moves on to look for data on the serial port again // maybe someone could figure out how to properly update the screen with status data statusmsg = "connecting to: " + domain + path; redraw(); // Now the phone moves on to monitor the serial port for more data getserialdata(); } } // catch any errors catch( Throwable t ) { statusmsg = "Error: " + t; redraw(); } } void getserialdata() { // same thing here, info does not have enough time to be displayed but that is the idea statusmsg = "waiting for info"; redraw(); try { m_os.write(ackback); //wait on the serial for data until we get the endbyte int ch = 0; // null char rmessage = "?"; // start the string with a ? which we will use to build the query_string 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 request = client.GET(path + args); } // 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 { m_os.write(smessage); statusmsg = " message sent to serial - done!"; redraw(); // go back to listen for more data on the serial port getserialdata(); } catch( Throwable t ) { statusmsg = "Error: " + t; redraw(); } }