|
ClassWork / SerialServer
Processing Code for Text Chat using Socket Connections
import processing.core.PApplet;
import processing.core.PFont;
import processing.net.Client;
import processing.net.Server;
import processing.serial.Serial;
public class Chat extends PApplet {
Server myServer;
int endOfMessageChar = 10;
Client myClient;
int socketPort = 10002;
String ip = "128.122.151.100";
boolean serverRole = true;
String displayText = "Nothing Yet";
static String[] args;
String testString = "";
static public void main(String _args[]) {
args = _args;
PApplet.main(new String[] { "Chat" });
}
public void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Print a list of the serial ports, for debugging purposes to find out what your ports are called:
// println(Serial.list());
getParams();
if (serverRole) {
myServer = new Server(this, socketPort);
println("Starting a server at port " + socketPort);
} else {
myClient = new Client(this, ip, socketPort);
println("Connecting as client at " + socketPort + " on " + ip);
}
PFont myFont = createFont("Arial", 18);
textFont(myFont);
}
public void draw() {
// listen to client, should be able to do this with ClientEvent but this seems not to work with Clients created by the server
if (myClient != null) {
String whatClientSaid = myClient.readStringUntil(endOfMessageChar);
if (whatClientSaid != null) {
displayText = "Got: " + whatClientSaid;
}
}
background(0);
if (testString.equals("")) {
text(displayText, 20, 20);
} else {
text(testString + " Press Enter To Send", 20, 20);
}
}
public void serverEvent(Server someServer, Client _newClient) {
myClient = _newClient;
println("We have a new client: " + _newClient.ip());
}
void tellSocket(String _whatSerialSaid) {
displayText = "Told Socket " + _whatSerialSaid;
if (myClient != null) myClient.write(_whatSerialSaid + "\n");
}
public void keyPressed() {
if (key == ENTER || key == RETURN) {
tellSocket(testString);
testString = "";
} else {
testString = testString + key;
}
}
void getParams() {
if (args != null) { // running as an application
if (args.length > 0) socketPort = Integer.parseInt(args[0]);
if (args.length > 1) {
ip = args[1];
serverRole = false;
}
} else { // running as an applet
String isItThere = getParameter("socketPort");
if (isItThere != null) socketPort = Integer.parseInt(isItThere);
isItThere = getParameter("ip");
if (isItThere != null) ip = isItThere;
}
}
}
Processing Code for Tele White Board
import processing.core.PApplet;
import processing.core.PFont;
import processing.net.Client;
import processing.net.Server;
import processing.serial.Serial;
public class TeleDraw extends PApplet {
Server myServer;
int endOfMessageChar = 10;
Client myClient;
int socketPort = 10002;
String ip = "128.122.151.100";
boolean serverRole = true;
static String[] args;
static public void main(String _args[]) {
args = _args;
PApplet.main(new String[] { "TeleDraw" });
}
public void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Print a list of the serial ports, for debugging purposes to find out what your ports are called:
// println(Serial.list());
getParams();
if (serverRole) {
myServer = new Server(this, socketPort);
println("Starting a server at port " + socketPort);
} else {
myClient = new Client(this, ip, socketPort);
println("Connecting as client at " + socketPort + " on " + ip);
}
}
public void draw() {
// listen to client, should be able to do this with ClientEvent but this seems not to work with Clients created by the server
if (myClient != null) {
String whatClientSaid = myClient.readStringUntil(endOfMessageChar);
if (whatClientSaid != null) {
println("What " + serverRole + " " + whatClientSaid);
String[] parts = whatClientSaid.split(",");
if (parts.length > 1) {
fill(0);
ellipse(Integer.parseInt(parts[0].trim()), Integer.parseInt(parts[1].trim()), 2, 2);
}
}
}
}
public void serverEvent(Server someServer, Client _newClient) {
myClient = _newClient;
println("We have a new client: " + _newClient.ip());
}
void tellSocket(String _whatSerialSaid) {
if (myClient != null) myClient.write(_whatSerialSaid + "\n");
}
public void mouseDragged() {
println(String.valueOf(mouseX) + "," + String.valueOf(mouseY));
tellSocket(String.valueOf(mouseX) + "," + String.valueOf(mouseY));
}
void getParams() {
if (args != null) { // running as an application
if (args.length > 0) socketPort = Integer.parseInt(args[0]);
if (args.length > 1) {
ip = args[1];
serverRole = false;
}
} else { // running as an applet
String isItThere = getParameter("socketPort");
if (isItThere != null) socketPort = Integer.parseInt(isItThere);
isItThere = getParameter("ip");
if (isItThere != null) ip = isItThere;
}
}
}
Arduino and Processing Code for Relaying Sensor Data Across the Internet
int ledPin = 3; // LED connected to digital pin 13
int photocell = 0;
boolean lastPhotocellCondition;
boolean pinCondition = false;
int photoThreshold = 450;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
beginSerial(9600);
}
void loop()
{
photocell = analogRead(4);
//Serial.println(photocell,DEC);
boolean now = (photocell > photoThreshold);
if (now != lastPhotocellCondition) {
if (now == true){
Serial.print(65,BYTE);
Serial.print(10,BYTE);
}
else{
Serial.print(66,BYTE);
Serial.print(10,BYTE);
}
lastPhotocellCondition = now;
}
if (Serial.available() > 1){
int condition = Serial.read();
Serial.read(); //burn the delimiter
if (condition == 65){
digitalWrite(ledPin, HIGH); // sets the LED on
}
else{
digitalWrite(ledPin, LOW); // sets the LED on
}
}
}
import processing.core.PApplet;
import processing.core.PFont;
import processing.net.Client;
import processing.net.Server;
import processing.serial.Serial;
public class SerialServerApp extends PApplet {
Serial serialPort;
Server myServer;
int endOfMessageChar = 10;
Client myClient;
String serialPortName;
int socketPort = 10002;
String ip = "128.122.151.100";
boolean serverRole = true;
String displayText = "Nothing Yet";
static String[] args;
String testString = "";
static public void main(String _args[]) {
args = _args;
PApplet.main(new String[] { "SerialServerApp" });
}
public void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Print a list of the serial ports, for debugging purposes to find out what your ports are called:
// println(Serial.list());
getParams();
if (Serial.list()[0] == null) serialPortName = Serial.list()[0];
serialPort = new Serial(this, serialPortName, 9600); // or you can just specify it
println("Connecting to serial port " + serialPortName);
if (serverRole) {
myServer = new Server(this, socketPort);
println("Starting a server at port " + socketPort);
} else {
myClient = new Client(this, ip, socketPort);
println("Connecting as client at " + socketPort + " on " + ip);
}
PFont myFont = createFont("Arial", 18);
textFont(myFont);
}
public void draw() {
// listen to client, should be able to do this with ClientEvent but this seems not to work with Clients created by the server
if (myClient != null) {
String whatClientSaid = myClient.readStringUntil(endOfMessageChar);
if (whatClientSaid != null) {
whatClientSaid = whatClientSaid.trim();
tellSerial(whatClientSaid);
}
}
background(0);
if (testString.equals("")) {
text(displayText, 20, 20);
} else {
text(testString + " Press Enter To Send", 20, 20);
}
}
public void serverEvent(Server someServer, Client _newClient) {
myClient = _newClient;
println("We have a new client: " + _newClient.ip());
}
public void serialEvent(Serial port) {
String whatSerialSaid = serialPort.readStringUntil(endOfMessageChar); // make sure you return (Ascii 13) at the end of your transmission
//println("Serial " + whatSerialSaid);
if (whatSerialSaid != null) {
whatSerialSaid = whatSerialSaid.trim();
tellSocket(whatSerialSaid);
}
}
void tellSocket(String _whatSerialSaid) {
displayText = "Told Socket " + _whatSerialSaid;
if (myClient != null) myClient.write(_whatSerialSaid + "\n");
}
void tellSerial(String _whatSocketSaid) {
displayText = "Told Serial " + _whatSocketSaid;
serialPort.write(_whatSocketSaid + "\n");
}
public void keyPressed() {
if (key == ENTER || key == RETURN) {
tellSocket(testString);
testString = "";
} else {
testString = testString + key;
}
}
void getParams() {
if (args != null) { // running as an application
if (args.length > 0) serialPortName = args[0];
if (args.length > 1) socketPort = Integer.parseInt(args[1]);
if (args.length > 2) {
ip = args[2];
serverRole = false;
}
} else { // running as an applet
String isItThere = getParameter("serialPort");
if (isItThere != null) serialPortName = isItThere;
isItThere = getParameter("socketPort");
if (isItThere != null) socketPort = Integer.parseInt(isItThere);
isItThere = getParameter("ip");
if (isItThere != null) ip = isItThere;
}
}
}
|