Server/Client setup in processing

HI,

I’m trying to get a server/ client connection going between the core and a processing application (using the network library) I found this processing example:

http://www.learningprocessing.com/examples/chapter-19/example-19-1/

which can connect to this local client (127.0.0.1):

http://www.learningprocessing.com/examples/chapter-19/example-19-2/

Now i tried to run the local communication example on the core (and changed the PORT to 5204). And hoped that the processing sketch would detect it, but it didn’t. I tried several ports (9000 as well) but I have the feeling it has to do with the IP.

I’m fairly new to setting up a client/ server communication and hope there is someone who can help met out to get it going.

Cheers,

R.

hi Ruben,

Must admit I don’t really know processing at all (only just about heard of it).

When you say local client do you mean local to the core?

127.0.0.1 is the address for localhost which will be whichever device you are using. i.e if you use 127.0.0.1 on the core it will basically talk to itself, similarly 127.0.0.1 on a computer is that computer itself. You will need to look up the IP address of the client you are trying to connect and send data to. Presumably it’s on your local network so that may well be 192.168.X.X

You should probably give the client you are trying to connect to a static IP just to help out the core (otherwise you may end up changing the address on the core a lot).

Here is some example code that sends data to my local server on port 8080.

TCPClient client;
char url[] = "192.168.2.2";
char initReq[] = "POST /Magni/index.php?";
char version[] = "&version=1 HTTP/1.1";
int port = 8080;

void setup() {    

if(client.connect(url, port)){
    digitalWrite(LED, HIGH);
    String request = initReq + String("type=start&core=") + Spark.deviceID() + version;
    client.println(request);
    client.print("HOST: ");
    client.println(url);
    client.println("Connection: close");

    client.println();
    client.flush();

}
}

void loop() {


if(client.connected() || client.connect(url, port)){
        String request = initReq + String("type=alert&core=") + Spark.deviceID() + version;
        client.println(request);
        client.print("HOST: ");
        client.println(url);
        client.println("Connection: close");

        client.println();
        client.flush();
    }
}
}

Hope that helps,

Stuart

Thanks for your help. I didn’t yet get it to work now. But will try later today. I’ll let you know what happens

Hey Stuart,

I tried several things and changed the IP to my computer’s one (which is running the processing server app)but it seems that it is not working. It’s hard for me to troubleshoot since i don’t have that much experience with it.

Maybe, or somene else, you have some additional suggestions?

Cheers,
R.

@ruben_vleuten could you share your code, or perhaps a snippet that we can use to help debug?

Hi Zach,

The processing code is the one below (which I got from example). It creates a a simple server. This works since I’m able to create a client on another Mac which can communicate with the server.

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 19-1: Simple therapy server

// Import the net libraries
import processing.net.*;

// Declare a server
Server server;

// Used to indicate a new message has arrived
float newMessageColor = 255;
PFont f;
String incomingMessage = “”;

void setup() {
size(400,200);

// Create the Server on port 5204
server = new Server(this, 5204);
f = createFont(“Arial”,16,true);
}

void draw() {
background(newMessageColor);

// newMessageColor fades to white over time
newMessageColor = constrain(newMessageColor + 0.3,0,255);
textFont(f);
textAlign(CENTER);
fill(255);

// The most recent incoming message is displayed in the window.
text(incomingMessage,width/2,height/2);
// If a client is available, we will find out
// If there is no client, it will be"null"
Client client = server.available();
// We should only proceed if the client is not null
if (client!= null) {

// Receive the message
// The message is read using readString().
incomingMessage = client.readString(); 
// The trim() function is used to remove the extra line break that comes in with the message.
incomingMessage = incomingMessage.trim();

// Print to Processing message window
println( "Client says:" + incomingMessage);

// Write message back out (note this goes to ALL clients)
server.write( "How does " + incomingMessage + " make you feel?\n" ); // A reply is sent using write().

// Reset newMessageColor to black
newMessageColor = 0;

}
}

// The serverEvent function is called whenever a new client connects.
void serverEvent(Server server, Client client) {
incomingMessage = "A new client has connected: " + client.ip();
println(incomingMessage);
// Reset newMessageColor to black
newMessageColor = 0;
}

For creating a client on the Core, I tried to use the code from Stuart (above), which I did not get to work, and also tried the local communication one from your website ( both I used on different ports)

Hope you can help,

Cheers,
R.

I managed to do it using the code in the client.connect() reference. This is the code I used:

TCPClient client;

byte server[] = {192, 168, X, X }; // this should be the IP address of the computer running the server

void setup()
{
Serial.begin(9600);
delay(1000);
Serial.println(“connecting…”);

if (client.connect(server, 9000))
{
Serial.println(“connected”);

}
else
{
Serial.println(“connection failed”);
}
}

void loop()
{

}