public

Temperature recorder with Arduino/Java/R

  • Download Gist
ArduinoTemperatureSketch.ino
Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Original code from the Arduino Experimentation Kit Example Code |
* CIRC-10 .: Temperature :. (TMP36 Temperature Sensor) |
* Amended to be used in Java/R
*/
 
//TMP36 Pin Variables
int temperaturePin = 0;
 
void setup()
{
Serial.begin(9600);
}
void loop() // run over and over again
{
float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor
temperature = (temperature - .5) * 100; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.print("T");
Serial.println(temperature); //printing the result
delay(1000); //waiting a second
}
 
float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}
SerialTemperature.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
import java.io.InputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
 
public class SerialTemperature implements SerialPortEventListener {
 
SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES[] = {
"/dev/ttyS32", // Linux
};
/** Buffered input stream from the port */
private InputStream input;
 
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;
 
private String temperatureBuffer;
 
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
 
// iterate through, looking for the port
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
 
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
 
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
 
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
 
// open the streams
input = serialPort.getInputStream();
 
// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
 
 
/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
 
/**
* This will be used by R to retrieve the temperature value
*/
public synchronized Float read() {
return Float.valueOf(temperatureBuffer.substring(1)).floatValue();
}
 
/**
* Handle an event on the serial port. Read the data and save it to the buffer
*/
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int available = input.available();
byte chunk[] = new byte[available];
input.read(chunk, 0, available);
 
String s = new String(chunk);
if(s.contains("T")) {
temperatureBuffer = s;
} else {
temperatureBuffer += s;
}
 
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
 
}
TemperatureRecorder.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
setwd('/data/R/ArduinoTemp/')
require(rJava)
require(ggplot2)
 
.jinit(classpath='SerialTemperature.jar')
ardJava <- .jnew('SerialTemperature')
 
.jcall(ardJava, returnSig='V', method='initialize')
 
tempCapture <- NULL
while(Sys.Date()<'2012-11-11') {
system('sleep 30')
try({
ans <- .jsimplify(.jcall(ardJava, returnSig='Ljava/lang/Float;', method='read'))
tempCapture <- rbind(tempCapture, data.frame(Time=Sys.time(), Temperature=ans))
print(ggplot(tempCapture) + geom_line(aes(x=Time, y=Temperature)) + theme_bw())
}, silent=T)
}
 
.jcall(ardJava, returnSig='V', method='close')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.