Arduino.RaspberryPIAs 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.
Description:
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.
/*
* 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.
*/
void setup()
{
Serial.begin(9600);
}
int val = 0,
previous = 0;
const String PREFIX = "OS"; // Device Prefix
const String ID = "MSG"; // Sentence ID
void loop()
{
val = analogRead(A0);
if (val != previous)
{
String payload = "LR," + String(val); // LR: Light Resistor
String nmea = generateNMEAString(payload, PREFIX, ID);
Serial.println(nmea);
}
previous = val;
delay(250);
}
int checksum(String s)
{
int cs = 0;
int len = s.length() + 1; // Yes, +1
char ca[len];
s.toCharArray(ca, len);
for (int i=0; i<len; i++)
cs ^= ca[i]; // XOR
return cs;
}
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);
cks.toUpperCase();
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.
checksum and generateNMEAString are for.
$OSMSG,LR,178*65
|| | | |
|| | | Checksum
|| | Message
|| Sentence ID
|Device prefix
$
$OS for Oliv SoftMSG for messageLR 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.* - 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./dev/ttyACM0.
arduino:
JAVA_OPTIONS=""
JAVA_OPTIONS="-Dserial.port=/dev/ttyACM0"
sudo java $JAVA_OPTIONS -cp $CP arduino.raspberrypi.SerialReader
The 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.
$OSMSG,LR,35*5D
$OSMSG,LR,27*5E
$OSMSG,LR,31*59
$OSMSG,LR,27*5E
$OSMSG,LR,33*5B
$OSMSG,L]
Oops! Invalid String [R,27*5E
$OSMSG,LR,31*59
$OSMSG,LR,27*5E
$OSMSG,LR,32*5A
$OSMSG,LR,28*51
$OSMSG,LR,32*5A
$OSM]
Oops! Invalid String [SG,LR,27*5E
$OSMSG,LR,32*5A
$OSMSG,LR,28*51
$OSMSG,LR,32*5A
$OSMSG,LR,28*51
$OSMSG,LR,32*5A
$OSMS]
Oops! Invalid String [G,LR,27*5E
$OSMSG,LR,32*5A
$OSMSG,LR,28*51
$OSMSG,LR,32*5A
$OSMSG,LR,28*51
$OSMSG,LR,32*5A
$OSMS]
Oops! Invalid String [G,LR,28*51
$OSMSG,LR,32*5A
$OSMSG,LR,28*51
5V pinA0 analog pinGND pin of the Arduino is connected to the previous pin of the photo resistor through a 10kΩ resistor.