Arduino Due Server Communicating with Processing GUI

I am trying to build a system for remote controlling a sound controller which I am going to feed using an Arduino Due. I want to communicate using ethernet and for that reason, I planned to use Due as the server and use Processing to create a GUI as the client so I can connect to the Due from my PC and control the pins from the GUI.
I am relatively new to using ethernet for control so I am trying to learn the libraries for both Arduino (which I am using EthernetENC library because I have an ENC28J60 breakout board to connect the Due to the router) and Processing 3. Because of this reason, I first wanted to do something more simple to get the hang of it so I decided to use an RGB LED to control the color which is similar to what I am actually planning to do.
I managed to control the LED by creating a web page and from the web page I can control the LED light just like I wanted. Here is the Arduino code for that:

// After uploading the code use the command prompt and type "ping <Static/UDHP IP>"  to check if the server is ready.
#include <SPI.h>
#include <EthernetENC.h>

//
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  //physical mac address
byte ip[] = { 192, 168, 137, 205 };                   // IP address in LAN – need to change according to your Network address
byte gateway[] = { 192, 168, 137, 1 };                // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                 //subnet mask
EthernetServer server(80);                            //server port

String readString;
int redPin = 13;
int greenPin = 12;
int bluePin = 11;

void setup() {
  pinMode(redPin, OUTPUT);    //pin selected to control
  pinMode(bluePin, OUTPUT);   //pin selected to control
  pinMode(greenPin, OUTPUT);  //pin selected to control

  // Turn off all the channels
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, HIGH);

  // Test LED
  digitalWrite(redPin, LOW);  // set pin high
  delay(250);
  digitalWrite(redPin, HIGH);  // set pin low
  delay(250);
  digitalWrite(greenPin, LOW);  // set pin high
  delay(250);
  digitalWrite(greenPin, HIGH);  // set pin low
  delay(250);
  digitalWrite(bluePin, LOW);  // set pin low
  delay(250);
  digitalWrite(bluePin, HIGH);  // set pin high
  //start Ethernet
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
}

void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
        }

        //if HTTP request has ended– 0x0D is Carriage Return \n ASCII
        if (c == 0x0D) {
          client.println("HTTP/1.1 200 OK");  //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE> ETHERNET CONTROLLER</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<hr>");
          client.println("<hr>");
          client.println("<H1 style=\"color:green;\">ETHERNET CONTROLLER</H1>");
          client.println("<H1 style=\"color:green;\">COLOUR CONTROLLER FROM WEBPAGE</H1>");
          client.println("<hr>");

          client.println("<H2><a href=\"/?RED\"\">Red</a><br></H2>");
          client.println("<H2><a href=\"/?GREEN\"\">Green</a><br></H2>");
          client.println("<H2><a href=\"/?BLUE\"\">Blue</a><br></H2>");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(10);
          // stopping client
          client.stop();

          // control arduino pin
          if (readString.indexOf("?RED") > -1)  // checks for RED
          {
            digitalWrite(redPin, !digitalRead(redPin));  // toggle redPin
            Serial.println("RED has toggled");
          }
          if (readString.indexOf("?GREEN") > -1)  // checks for GREEN
          {
            digitalWrite(greenPin, !digitalRead(greenPin)); // toggle greenPin
            Serial.println("GREEN has toggled");
          }
          if (readString.indexOf("?BLUE") > -1)  // checks for BLUE
          {
            digitalWrite(bluePin, !digitalRead(bluePin)); // toggle bluePin
            Serial.println("BLUE has toggled"); 
          }
          //clearing string for next read
          readString = "";
        }
      }
    }
  }
}

This is the web page I am using to control the LED

Right now I am trying to translate this to Processing 3 and I created a GUI with the following code and embedded the links for RED, GREEN and BLUE from the web page to the buttons just to see how it feels like but this is obviously not what I want to do:

import processing.net.*;

// Button settings
int redbuttonX, redbuttonY, greenbuttonX, greenbuttonY, bluebuttonX, bluebuttonY;      
int buttonSizeX = 150;     // Diameter of button
int buttonSizeY = 80;
color red, green, blue;
color redHighlight, greenHighlight, blueHighlight;
boolean redOver = false;
boolean redPressed = false;
boolean greenOver = false;
boolean greenPressed = false;
boolean blueOver = false;
boolean bluePressed = false;

void setup() {
  size(400, 600);
  background(color(150));
  red = color(250,0,0);
  green = color(0,250,0);
  blue = color(0,0,250);
  
  redHighlight = color(200,125,125);
  greenHighlight = color(125,200,125);
  blueHighlight = color(125,125,200);
  
  redbuttonX = width/2-buttonSizeX/2;
  redbuttonY = height/2-buttonSizeY/2-100;
  greenbuttonX = width/2-buttonSizeX/2;
  greenbuttonY = height/2-buttonSizeY/2;
  bluebuttonX = width/2-buttonSizeX/2;
  bluebuttonY = height/2-buttonSizeY/2+100;
  
  
}

void draw() {
  update(mouseX, mouseY);
  
  if (redOver) {
    fill(redHighlight);
  } else {
    fill(red);
  }
  stroke(25);
  strokeWeight(5);
  rect(redbuttonX, redbuttonY, buttonSizeX, buttonSizeY);
  
  if (greenOver) {
    fill(greenHighlight);
  } else {
    fill(green);
  }
  stroke(25);
  strokeWeight(5);
  rect(greenbuttonX, greenbuttonY, buttonSizeX, buttonSizeY);
  
  if (blueOver) {
    fill(blueHighlight);
  } else {
    fill(blue);
  }
  stroke(25);
  strokeWeight(5);
  rect(bluebuttonX, bluebuttonY, buttonSizeX, buttonSizeY);
  
  fill(0);
  textSize(30);
  text("Ethernet RGB GUI", redbuttonX+buttonSizeX/4-80, redbuttonY+buttonSizeY/2-100); 
}

void update(int x, int y) {
  if ( overButton(redbuttonX, redbuttonY, buttonSizeX, buttonSizeY) ) {
    redOver = true;
} else {
    redOver = false;
}
if ( overButton(greenbuttonX, greenbuttonY, buttonSizeX, buttonSizeY) ) {
    greenOver = true;
} else {
    greenOver = false;
}
if ( overButton(bluebuttonX, bluebuttonY, buttonSizeX, buttonSizeY) ) {
    blueOver = true;
} else {
    blueOver = false;
}

}
void mousePressed() {
  
  if (redOver) {
    println("Red button has been pressed!");
    if(!redPressed) {
      link("http://192.168.137.205/?RED");
    }
  }
  if (greenOver) {
    println("Green button has been pressed!");
    if(!greenPressed) {
      link("http://192.168.137.205/?GREEN");
    }

    }
  if (blueOver) {
    println("Blue button has been pressed!");
    if(!bluePressed) {
    link("http://192.168.137.205/?BLUE");
    }
    }
  delay(100);
}

boolean overButton(int x, int y, int width, int height)  {
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

I tried multiple things to connect Processing to Arduino server as client and I managed to do it with this code:

import processing.net.*;

Client c;
String input;

void setup() 
{
  size(450, 255);
  background(204);
  stroke(0);
  frameRate(25); // Slow it down a little
  // Connect to the server's IP address and port
  c = new Client(this, "192.168.137.205", 80); // Replace with your server's IP and port
}

void draw() 
{
    c.write("Hello Server\n");
  // Receive data from server
  if (c.available() > 0) {
    input = c.readString();
    input = input.substring(0, input.indexOf("\n")); // Only up to the newline
    textSize(25);
    text(input, width/2-75, height/2);
  }
}

And with a simple Arduino code such as this:

#include <SPI.h>
#include <EthernetENC.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 137, 205 };
byte gateway[] = { 192, 168, 137, 1 };
byte subnet[] = { 255, 255, 255, 0 };

String input;
String readString;
EthernetServerPrint server = EthernetServerPrint(80);

void setup() {
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.begin(9600);
}
void loop() {
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client > -1) {
    while (client.connected()) {
      if (client.available()) {
        input = client.readString();
        input = input.substring(0, input.indexOf("\n"));  // Only up to the newline
        server.write("Hello Client\n");
        Serial.println(input);
      }
    }
  }
}

I tried to receive and send "Hello Client/Server" text but I can't read the "Hello Client" message on the Processing GUI and I can only see the "Hello Server" message after I close the Processing GUI. My goal here was to send strings such as "RED", "GREEN" and "BLUE" and check these strings in Arduino just like in the web page version to toggle the pins. I am not comfortable with ethernet libraries still and I would like to have some advices to improve my code. Thanks in advance!

I notice a forward slash here /n   instead of a back-slash \n

Corrected, same result.

you tried something like

char msg[] = "Hello Client\n";
server.write(msg,sizeof(msg));

@KASSIMSAMJI
That would send the trailing null char….
The other version is correct if the server class supports sending a c-string

the one using server.println() ?

write() knows (usually) how to handle a c-string

client is an instance of EthernetClient, why would you compare that to an integer ?
usually one does just

if (client) {
  ...
}

I'm not familiar with EthernetENC so can't comment on the details. is a server an HTTP server of just socket based communication?

EthernetENC.h is very much the same as Ethernet.h with some minor requirements for some examples. Yes, it is an HTTP server where I am controlling the MCU from a web client (chrome) but I am trying to change this with a Processing 3 client (GUI) and I will discard the web client totally if I am able to communicate with the server on Arduino Due using my GUI. The HTTP server will still run in the background of course I just don't want to use a web browser for controlling since I have to type in the IP address every time I want to open the controlling interface. I want to create an executable GUI with Processing 3 for that purpose.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.