Happy Internet of Things (IoT) Hanukkah from Java SE Embedded!

Hope everyone has a happy Internet of Things (IoT) Hanukkah from Java SE Embedded and this Rasberry Pi running a Java SE Embedded app to light up the 8th night menorah light.

Here's the holiday Java code for your Internet of Things (IoT) menorah:

/*
 * Java Embedded Raspberry Pi GPIO app
 */

import java.io.FileWriter;
import java.io.File;

/**
 *
 * @author hinkmond
 */
public class JavaEmbMenorah {
    
    static final String GPIO_OUT = "out";
    static final String GPIO_ON = "1";
    static final String GPIO_OFF = "0";
    
    static String[] GpioChannels = 
    { "0", "1", "4", "17", "21", "22", "10", "9", "11" };

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        FileWriter[] commandChannels;
        
        try {
            
            /*** Init GPIO port for output ***/
            
            // 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 port input/output control
                FileWriter directionFile =
                    new FileWriter("/sys/class/gpio/gpio" + gpioChannel + 
                        "/direction");
            
                // Set port for output
                directionFile.write(GPIO_OUT);
                directionFile.flush();
            }
            
            /*** Send commands to GPIO port ***/
            
            commandChannels = new FileWriter[GpioChannels.length];
            
            for (int channum=0; channum < GpioChannels.length; channum++) {
                
                // Open file handle to issue commands to GPIO port
                commandChannels[channum] = 
                        new FileWriter("/sys/class/gpio/gpio" +
                        GpioChannels[channum] + "/value");
            }
            
            int channum = 0;
            int sleepPeriod = 1000; // (1 sec. in milliseconds)
            // Loop forever
            while (true) {
                
                if (++channum >= GpioChannels.length) {
                    channum = 0;
                }
                
                // Refresh Menorah Lights
                commandChannels[channum].write(GPIO_ON);
                commandChannels[channum].flush();
               
                
                // Wait for a while
                java.lang.Thread.sleep(sleepPeriod);
        
            }

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

Comments:

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