Page 1 of 1
Control the Pi using java run on a pc.
Posted: Sat Apr 26, 2014 6:06 pm
by matthewborgcarr
Hi, I wanted to ask whether it is possible to control the raspberry pi using a java program being run on a pc with either windows or Mac Os.
My intention is to connect several relays to the Pi and control them with Java.
The interface between the Pi and the laptop has to be a usb cable on the laptop's side.
With regards to the Pi's side I am not exactly sure what the interface has to be, and if possible I would like to obtain some tips as well.
Thanks.
Re: Control the Pi using java run on a pc.
Posted: Sat Apr 26, 2014 11:20 pm
by DougieLawson
matthewborgcarr wrote:
The interface between the Pi and the laptop has to be a usb cable on the laptop's side.
You can't do that with a RPi. The RPi runs as a USB master, not a USB slave.
You'll have more success using WiFi or Ethernet with a simple bit of socket programming (or something funky like MQTT). You could do it with a USB to 3V3 RS232 and connect to the TX/RX pins on the GPIO and using a serial connection from your laptop.
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 03, 2014 10:23 pm
by matthewborgcarr
Thank you for your reply.
I have done a very intensive research with no success.
I can't figure out how I am going to program or instruct the Pi to control these relays using java running on another device....
Has anyone done something similar to this?
Thanks
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 03, 2014 10:32 pm
by joan
I don't fully understand what you are trying to achieve.
However you can control the Pi's gpios using the
pigpio socket interface.
That will let you run your Java on Windows/Macs and communicate via TCP/IP with the Pi to control the gpios.
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 03, 2014 10:37 pm
by matthewborgcarr
That is helpful.
I have also looked for "FTDI USB adaptor" which in certain forums are highly recommended.
These use RXTX communication terminology which converts to serial communication.
Mat
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 03, 2014 11:05 pm
by aTao
What about USB -> gigabit ethernet on the PC as per
http://www.dabs.com/products/startech-c ... ce%20Cards
Then, well, theres gazillions of options, most common I think is RPi webserver -> python -> gpio.
Or you could easily use java -> shh -> RPi shell
Or pc java -> RPi home brew java server (java reflection here maybe)
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 03, 2014 11:07 pm
by Douglas6
If you just need GPIO on a Windows PC, there is for example
Flyfish Technologies' FF32 chip. It's got reference code for Visual Basic; Java might be a bit more work.
Re: Control the Pi using java run on a pc.
Posted: Sat Oct 04, 2014 6:41 am
by aTao
Douglas6 wrote:If you just need GPIO on a Windows PC, there is for example
Flyfish Technologies' FF32 chip. It's got reference code for Visual Basic; Java might be a bit more work.
There is also a C demo, combine that with JNI and theres your java. Unless of course the RPi is doing more than just being GPIO
Re: Control the Pi using java run on a pc.
Posted: Thu Oct 09, 2014 8:59 pm
by matthewborgcarr
Hi All,
Ok so now I bought all the hardware necessary to control the Pi using Java from my macbook.
I now need to know the configuration the Pi needs to be in for this to work.
By configuration I mean... Does the Pi need to have an OS running, if yes is there a particular OS which works best for my needs?
Thanks people. Your help is much appreciated.
Matthew
Re: Control the Pi using java run on a pc.
Posted: Thu Oct 09, 2014 9:13 pm
by fruitoftheloom
matthewborgcarr wrote:Hi All,
Ok so now I bought all the hardware necessary to control the Pi using Java from my macbook.
I now need to know the configuration the Pi needs to be in for this to work.
By configuration I mean... Does the Pi need to have an OS running, if yes is there a particular OS which works best for my needs?
Thanks people. Your help is much appreciated.
Matthew
Without a SD Card with the Boot Files the Pi is a paperweight, it has no BIOS.
Raspbian Wheezy is the most popular OS and can be run with or without a Desktop GUI
May we assume you have a RPi B+ ?
Re: Control the Pi using java run on a pc.
Posted: Thu Oct 09, 2014 9:22 pm
by matthewborgcarr
Hi and thanks for the reply.
I am using a 512MB Raspberry Pi Model B Revision 2 Computer - Rasberry PCB BOARD 2.0.
(I have started my own blog on this topic -
http://piprojectsblog.wordpress.com)
Mat
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 10, 2014 7:28 pm
by matthewborgcarr
@ fruitoftheloom
Is it still possible to do what I have in mind with the raspberry pi even though it is not the b+?
Thanks
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 10, 2014 7:54 pm
by fruitoftheloom
matthewborgcarr wrote:@ fruitoftheloom
Is it still possible to do what I have in mind with the raspberry pi even though it is not the b+?
Thanks
Yes
The B+ is just an evolution of the B and therefore the CPU, GPU, Memory are the same
http://www.raspberrypi.org/introducing- ... del-b-plus
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 10, 2014 8:27 pm
by matthewborgcarr
Ok that is a relief. I though I had to incur further expenses... hehe
So with the OS installed on the Pi, I just need to switch it on, leave it on the "desktop screen" and run the java program from my laptop and the GPIO controls the relays?
Thanks
Mat
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 10, 2014 9:07 pm
by Douglas6
Um, no. You'll need to connect the relays to the Pi of course. You'll also need a program running on the Pi that reads and interprets the messages being sent from the PC, and turns on and off the appropriate GPIO pins in response. That program on the Pi can be done in Java, or, perhaps more easily and with more example code, in Python. I think this is going to be a great learning experience for you.
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 10, 2014 9:13 pm
by matthewborgcarr
I look forward to start working on my project.
I am writing a blog detailing the progress of this project on:
http://piprojectsblog.wordpress.com.
Would it be possible if you direct me to the right direction on where I should look to get the right information for setting up my devices please?
Thanks.
Mat
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 10, 2014 9:13 pm
by DougieLawson
Douglas6 wrote:That program on the Pi can be done in Java ...
Nothing wrong with PI4J
http://pi4j.com/ which is easy to install and easy to use for a Java programmer.
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 10, 2014 9:33 pm
by Douglas6
DougieLawson wrote:Nothing wrong with PI4J
http://pi4j.com/ which is easy to install and easy to use for a Java programmer.
Yup, if you're programming in Java on the Pi, that's the library to use.
[Edit: And I just now noticed that it even has support for serial communications built-in.]
Re: Control the Pi using java run on a pc.
Posted: Fri Oct 10, 2014 9:46 pm
by aTao
Looks like you are heading for the Java PC -> ethernet cable RPi Java route.
Early steps to take are:
get communication from the PC to the RPi and the other way you at least need to be able to ping.
forget the desktop on the RPi, get used to ssh via the ethernet connection.
have a look at the Java classes for opening and monitoring ports (its about 15 years since I wrote a multiuser VR server in Java, but I do remember the port listening bit was easy)
maybe look at the Java Reflection system, makes things feel more like one program rather than bits running on separate machines.
and as has been mentioned pi4j, although a "roll your own" using the file system access to GPIO is very simple and quite fast enough for relay control, all you do is write a few characters to some soecific files and the GPIO lines do what you want.
Re: Control the Pi using java run on a pc.
Posted: Sat Nov 15, 2014 11:19 pm
by matthewborgcarr
Dear All
I finally managed to get all the equipment together and connected all the stuff mentioned in this blog.
On my laptop using Java I am running the following program:
Code: Select all
package twowayserialcomm;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static javax.management.Query.gt;
public class TwoWaySerialComm {
void connect( String portName ) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier( portName );
if( portIdentifier.isCurrentlyOwned() ) {
System.out.println( "Error: Port is currently in use" );
} else {
int timeout = 2000;
CommPort commPort = portIdentifier.open( this.getClass().getName(), timeout );
if( commPort instanceof SerialPort ) {
SerialPort serialPort = ( SerialPort )commPort;
serialPort.setSerialPortParams( 57600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE );
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
( new Thread( new SerialReader( in ) ) ).start();
( new Thread( new SerialWriter( out ) ) ).start();
} else {
System.out.println( "Error: Only serial ports are handled by this example." );
}
}
}
public static class SerialReader implements Runnable {
InputStream in;
public SerialReader( InputStream in ) {
this.in = in;
}
@Override
public void run() {
byte[] buffer = new byte[ 1024 ];
int len = -1;
try {
while( ( len = this.in.read( buffer ) ) > -1 ) {
System.out.print( new String( buffer, 0, len ) );
}
} catch( IOException e ) {
e.printStackTrace();
}
}
}
public static class SerialWriter implements Runnable {
OutputStream out;
public SerialWriter( OutputStream out ) {
this.out = out;
}
public void run() {
try {
int c = 0;
while( ( c = System.in.read() ) > -1 ) {
this.out.write( c );
}
} catch( IOException e ) {
}
}
}
public static void main( String[] args ) {
try {
( new TwoWaySerialComm() ).connect( "/dev/ttyUSB0" );
} catch( Exception e ) {
}
}
}
And I am getting the following error in the CMD:
Code: Select all
WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.2pre2
BUILD SUCCESSFUL (total time: 2 seconds)
Could anyone let me know what needs to be done to solve this issue?
Thanks
Mat