Just a small follow up question .
This is not giving the desired response.
void receive( byte[] data, String ip, int port ) {
data = subset(data, 0, data.length);
message = new String( data );
if(message == "ack_on") received = 1;
if(message == "ack_off") received = 0;
}
But this is working as I wanted. What is the difference between the two? It should have been same in my opinion.
void receive( byte[] data, String ip, int port ) {
data = subset(data, 0, data.length);
message = new String( data );
received = "ack_on".equals(message);
}
I will put the full code again just for clarity. The commented portion is the not working portion. I had to modify my code a bit to make it look a bit decent
import controlP5.*; //import ControlP5 library
import hypermedia.net.*;
UDP udp;
ControlP5 cp5;
PImage led_red, led_green, led_white;
PFont font;
String message;
int received = 2;
Boolean breceived;
void setup() {
size(300, 300);
cp5 = new ControlP5(this);
font = createFont("Ubuntu Mono Bold", 15);
led_red = loadImage("red.png");
led_green = loadImage("green.png");
led_white = loadImage("white.png");
udp = new UDP(this, 6124);
udp.listen(true);
// Add the buttons to GUI
cp5.addButton("ON") //The button
.setPosition(50, 60) //x and y coordinates of upper left corner of button
.setSize(70, 50) //(width, height)
;
cp5.addButton("OFF") //The button
.setPosition(50, 120) //x and y coordinates of upper left corner of button
.setSize(70, 50) //(width, height)
;
}
void draw() { //Same as loop in arduino
background(150, 150, 150);
textFont(font);
text("Remote Control Interface", 70, 30);
image(led_white, 140, 60);
image(led_white, 140, 120);
if (received == 1) {
image(led_green, 140, 60);
text("Ack for ON", 186, 88);
}
if (received == 0) {
image(led_red, 140, 120);
text("Ack for OFF", 186, 148);
}
}
void ON() {
send("ON");
}
void OFF() {
send("OFF");
}
void receive( byte[] data, String ip, int port ) { // <-- extended handler
data = subset(data, 0, data.length);
message = new String( data );
//if (message == "ack_on") received = 1;
//if (message == "ack_off") received = 0;
breceived = "ack_on".equals(message);
if(breceived) received =1;
else if(!breceived) received =0;
println( "receive: \""+message+"\" from "+ip+" on port "+port );
}
void send(String message) {
//String message = "ON"; // the message to send
String ip = "localhost"; // the remote IP address
int port = 6123; // the destination port
udp.send( message, ip, port );
}