Hinkmond Wong's Weblog

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

Guest Author

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:


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()) {


// Set the port for use
// Open file handle to input/output dir control of port
FileWriter directionFile

= new FileWriter("/sys/class/gpio/gpio"

+ gpioChannel + "/direction");
// Set port for input
// 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



} catch (IOException ioe) {



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 = " +



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 >>>

Join the discussion

Comments ( 1 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.