Customize sketch ocsp5

edited April 22 in Library Questions

Hello friends, I have a simple example that runs perfectly with the NET library. I would like to do exactly the same process but using the OCSP5 library. I have tried in many ways without success. If anyone can help me, thank you.

import processing.net.*;

Client c;


void setup() {
  size(200, 200);
  background(50);
  fill(200);
  c = new Client(this, "192.168.1.2", 8080); // Connect to server on port 8080

}

void draw() {
    c.write("rele1");
    delay(2000);

}

Answers

  • Can you give an example of what you tried with the OscP5 library that didn't work? It's easier to correct mistakes than giving a complete solution.

    Bear in mind that an OSC message requires an address pattern in addition to a target IP address and port.

  • I have a hardware that changes the state of a relay when it receives the string "rele1". Using the java mode processing and this sketch I put here works fine but I could not use any examples from the ocsp5 library to do what I need. I changed the ip and port values ​​in the examples. It does not make a mistake but it's not right, I can not change the state of the relay by sending this string. About what I did in the ocsp5 examples, I just changed ip and port.

  • That won't work. You need to know the address pattern. I suspect "rele1" is actually the address pattern and you need to send a blank message. So try to modify an OscP5 example so it sends an empty message with "rele1" or "/rele1" as its address pattern. This might already work (it's a slightly modified OscP5 example sketch):

    import oscP5.*;
    import netP5.*;
    
    OscP5 oscP5;
    NetAddress myRemoteLocation;
    
    void setup() {
      size(400,400);
      frameRate(25);
      /* start oscP5, listening for incoming messages at port 12000 */
      oscP5 = new OscP5(this,12000);
    
      /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
       * an ip address and a port number. myRemoteLocation is used as parameter in
       * oscP5.send() when sending osc packets to another computer, device, 
       * application. usage see below. for testing purposes the listening port
       * and the port of the remote location address are the same, hence you will
       * send messages back to this sketch.
       */
      myRemoteLocation = new NetAddress("127.0.0.1",12000);
    }
    
    void draw() {
      background(0);  
    }
    
    
    void mousePressed() {
      /* create a new osc message object */
      OscMessage myMessage = new OscMessage("/rele1");
    
     //no need to include values to the message
    
      /* send the message */
      oscP5.send(myMessage, myRemoteLocation); 
    }
    

    If "rele1" is not the address pattern, the specification of your device should specify what it is then, since an OSC message always requires this.

  • The default communication is TCP, I have to create a socket between the program and the hardware. This example I have tested and not obitive success. I tried the example ocsp5tcop and a message appears in my console that the socket was successfully created but the communication does not happen. What I find strange is that when I give the command:

    Println (myMessage);

    What I get in response is:

    Null: 0 | / Rele1
    

    I do not know why this null: 0 | pops up. What can it be? Thank you very much in advance.

  • I think the Null and 0 refer to the IP address and port, respectively. That they're null/0 probably means that the message was not sent properly or something went wrong. Could you share your whole code please? Or at least the relevant part to sending the OSC message.

  • import oscP5.*;
    import netP5.*;
    
    
    OscP5 oscP5tcpServer;
    
    OscP5 oscP5tcpClient;
    
    NetAddress myServerAddress;
    
    void setup() {
      /* create  an oscP5 instance using TCP listening @ port 11000 */
      oscP5tcpServer = new OscP5(this, 11000, OscP5.TCP);
    
      /* create an oscP5 instance using TCP. 
       * the remote address is 127.0.0.1 @ port 11000 = the server from above
       */
      oscP5tcpClient = new OscP5(this, "192.168.1.2", 8080, OscP5.TCP);
    }
    
    
    void draw() {
      background(0);  
    }
    
    
    void mousePressed() {
      /* the tcp client sends a message to the server it is connected to.*/
      oscP5tcpClient.send("rele1", new Object[] {new Integer(1)});
    }
    
    
    void keyPressed() {
      /* check how many clients are connected to the server. */
      println(oscP5tcpServer.tcpServer().getClients().length);
    }
    
    void oscEvent(OscMessage theMessage) {
      /* in this example, both the server and the client share this oscEvent method */
      System.out.println("### got a message " + theMessage);
      if(theMessage.checkAddrPattern("/rele1")) {
        /* message was send from the tcp client */
        OscMessage m = new OscMessage("/response");
        m.add("server response: got it");
        /* server responds to the client's message */
        oscP5tcpServer.send(m,theMessage.tcpConnection());
      }
    }
    

    This code is what I'm using, and below is the output on the console:

    __OscP5 0.9.9 infos, comments, questions at http://www.sojamo.de/oscP5

    [2017/4/21 19:58:6] PROCESS @ OscP5 stopped.

    [2017/4/21 19:58:7] PROCESS @ TcpServer ServerSocket started @ 11000

    OscP5 0.9.9 infos, comments, questions at http://www.sojamo.de/oscP5

    [2017/4/21 19:58:7] PROCESS @ OscP5 stopped.

    [2017/4/21 19:58:8] PROCESS @ TcpClient ### starting new TcpClient 192.168.1.2:8080_

    _

  • Where did you put the println(myMessage);? What happens when you click with the mouse in the sketch?

    Do you have a specification document for the device?

  • edited April 22

    It was a mess of mine, that's not why I give the println command. In this example, if I click the mouse nothing happens. If I press any key it returns me "0".

  •  /**
     * oscP5flush by andreas schlegel
     * example shows how to send osc messages without having to instantiate an oscP5 object.
     * this can be useful if you are not listening for incoming messages and you
     * want to avoid to have the additional oscP5 thread running listening for incoming 
     * message (which you wont need if you are only sending messages).
     * oscP5 website at http://www.sojamo.de/oscP5
     */
    
    import oscP5.*;
    import netP5.*;
    
    
    NetAddress myRemoteLocation;
    void setup() {
      size(400,400);
      frameRate(25);
      /* set up a remote location */
      myRemoteLocation = new NetAddress("192.168.1.2",8080);
    }
    
    
    void draw() {
      background(0);
    }
    
    
    void mousePressed() {
      /* create a new OscMessage with an address pattern, in this case /test. */
      OscMessage myOscMessage = new OscMessage("rele1");
    
      /* add a value (an integer) to the OscMessage */
     // myOscMessage.add(100);
    
      /* send the OscMessage to the remote location. 
       */
      OscP5.flush(myOscMessage,myRemoteLocation);
      println(myOscMessage);
    }
    

    This is the sketch that returns me "null: 0 | rele1" when I just assign the value "rele1" to the variable myOscMessage

  • I think that just means the OscP5.flush() method does not change the myOscMessage variable.

    When I tried your TCP sketch this is my output:

    OscP5 0.9.9 infos, comments, questions at http://www.sojamo.de/oscP5
    
    
    ### [2017/4/22 2:30:5] PROCESS @ OscP5 stopped.
    ### [2017/4/22 2:30:6] PROCESS @ TcpServer ServerSocket started @ 11000
    OscP5 0.9.9 infos, comments, questions at http://www.sojamo.de/oscP5
    
    
    ### [2017/4/22 2:30:6] PROCESS @ OscP5 stopped.
    ### [2017/4/22 2:30:27] ERROR @ TcpClient IOException while trying to create a new socket.
    

    then when I hit a key in the window I get a "0". This tells me there are no clients. I have no experience with this method of sending OSC.

    Trying to find something obvious we missed:

    Hello friends, I have a simple example that runs perfectly with the NET library. I would like to do exactly the same process but using the OCSP5 library. I have tried in many ways without success. If anyone can help me, thank you.

    Does that mean the code you posted in your first post manages to activate the relay?

  • The first sketch I put in works fine. I'm migrating from library because ocsp5 has support for android mode

  • Why are you using port 8080? Is this part of the instruction from your device?

    What you need to do first is run two sketches. One to be the client and one to be the server. You will use a different port, and avoid those ones with defined functionalities. For example, 12345 is a common one to try. You need to become familiar with how oscP5 works. It is not difficult to get it running and to send data between sketches. The sketches could even be running in different computers. However, they both need to be under the same network. One needs to be a server and the second needs to be a client. The server must be running first, as you might already know.

    To clarify, you are using your NET library at all. You are using Processing to communicate to your Android device? I have tested oscP5 in android and it works fine. I will suggest using UDP to start. UDP is just the standard protocol used by oscP5. Then you can switch to TCP/IP.

    The examples of this page are perfect for your case: http://www.sojamo.de/libraries/oscP5/#examples

    Kf

  • The first sketch I put in works fine. I'm migrating from library because ocsp5 has support for android mode

    Ohhhh. #-o That means OSC won't work. An OSC message follows a strict structure. It starts with a string identifying itself as an OSC command, then there is a type tag string and the command string and probably some other information too. It's not just the command string but a whole lot of fluff too. The device does not implement the OSC protocol and does not know what to do with the information it receives. You can't use the OSC library for your purpose and your original question is wrong, it should actually be "how can one send TCP messages in android mode".

  • Why are you using port 8080? Is this part of the instruction from your device?

    Yes, my device is configured for an 8080 port, I can change it though I do not think so.

    What you need to do first is run two sketches. One to be the client and one to be the server. You will use a different port, and avoid those ones with defined functionalities. For example, 12345 is a common one to try. You need to become familiar with how oscP5 works. It is not difficult to get it running and to send data between sketches. The sketches could even be running in different computers. However, they both need to be under the same network. One needs to be a server and the second needs to be a client. The server must be running first, as you might already know.

    On this, I saw a video note 10 that explains very well about this process:

    To clarify, you are using your NET library at all. You are using Processing to communicate to your Android device? I have tested oscP5 in android and it works fine. I will suggest using UDP to start. UDP is just the standard protocol used by oscP5. Then you can switch to TCP/IP.

    In fact my intention is to communicate android with ESP8266. All the communications I make between antre processing and ESP I use UDP. But this hardware that I have specifically it is not my product and already has an application of its own. What do I want and make another computer program from scratch. But in the future I will only use UDP.

    Ohhhh. That means OSC won't work. An OSC message follows a strict structure. It starts with a string identifying itself as an OSC command, then there is a type tag string and the command string and probably some other information too. It's not just the command string but a whole lot of fluff too. The device does not implement the OSC protocol and does not know what to do with the information it receives. You can't use the OSC library for your purpose and your original question is wrong, it should actually be "how can one send TCP messages in android mode".

    I understand. But when I search TCP android what comes next is this library. If it does not work for now, I'll go look for some others. Do you know any?

  • To clarify, ESP8266 is an external hardware and you want to use Processing to build an application that allows you to connect to this device. The device is a wifi module so I understand. You can use the ketai library. It has a WiFi example that you could use for your application.

    For oscP5, it works in android mode and it will work out of the box if it talks to another application running osc. If you want to run oscP5 on Android on one side and the processing'e net library in a computer on the other side, android will be able to deliver messages to the P3 net program. However, sending data to the android won't work. The reason is that oscP5 expects certain format when receiving data. I believe if you build this structure in your computer side right before you send it, the oscP5 on the Android side should be able to read it. I haven't tried myself.... it is just an idea.

    Kf

  • Answer ✓

    Sorry for the delay in answering, I was testing. I gave up trying to use the ocsp5 library to trigger that hardware I had here. I will not need it in the future. Using esp8266 + ocsp + UDP worked very well. As for the Ketai library seems interesting but I have not tested it yet. Thank you, from the bottom of my heart, for personal help.

Sign In or Register to comment.