Raspberry PI reads data from Arduino, in Java
As the Arduino is a real-time processor, and as the Raspberry PI is not, it could make some sense to have both those guys connected to each other.
In this example, we will have the Arduino to read analog data from a light-sensor (photo resistor), and send those values
to the Raspberry PI, through a USB Serial port.
The Arduino sketch
As you can see in the
* Reads a light-resistor (pin A0).
* See circuit in the book, p63.
* "Getting started with Arduino"
* Generates NMEA-like messages, emitted on the Serial port
* A Raspberry PI is at the other end of the serial cable.
int val = 0,
previous = 0;
const String PREFIX = "OS"; // Device Prefix
const String ID = "MSG"; // Sentence ID
val = analogRead(A0);
if (val != previous)
String payload = "LR," + String(val); // LR: Light Resistor
String nmea = generateNMEAString(payload, PREFIX, ID);
previous = val;
int checksum(String s)
int cs = 0;
int len = s.length() + 1; // Yes, +1
for (int i=0; i<len; i++)
cs ^= ca[i]; // XOR
String generateNMEAString(String payload, String prefix, String id)
String nmea = "";
if (prefix.length() != 2)
return nmea; // ("Bad prefix [" + prefix + "], must be 2 character long.");
if (id.length() != 3)
return nmea; // ("Bad ID [" + id + "], must be 3 character long.");
nmea = prefix + id + "," + payload;
int cs = checksum(nmea);
String cks = String(cs, HEX);
if (cks.length() < 2) // lpad '0'
cks = "0" + cks;
nmea += ("*" + cks); // *00
return "$" + nmea; // Prefixed with $
loop method, the photo resistor value is read from pin
A0 every 250ms, and sent on the serial port if the value is different from the one previously read, in an NMEA-like format.
Validating the received data
In order to make sure there is no data alteration between the Arduino and the Raspberry PI, we use an NMEA-like protocol, with a checksum.
The idea is not to be burdened with invalid sentences, that would have been damaged during the transport through the serial port.
This way, the receiver of the data can tell if they are valid or not, before parsing them. In the sketch above, this is what the
generateNMEAString are for.
|| | | |
|| | | Checksum
|| | Message
|| Sentence ID
- All NMEA sentences begin with a
- The device prefix is 2 character long.
OS for Oliv Soft
- The sentence ID is 3 character long.
MSG for message
- The message is a comma-seperated list of character data. Here
LR stands for Light Resistor, and
178 is the current value. The structure of the message is completely arbitrary, you can come up with your own.
- The checksum - preceeded with a
* - is the two-character hexadecimal value of a bitwise exclusive 'or' (XOR) on all the characters of the string beginning with the device prefix (
OSMSG,LR,178 in this case). See the
checksum method in the Arduino sketch above.
The Java code on the Raspberry PI
It's all about resading the right serial port. Here, it is
See in the script named
sudo java $JAVA_OPTIONS -cp $CP arduino.raspberrypi.SerialReader
main method of the
arduino.raspberrypi.SerialReader.java class opens the serial port, and creates a serial listener that will take care of displaying the valid sentences received from trhe Arduino.
Oops! Invalid String [R,27*5E
Oops! Invalid String [SG,LR,27*5E
Oops! Invalid String [G,LR,27*5E
Oops! Invalid String [G,LR,28*51
The NMEA sentences arrive one by one, they are parsed if valid (notice the invalid ones) to extract the value we are interested in.
The only wiring on the the Raspberry PI is the USB connection with the Arduino.
On the Arduino:
- One pin of the photo resistor is connected to the Arduino
- The other pin of the photo resistor is connected to the Arduino
A0 analog pin
GND pin of the Arduino is connected to the previous pin of the photo resistor through a 10kΩ resistor.
Oliv did it