10.20.2013

Java Socket Server/Client on Raspberry Pi

In order to connect to the Raspberry Pi, we've been using either a secure shell login (ssh) or a virtual desktop (VNC). These capabilities allow to us download software and test different code vital to our project. What these do not allow is instant communication through a Java Program. Socket servers/clients will allow us to open up different ports on the Raspberry Pi and wirelessly stream data through them. For example we could open up a port that wirelessly writes numbers to a Pi-Blaster file, therefore controlling our motors.

A socket server program listens to a port. When a client tries to connect the server either allows it in or denies it access to the port. If allowed access the client and server can now communicate using UDP/TCP communication protocols.

Below is a quick explanation and example of using Socket Severs/Clients on the Raspberry Pi for PWM control.

Sockets Running Pi-Blaster:

Sending strings back and forth between a client and server uses simple InputStream()/OutputStream() commands. We needed to be able to send strings and then have them echoed to /dev/pi-blaster (command line virtualization). We had some trouble echoing commands using 'bash' from a Java program. We eventually figured it out using a Java program and Shell Script on the Raspberry Pi.

We first sent PWM values to a file called motorcontrol.sh and then use Runtime() in Java to emulate a 'bash'  command. The following is the Shell Script which 'echos' are the values we give it to /dev/pi-blaster.

The code for the Server and Client can be found on our Downloads page under Java - Socket Server.

Edit the IP address in the Client.java and Server.java files to match you specific IP requirements.

After exporting the server into a runnable .jar file copy it to the Raspberry Pi and then run it using this command:


Next run the Client on your laptop (we are just using Eclipse for now). It should look something like this:




and you have a Client/Server connection to the Raspberry Pi to run Pi-Blaster! Stay updated for instructions on how to use an XBox Controller to send values (instead of a JOptionPane).


1 comment: