WiFi Communication between PC and Android Problem

Hi all,

I am trying to establish bidirectional communication via Wifi between an Android device and a PC running Windows. I have followed the Daniel Sauter's book "Rapid Android Development", Chapter 6: Networking devices with Wifi. https://www.mobileprocessing.org/networking.html
I am trying the first example of the chapter.

On the Android side, the code can be found at:

https://github.com/danielsauter/rapid-android-development/blob/master/code/Networking/WiFiDataExchangeAndroid/WiFiDataExchangeAndroid.pde

On the PC side, the code can be found at:

https://github.com/danielsauter/rapid-android-development/blob/master/code/Networking/WiFiDataExchangePC/WiFiDataExchangePC.pde

When running both codes, the android device gets the messages from the PC but the PC doesn't get the messages sent by the Android device. Is there any solution? If not, Is there another approach to establish the communication using Processing?

I am using:

PC: Windows 10 v.1709, Processing 3.3.6, Android Mode 4.0, Ketai 14, oscP5 0.9.9

Android device: Samsung S5 Mini (SM-G800F), Android 6.0.1

Thanks in advance!

Answers

  • Solved!

    I found another discussion with a similar problem:

    https://forum.processing.org/two/discussion/18194/receive-osc-using-oscp5-library-android-mode-4-0-beta2

    It seems that oscP5 is not compatible with Android Mode 4.0. So, I have changed the codes to use the library UDP and now it works.

  • Print, please, the redesigned code under to use the library UDP

  • I found the working code. Maybe someone will need it. Processing 3.3.6, Android Mode 4.0. Phone: Android 7.0.

    https://github.com/gratefulfrog/Car/wiki/Processing-Android-mode-and-Ketai-Library#wifi

    I delete this part: public void settings(){ fullScreen();

    and replace in setup() this : orientation(PORTRAIT); //LANDSCAPE);

    work code see below:

    For Pc javaMode:

    
    import oscP5.*;
    import netP5.*;
    
    OscP5 oscP5;
    NetAddress remoteLocation;
    float accelerometerX, accelerometerY, accelerometerZ;
    
    void setup() {
      size(480, 480);
      oscP5 = new OscP5(this, 18000);
      remoteLocation = new NetAddress("192.168.1.34", 18000); // Customize! input android device ip!!!!!!
      textAlign(CENTER, CENTER);
      textSize(24);
    }
    void draw() {
      background(78, 93, 75);
      text( "Remote Accelerometer Info: " + "\n" +
            "x: "+ nfp(accelerometerX, 1, 3) + "\n" +
            "y: "+ nfp(accelerometerY, 1, 3) + "\n" +
            "z: "+ nfp(accelerometerZ, 1, 3) + "\n" +
            "Local Info: \n" +
            "mousePressed: " + mousePressed, width/2, height/2);
    
      OscMessage myMessage = new OscMessage("mouseStatus");
      myMessage.add(mouseX);
      myMessage.add(mouseY);
      myMessage.add(int(mousePressed));
      oscP5.send(myMessage, remoteLocation);
    }
    
    void oscEvent(OscMessage theOscMessage) {
      if (theOscMessage.checkTypetag("fff")) {
        accelerometerX = theOscMessage.get(0).floatValue();
        accelerometerY = theOscMessage.get(1).floatValue();
        accelerometerZ = theOscMessage.get(2).floatValue();
      }
    }
    

    For Android device, android Mode:

    
    /* Wifi to pc from Nexus 4
     * works PERFECTLY !!!
     */
    
    
    import netP5.*;
    import oscP5.*;  
    import ketai.net.*;
    import ketai.sensors.*;
    OscP5 oscP5;
    KetaiSensor sensor;
    
    
    NetAddress remoteLocation;
    float myAccelerometerX, myAccelerometerY, myAccelerometerZ;
    int x, y, p;
    String myIPAddress;
    String remoteAddress = "192.168.1.2"; // input Pc ip!!!!!!!!!!!!!!!!!!!
    
    
    
    void setup() {
      sensor = new KetaiSensor(this);
      orientation(PORTRAIT);
      textAlign(CENTER, CENTER);
      textSize(36);
      initNetworkConnection();
      sensor.start();
    }
    
    void draw() {
      background(78, 93, 75);
      
      text( "Remote Mouse Info: \n" +
            "mouseX: " + x + "\n" +
            "mouseY: " + y + "\n" +
            "mousePressed: " + p + "\n\n" +
            "Local Accelerometer Data: \n" +
            "x: " + nfp(myAccelerometerX, 1, 3) + "\n" +
            "y: " + nfp(myAccelerometerY, 1, 3) + "\n" +
            "z: " + nfp(myAccelerometerZ, 1, 3) + "\n\n" +
            "Local IP Address: \n" + myIPAddress + "\n\n" +
            "Remote IP Address: \n" + remoteAddress, width/2, height/2);
      
      OscMessage myMessage = new OscMessage("accelerometerData");
      myMessage.add(myAccelerometerX);
      myMessage.add(myAccelerometerY);
      myMessage.add(myAccelerometerZ);
      oscP5.send(myMessage, remoteLocation);
    }
    
    void oscEvent(OscMessage theOscMessage) {
      if (theOscMessage.checkTypetag("iii")){
        x = theOscMessage.get(0).intValue();
        y = theOscMessage.get(1).intValue();
        p = theOscMessage.get(2).intValue();
      }
    }
    
    void onAccelerometerEvent(float x, float y, float z){
      myAccelerometerX = x;
      myAccelerometerY = y;
      myAccelerometerZ = z;
    }
    void initNetworkConnection(){
      oscP5 = new OscP5(this, 18000);
      remoteLocation = new NetAddress(remoteAddress, 18000);
      myIPAddress = KetaiNet.getIP();
    }
    
  • Hi! Thanks for your answer.

    Where you have made the changes? Do you have modified the libraries? Because in my case, your code is still not working in my Samsung S5 mini.

    See below the codes working with UDP

    For PC (Java mode):

    //Libraries
    import hypermedia.net.*;
    
    // Communication
    UDP udp;
    String ip = "192.168.1.125";
    int port = 50000;
    
    // Variables
    float accelerometerX, accelerometerY, accelerometerZ;
    
    void setup() {
      size(480, 480);
    
      // Object UDP
      udp = new UDP (this, port);
      //udp.log(true);  // Prints the connection activity
      udp.listen(true);
    
    
      textAlign(CENTER, CENTER);
      textSize(24);
    }
    
    void draw() {
    
      background(78, 93, 75);
      text("Remote Accelerometer Info: " + "\n" +
        "x: "+ nfp(accelerometerX, 1, 3) + "\n" +
        "y: "+ nfp(accelerometerY, 1, 3) + "\n" +
        "z: "+ nfp(accelerometerZ, 1, 3) + "\n\n" +
        "Local Info: \n" + 
        "mousePressed: " + mousePressed, width/2, height/2);
    
      // create and send message
      String message = str(mouseX) + "," + str(mouseY) + "," + str(int(mousePressed)) +"\n";
      udp.send(message, ip, port);
    
    }
    
    // Receive Event
    void receive( byte[] data, String ip, int port)
    {
    
      data = subset(data,0, data.length-1);
      String message = new String(data);
      String[] values = split(message,",");
    
      accelerometerX = float(values[0]);
      accelerometerY = float(values[1]);
      accelerometerZ = float(values[2]);
    
      // print the result
      //println( "receive: \""+message+"\" from "+ip+" on port "+port );
    
    }
    

    For Android device (Android Mode):

    // Libraries
    import hypermedia.net.*;
    import ketai.net.*;
    import ketai.sensors.*;
    
    // Communication
    UDP udp;
    String ip = "192.168.1.117";
    int port = 50000;
    
    // Android sensors
    KetaiSensor sensor;
    
    // Variables 
    float myAccelerometerX, myAccelerometerY, myAccelerometerZ;
    int x, y, p; 
    String myIPAddress; 
    
    void setup() {
    
      sensor = new KetaiSensor(this);
    
      orientation(PORTRAIT);
      textAlign(CENTER, CENTER);
      textSize(48);
    
      // Object UDP
      udp = new UDP (this, port);
      //udp.log(true); // Prints the connection activity
      udp.listen(true);
    
      sensor.start();
      myIPAddress = KetaiNet.getIP(); 
    }
    
    void draw() {
    
      background(78, 93, 75);
    
      text("Remote Mouse Info: \n" +                          // 3
      "mouseX: " + x + "\n" +
        "mouseY: " + y + "\n" +
        "mousePressed: " + p + "\n\n" +
        "Local Accelerometer Data: \n" + 
        "x: " + nfp(myAccelerometerX, 1, 3) + "\n" +
        "y: " + nfp(myAccelerometerY, 1, 3) + "\n" +
        "z: " + nfp(myAccelerometerZ, 1, 3) + "\n\n" +
        "Local IP Address: \n" + myIPAddress + "\n\n" +
        "Remote IP Address: \n" + ip, width/2, height/2);
    
      // Create and send message
      String message = str(myAccelerometerX) + "," + str(myAccelerometerY) + "," + str(myAccelerometerZ) +"\n";
      udp.send(message, ip, port);
    }
    
    // Receive Event
    void receive( byte[] data, String ip, int port)
    {
    
      data = subset(data,0, data.length-1);
      String message = new String(data);
      String[] values = split(message,",");
    
      x = int(values[0]);
      y = int(values[1]);
      p = int(values[2]);
    
      // print the result
      //println( "receive: \""+message+"\" from "+ip+" on port "+port );
    
    }
    
    void onAccelerometerEvent(float x, float y, float z)
    {
      myAccelerometerX = x;
      myAccelerometerY = y;
      myAccelerometerZ = z;
    }
    
Sign In or Register to comment.