Halloween: Season for Java Embedded Internet of Spooky Things (IoST) (Part 4)
- by hinkmond
And now here's the Java code that you'll need to read your ghost sensor on your Raspberry Pi
The general idea is that you are using Java code to access the GPIO pin on your Raspberry Pi where the ghost sensor (JFET trasistor) detects minute changes in the electromagnetic field near the Raspberry Pi and will change the GPIO pin to high (+3 volts) when something is detected, otherwise there is no value (ground).
Here's that Java code:
try {
/*** Init GPIO port(s) for input ***/
// Open file handles to GPIO port unexport and export controls
FileWriter unexportFile =
new FileWriter("/sys/class/gpio/unexport");
FileWriter exportFile =
new FileWriter("/sys/class/gpio/export");
for (String gpioChannel : GpioChannels) {
System.out.println(gpioChannel);
// Reset the port
File exportFileCheck = new
File("/sys/class/gpio/gpio"+gpioChannel);
if (exportFileCheck.exists()) {
unexportFile.write(gpioChannel);
unexportFile.flush();
}
// Set the port for use
exportFile.write(gpioChannel);
exportFile.flush();
// Open file handle to input/output direction control of port
FileWriter directionFile =
new FileWriter("/sys/class/gpio/gpio" + gpioChannel +
"/direction");
// Set port for input
directionFile.write(GPIO_IN);
}
/*** Read data from each GPIO port ***/
RandomAccessFile[] raf = new RandomAccessFile[GpioChannels.length];
int sleepPeriod = 10;
final int MAXBUF = 256;
byte[] inBytes = new byte[MAXBUF];
String inLine;
int zeroCounter = 0;
// Get current timestamp with Calendar()
Calendar cal;
DateFormat dateFormat =
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
String dateStr;
// Open RandomAccessFile handle to each GPIO port
for (int channum=0; channum
And, then we just load up our Java SE Embedded app, place each Raspberry Pi with a ghost sensor attached in strategic locations around our Santa Clara office (which apparently is very haunted by ghosts from the Agnews Insane Asylum 1906 earthquake), and watch our analytics for any ghosts.
Easy peazy.
See the previous posts for the full series on the steps to this cool demo:
Halloween: Season for Java Embedded Internet of Spooky Things (IoST) (Part 1)
Halloween: Season for Java Embedded Internet of Spooky Things (IoST) (Part 2)
Halloween: Season for Java Embedded Internet of Spooky Things (IoST) (Part 3)
Halloween: Season for Java Embedded Internet of Spooky Things (IoST) (Part 4)
Hinkmond