Hurray! i finished my WiFi rover! though be it inefficient....extremely inefficient, im glad to say, i have a working prototype. The basic layout is, i drive it using a Java program that i made...Heres where we encounter our first problem, im not great with networking IO's yet, and, it shows. Here is my Java code.
Java Code:
[b]Main.java[/b]
[code]
package arduinocontrol;
import javax.swing.JFrame;
class Main extends JFrame{
public void frameSetup(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Setup s = new Setup();
add(s);
s.run();
setSize(200, 150);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
m.frameSetup();
}
}
Setup.java
package arduinocontrol;
import java.awt.Color;
import java.io.*;
import java.util.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
public class Setup extends JPanel {
boolean rove = false;
boolean check = true;
boolean check1 = true;
boolean check2 = true;
boolean check3 = true;
public static final int PORT = 1000;
String serverName = "192.168.1.5";
InetAddress serverIP = null;
Socket clientSocket = null;
Scanner in = null;
PrintWriter out = null;
JLabel label = new JLabel("Ready to drive...");
public void run() {
System.out.println("Running");
setFocusable(true);
setBackground(Color.black);
label.setForeground(Color.green);
label.setLocation(getX()/2, getY()/2);
add(label);
//setBackground(Color.BLACK);
addKeyListener(new klAdapter());
}
public void goForword() {
System.out.print("Foword");
try {
serverIP = InetAddress.getByName(serverName);
clientSocket = new Socket(serverIP, PORT);
in = new Scanner(clientSocket.getInputStream());
out = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("connected.");
} catch (UnknownHostException e) {
System.out.println("not found.");
} catch (IOException e) {
System.out.println("error connecting.");
}
out.println("1");
try {
clientSocket.close();
System.out.println("Disconnected.");
} catch (IOException e) {
System.out.println("error disconnecting.");
}
}
public void goBackword() {
System.out.print("Backword");
try {
serverIP = InetAddress.getByName(serverName);
clientSocket = new Socket(serverIP, PORT);
in = new Scanner(clientSocket.getInputStream());
out = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("connected.");
} catch (UnknownHostException e) {
System.out.println("not found.");
} catch (IOException e) {
System.out.println("error connecting.");
}
out.println("2");
try {
clientSocket.close();
System.out.println("Disconnected.");
} catch (IOException e) {
System.out.println("error disconnecting.");
}
}
public void turnLeft() {
System.out.print("Left");
try {
serverIP = InetAddress.getByName(serverName);
clientSocket = new Socket(serverIP, PORT);
in = new Scanner(clientSocket.getInputStream());
out = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("connected.");
} catch (UnknownHostException e) {
System.out.println("not found.");
} catch (IOException e) {
System.out.println("error connecting.");
}
out.println("3");
try {
clientSocket.close();
System.out.println("Disconnected.");
} catch (IOException e) {
System.out.println("error disconnecting.");
}
}
public void turnRight() {
System.out.print("Right");
try {
serverIP = InetAddress.getByName(serverName);
clientSocket = new Socket(serverIP, PORT);
in = new Scanner(clientSocket.getInputStream());
out = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("connected.");
} catch (UnknownHostException e) {
System.out.println("not found.");
} catch (IOException e) {
System.out.println("error connecting.");
}
out.println("4");
try {
clientSocket.close();
System.out.println("Disconnected.");
} catch (IOException e) {
System.out.println("error disconnecting.");
}
}
public void Stopped() {
System.out.print("Stopped");
try {
serverIP = InetAddress.getByName(serverName);
clientSocket = new Socket(serverIP, PORT);
in = new Scanner(clientSocket.getInputStream());
out = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("connected.");
} catch (UnknownHostException e) {
System.out.println("not found.");
} catch (IOException e) {
System.out.println("error connecting.");
}
out.println("0");
try {
clientSocket.close();
System.out.println("Disconnected.");
} catch (IOException e) {
System.out.println("error disconnecting.");
}
}
public void roveStateON(){
try {
serverIP = InetAddress.getByName(serverName);
clientSocket = new Socket(serverIP, PORT);
in = new Scanner(clientSocket.getInputStream());
out = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("connected.");
} catch (UnknownHostException e) {
System.out.println("not found.");
} catch (IOException e) {
System.out.println("error connecting.");
}
out.println("6");
try {
clientSocket.close();
System.out.println("Disconnected.");
} catch (IOException e) {
System.out.println("error disconnecting.");
}
}
public void roveStateOFF(){
try {
serverIP = InetAddress.getByName(serverName);
clientSocket = new Socket(serverIP, PORT);
in = new Scanner(clientSocket.getInputStream());
out = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("connected.");
} catch (UnknownHostException e) {
System.out.println("not found.");
} catch (IOException e) {
System.out.println("error connecting.");
}
out.println("7");
try {
clientSocket.close();
System.out.println("Disconnected.");
} catch (IOException e) {
System.out.println("error disconnecting.");
}
}
private class klAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_UP) && check == false) {
label.setText("Stopped...");
Stopped();
check = true;
}
if ((key == KeyEvent.VK_DOWN) && check1 == false) {
label.setText("Stopped...");
Stopped();
check1 = true;
}
if ((key == KeyEvent.VK_LEFT) && check2 == false) {
label.setText("Stopped...");
Stopped();
check2 = true;
}
if ((key == KeyEvent.VK_RIGHT) && check3 == false) {
label.setText("Stopped...");
Stopped();
check3 = true;
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_SPACE)) {
if(rove == true){
rove = false;
roveStateOFF();
}else{
rove = true;
roveStateON();
}
}
if ((key == KeyEvent.VK_UP) && check == true) {
label.setText("Moving Forword!");
goForword();
check = false;
}
if ((key == KeyEvent.VK_DOWN) && check1 == true) {
label.setText("Moving Backword!");
goBackword();
check1 = false;
}
if ((key == KeyEvent.VK_LEFT) && check2 == true) {
label.setText("Turning Left!");
turnLeft();
check2 = false;
}
if ((key == KeyEvent.VK_RIGHT) && check3 == true) {
label.setText("Turning Right!");
turnRight();
check3 = false;
}
}
}
}
I need to improve that...ALOT! haha, help would be appreciated. I will post the Arduino code under this one!