matthewborgcarr
Posts: 9
Joined: Sat Apr 26, 2014 5:30 pm

Control the Pi using java run on a pc.

Sat Apr 26, 2014 6:06 pm

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.

User avatar
DougieLawson
Posts: 39303
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Control the Pi using java run on a pc.

Sat Apr 26, 2014 11:20 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

matthewborgcarr
Posts: 9
Joined: Sat Apr 26, 2014 5:30 pm

Re: Control the Pi using java run on a pc.

Fri Oct 03, 2014 10:23 pm

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

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Control the Pi using java run on a pc.

Fri Oct 03, 2014 10:32 pm

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.

matthewborgcarr
Posts: 9
Joined: Sat Apr 26, 2014 5:30 pm

Re: Control the Pi using java run on a pc.

Fri Oct 03, 2014 10:37 pm

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

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Control the Pi using java run on a pc.

Fri Oct 03, 2014 11:05 pm

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)
>)))'><'(((<

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Control the Pi using java run on a pc.

Fri Oct 03, 2014 11:07 pm

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.

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Control the Pi using java run on a pc.

Sat Oct 04, 2014 6:41 am

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
>)))'><'(((<

matthewborgcarr
Posts: 9
Joined: Sat Apr 26, 2014 5:30 pm

Re: Control the Pi using java run on a pc.

Thu Oct 09, 2014 8:59 pm

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

fruitoftheloom
Posts: 23549
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: Control the Pi using java run on a pc.

Thu Oct 09, 2014 9:13 pm

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+ ?
Rather than negativity think outside the box !
RPi 4B 4GB (SSD Boot) RaspiOS64 ARM64
Asus ChromeBox 3 Celeron is my other computer...

matthewborgcarr
Posts: 9
Joined: Sat Apr 26, 2014 5:30 pm

Re: Control the Pi using java run on a pc.

Thu Oct 09, 2014 9:22 pm

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

matthewborgcarr
Posts: 9
Joined: Sat Apr 26, 2014 5:30 pm

Re: Control the Pi using java run on a pc.

Fri Oct 10, 2014 7:28 pm

@ fruitoftheloom

Is it still possible to do what I have in mind with the raspberry pi even though it is not the b+?

Thanks

fruitoftheloom
Posts: 23549
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: Control the Pi using java run on a pc.

Fri Oct 10, 2014 7:54 pm

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
Rather than negativity think outside the box !
RPi 4B 4GB (SSD Boot) RaspiOS64 ARM64
Asus ChromeBox 3 Celeron is my other computer...

matthewborgcarr
Posts: 9
Joined: Sat Apr 26, 2014 5:30 pm

Re: Control the Pi using java run on a pc.

Fri Oct 10, 2014 8:27 pm

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

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Control the Pi using java run on a pc.

Fri Oct 10, 2014 9:07 pm

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.

matthewborgcarr
Posts: 9
Joined: Sat Apr 26, 2014 5:30 pm

Re: Control the Pi using java run on a pc.

Fri Oct 10, 2014 9:13 pm

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

User avatar
DougieLawson
Posts: 39303
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Control the Pi using java run on a pc.

Fri Oct 10, 2014 9:13 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Control the Pi using java run on a pc.

Fri Oct 10, 2014 9:33 pm

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.]

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Control the Pi using java run on a pc.

Fri Oct 10, 2014 9:46 pm

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.
>)))'><'(((<

matthewborgcarr
Posts: 9
Joined: Sat Apr 26, 2014 5:30 pm

Re: Control the Pi using java run on a pc.

Sat Nov 15, 2014 11:19 pm

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

Return to “Java”