Internet of Things (IoT) Christmas Special: Santa Detector (Part 3)

Admiral Ackbar Santa thinks our Santa Detector is a trap. Well, he's right! We are setting up our Internet of Things (IoT) Santa Detector to catch Santa in the act on Christmas Eve to tell the world whether Santa Claus exists.

(I have a sneaky suspicion that we will detect Santa Claus on Christmas Eve and prove his existence. But, that's just a hunch... ;-) )

Admiral Ackbar Santa really shouldn't worry. We're not really going to try to trap Santa, just tweet when he's detected by our Java SE Embedded application.

So, you've followed the directions, so far, now you just need the Java source code for the Java SE Embedded app to run on your Raspberry Pi connected with a Parallax PIR sensor.

Here's the full Java source code that you'll need:
https://java.net/projects/orbit/downloads/download/SantaDetector.java

Here's a code excerpt from the full file above of Java SE Embedded code that uses the GPIO port on your Raspberry Pi connected to the Parallax PIR sensor to read in a value to detect if Santa is standing in front of the strategically located bait, er, um... I mean, plate of cookies and glass of milk:

//
// 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("Activating GPIO port: "
		       + 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 dir control of port
    FileWriter directionFile
	= new FileWriter("/sys/class/gpio/gpio"
			 + gpioChannel + "/direction");

    // Set port for input
    directionFile.write(GPIO_IN);
}
                
// Open RandomAccessFile handle for each GPIO port
for (int channum=0; channum < raf.length; channum++) {
    raf[channum] = new RandomAccessFile("/sys/class/gpio/gpio"
					+ GpioChannels[channum] + 
					"/value", "r");
}

//...


// Loop to keep reading sensor value
while (true) {
    int available;
            
	// Use RandomAccessFile handle to read in GPIO port value
	for (int channum=0; channum < raf.length; channum++) {
            
	    try {
		// Reset file seek pointer to read latest value 
		//   of GPIO port
		raf[channum].seek(0);
		raf[channum].read(inBytes);
	    } catch (IOException ioe) {
		ioe.printStackTrace();
	    }
                        
	    inLine = new String(inBytes);
            
	    // Check if "on" value was read
	    if (inLine.equals("1")) {

		// Specially mark value non-zero value
		gpioPortVal = 1;
		  
	    } else {
		// Else, no "on" value read
		gpioPortVal = 0;
	    }

	    System.out.println("Reading GPIO Port: " + 
			       GpioChannels[channum] +
			       "  Time: "+ timedateString + "  GPIO value = " + 
			       gpioPortVal);
	}
    }

So, as you can see in the code snippet above, the Java source code just keeps reading from the GPIO port where the PIR sensor is connected, whether it has a value of "0" (no motion detected) or "1" (motion detected), and that will tell us if Santa is there in front of the plate of cookies and glass of milk.

In the next blog post, we'll look at the Java code that will send out a Tweet when that happens, letting the cloud know when Santa has arrived at our location.

So, don't worry Admiral Ackbar Santa, it's all good! ;-)

See the full series of steps:
Internet of Things (IoT) Christmas Special: Santa Detector (Part 1)
Internet of Things (IoT) Christmas Special: Santa Detector (Part 2)
Internet of Things (IoT) Christmas Special: Santa Detector (Part 3)
Internet of Things (IoT) Christmas Special: Santa Detector (Part 4)
Internet of Things (IoT) Christmas Special: Santa Detector (Part 5)
<<< Previous  | Next >>>

Comments:

Check the original detector ;)
http://slid.es/disk91/internetofthings

Have fun and happy Xmas

Posted by guest on December 23, 2013 at 11:45 AM PST #

Post a Comment:
Comments are closed for this entry.
About

Hinkmond Wong's blog on making Machine to Machine (M2M) and the Incredible Internet of Things (IoT) smarter with Java Embedded Technologies

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today