aarushi_mittal
Posts: 40
Joined: Wed Jul 05, 2017 10:16 am
Location: Delhi, India

Use sensor data from Serial communication to pi3 in java app

Tue Jul 11, 2017 12:07 pm

Hi .
I am sending sensor data from arduino to raspberry pi , using a usb.
I want to catch that readings into my java program .

How can it be done ?

I am using a raspberry pi 3 , and it already has enable_uart=1.
I am following this code : http://pi4j.com/example/serial.html

Is there a problem with the code or are the settings for serial communication not done properly on my pi ?
The code only outputs console.print lines , but not the serial.write lines.

Kindly help :?
Aarushi

User avatar
topguy
Posts: 6164
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Use sensor data from Serial communication to pi3 in java

Wed Jul 12, 2017 12:32 pm

A couple of inconsistencies:
- "I am sending sensor data from arduino to raspberry pi , using a usb." - If you only have an USB cable between the Arduino and you pi you can ignore anything related to setting up or disabling the console on the UART on the GPIO pins.
- ( .. also If you are using an up to date Raspbian Jessie then the name of the UART device is no longer "/dev/ttyAMA0", but this information is also not relevant unless you use these pins )

Figure out what the correct name of the serial device you get when you connect the Arduino. There is probably many Arduino users here that can tell you what to expect, but my general method is to:
- boot the Pi.
- then plug in the Arduino ( or other USB device )
- look at the output of "dmesg" command, the last lines will contain information from the kernel about the device.

mattlewis
Posts: 113
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Use sensor data from Serial communication to pi3 in java

Thu Jul 13, 2017 9:19 pm

You could try use Firmata4j to control the sensors directly from Java on the Pi (connected to the Arduino via USB). Parsing serial output sounds messy.
Matt
http://www.diozero.com/

aarushi_mittal
Posts: 40
Joined: Wed Jul 05, 2017 10:16 am
Location: Delhi, India

Re: Use sensor data from Serial communication to pi3 in java

Sun Jul 16, 2017 8:20 am

mattlewis wrote:You could try use Firmata4j to control the sensors directly from Java on the Pi (connected to the Arduino via USB). Parsing serial output sounds messy.
Matt
Hi Matt,

I seem to understand your approach now.
Can you share some tutorials. I cannot find any for JAva on Pi using firmata

Thanks
Aarushi

mattlewis
Posts: 113
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Use sensor data from Serial communication to pi3 in java

Sun Jul 16, 2017 6:24 pm

What sensors / devices are you controlling?

My diozero library hides the complexity of device access. To run the simple LED for example, you just reference the firmata4j provider JAR file at runtime, e.g.:
To run the LED sample app with the in-built provider:

Code: Select all

java -cp tinylog-1.2.jar:diozero-core-$DIOZERO_VERSION.jar:diozero-sampleapps-$DIOZERO_VERSION.jar com.diozero.sampleapps.LEDTest 12
To run with the pigpio provider:

Code: Select all

sudo java -cp tinylog-1.2.jar:diozero-core-$DIOZERO_VERSION.jar:diozero-sampleapps-$DIOZERO_VERSION.jar:diozero-provider-pigpio-$DIOZERO_VERSION.jar:pigpioj-java-1.0.1.jar com.diozero.sampleapps.LEDTest 12
To run with Firmata4j:

Code: Select all

java -cp tinylog-1.2.jar:diozero-core-$DIOZERO_VERSION.jar:diozero-sampleapps-$DIOZERO_VERSION.jar:diozero-provider-firmata4j-$DIOZERO_VERSION.jar:firmata4j-2.3.5.jar:jssc-2.8.0.jar com.diozero.sampleapps.LEDTest 12
diozero comes with a library supported devices. Firmata4j doesn't support SPI at the moment unfortunately.

Regards,
Matt
http://www.diozero.com/

aarushi_mittal
Posts: 40
Joined: Wed Jul 05, 2017 10:16 am
Location: Delhi, India

Re: Use sensor data from Serial communication to pi3 in java

Mon Jul 17, 2017 12:21 pm

mattlewis wrote:What sensors / devices are you controlling?

My diozero library hides the complexity of device access. To run the simple LED for example, you just reference the firmata4j provider JAR file at runtime, e.g.:
To run the LED sample app with the in-built provider:

Code: Select all

java -cp tinylog-1.2.jar:diozero-core-$DIOZERO_VERSION.jar:diozero-sampleapps-$DIOZERO_VERSION.jar com.diozero.sampleapps.LEDTest 12
To run with the pigpio provider:

Code: Select all

sudo java -cp tinylog-1.2.jar:diozero-core-$DIOZERO_VERSION.jar:diozero-sampleapps-$DIOZERO_VERSION.jar:diozero-provider-pigpio-$DIOZERO_VERSION.jar:pigpioj-java-1.0.1.jar com.diozero.sampleapps.LEDTest 12
To run with Firmata4j:

Code: Select all

java -cp tinylog-1.2.jar:diozero-core-$DIOZERO_VERSION.jar:diozero-sampleapps-$DIOZERO_VERSION.jar:diozero-provider-firmata4j-$DIOZERO_VERSION.jar:firmata4j-2.3.5.jar:jssc-2.8.0.jar com.diozero.sampleapps.LEDTest 12
diozero comes with a library supported devices. Firmata4j doesn't support SPI at the moment unfortunately.

Regards,
Matt
Hi Matt,
Thanks for the quick response.
I figured out the mistake just yesterday.
I had to replace /dev/ttyAMA0 with /dev/ttyACM0

and my seed moisture sensor was sending data to raspberry pi java application via my arduino unoe.
P.S. : i was using a raspberry pi 2

But I would now start experimenting with Firmata to see how I can use it for future PoCs.

Thanks for all the help,
Aarushi :)

Return to “Advanced users”