Halloween: Season for Java Embedded Internet of Spooky Things (IoST) (Part 4)
Posted
by hinkmond
on Oracle Blogs
See other posts from Oracle Blogs
or by hinkmond
Published on Sat, 26 Oct 2013 00:32:54 +0000
Indexed on
2013/10/26
4:02 UTC
Read the original article
Hit count: 291
/Java Embedded
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 < raf.length; channum++) { raf[channum] = new RandomAccessFile("/sys/class/gpio/gpio" + GpioChannels[channum] + "/value", "r"); } // Loop forever while (true) { // Get current timestamp for latest event cal = Calendar.getInstance(); dateStr = dateFormat.format(cal.getTime()); // Use RandomAccessFile handle to read in GPIO port value for (int channum=0; channum < raf.length; channum++) { // Reset file seek pointer to read latest value of GPIO port raf[channum].seek(0); raf[channum].read(inBytes); inLine = new String(inBytes); // Check if any value was read if (inLine != null) { // Compress 0 values so we don't see too many // unimportant lines if (inLine.startsWith("0")) { if (zeroCounter < 1000) { zeroCounter++; } else { System.out.print(dateStr + ": " + inLine); zeroCounter = 0; } } else { // Else, specially mark value non-zero value System.out.print("***" + dateStr + ": " + inLine); zeroCounter = 0; } } // Wait for a while java.lang.Thread.sleep(sleepPeriod); } } } catch (Exception exception) { exception.printStackTrace(); } |
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)
© Oracle Blogs or respective owner