Hi guys
I'm new to processing but not new to programming. I'm working on a project that uses processing as a go-between, for Arduino input switches and arrays of LEDs, and php and .txt files stored on a web server.
To begin with. I had a version of the processing code to retrieve data from a .txt file on a server, called setting.txt. I used the "loadStrings" method for getting the data, and that worked fine. But, occasionally, my laptops connection to the server would drop out, and that caused the "loadStrings" method to fall over.
That method has scant information about dealing with errors, I tried the null method, but couldn't get that to work on an array.
So, I set my sights on something a little more robust. Here's what I came up with, the only problem is, occasionally, I get a "java.net. ConnectException: Connection timed out: connect" error.
I've searched the forum and could only find one other reference to a similar error, and there apparently was not a solution or cause posted regarding it.
So I'm slightly concerned, because there isn't a "3rd" method for me to try-out, and both have this nagging flaw which means the application can't be relied on.
Here's the latest version - if there's anyone clued up on either the "loadStrings" method or "ConnectException" bug, I'd appreciate your help.
There's no problems on the Arduino code side, that works fine, it's just these two processing methods that are causing the problem.
I'm running the code from processing, on Windows XP
Cheers
Code:
import processing.net.*;
import processing.serial.*;
Client client; // client used to post data to settings.txt
Client c; // client used to retireve data from settings.txt
Serial port;
String[] myString;
String[] theString;
String data;
int linefeed = 10;
int carriageReturn = 13;
void setup() {
Open the port that the board is connected to and use the same speed (9600 bps)
println(Serial.list());
port = new Serial(this, Serial.list()[1], 9600); // port #1 COM4
c = new Client(this, "www.myserver.com", 80); // Connect to server on port 80
c.write("GET /~myserver/settings.txt HTTP/1.1\n"); // Ask for settings.txt
c.write("Host: myserver.com\n\n"); // Identify
}
void serialEvent (Serial port) { // process inputs received from Arduino
String myString = port.readStringUntil(carriageReturn);
if (myString !=null){
String theString = trim(myString);
client = new Client(this, "www.myserver.com", 80);
client.write("GET /~myserver/switch.php?receivedValue=" + theString + " HTTP/1.1\n");
client.write("HOST: myserver.com\n\n");
println(myString);
myString="";
port.clear();
}
}
void draw() {
if (c.available() > 0) {
data = c.readString();
String[] p = splitTokens(data);
int myredintval = unhex(p[36]);
int mygreenintval = unhex(p[37]);
int myblueintval = unhex(p[38]);
char myredcharval = char(myredintval);
char mygreencharval = char(mygreenintval);
char mybluecharval = char(myblueintval);
println(myredcharval);
println(mygreencharval);
println(mybluecharval);
// send data to Arduino
port.write('A');// send A to start receiving
delay(10);
port.write(myredcharval); // send red
delay(10);
port.write(mygreencharval); // send green
delay(10);
port.write(mybluecharval); // send blue
}
delay(200); // Delay of 200 milliseconds
println(millis());
//Get fresh data from settings.txt
c.write("GET /~myserver/settings.txt HTTP/1.1\n"); // Use the HTTP "GET" command to ask for a Web page
c.write("Host: myserver.com\n\n"); // Identify
}