Created
November 11, 2012 18:55
-
-
Save jean-robert/4055869 to your computer and use it in GitHub Desktop.
Temperature recorder with Arduino/Java/R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment