Thursday Nov 29, 2012

RPi and Java Embedded GPIO: Sensor Connections for Java Enabled Interface

Now we're ready to connect the hardware needed to make a static electricity sensor for the Raspberry Pi and use Java code to access it through a GPIO port. First, very carefully bend the NTE312 (or MPF-102) transistor "gate" pin (see the diagram on the back of the package or refer to the pin diagram on the Web). You can see it in the inset photo on the bottom left corner. I bent the leftmost pin of the NTE312 transistor as I held the flat part toward me.

That is going to be your antenna. So, connect one of the jumper wires to the bent pin. I used the dark green jumper wire (looks almost black; coiled at the bottom) in the photo. Then push the other 2 pins of the transistor into your breadboard. Connect one of the pins to Pin # 1 (3.3V) on the GPIO header of your RPi. See the diagram if you need to glance back at it. In the photo, that's the orange jumper wire.

And connect the final unconnected transistor pin to Pin # 22 (GPIO25) on the RPi header. That's the blue jumper wire in my photo. For reference, connect the LED anode (long pin on a common anode LED/short pin on a common cathode LED, check your LED pin diagram) to the same breadboard hole that is connecting to Pin # 22 (same row of holes where the blue wire is connected), and connect the other pin of the LED to GROUND (row of holes that connect to the black wire in the photo).

Test by blowing up a balloon, rubbing it on your hair (or your co-worker's hair, if you are hair-challenged) to statically charge it, and bringing it near your antenna (green wire in the photo). The LED should light up when it's near and go off when you pull it away. If you need more static charge, find a co-worker with really long hair, or rub the balloon on a piece of silk (which is just as good but not as fun).

Next blog post is where we do some Java coding to access this sensor on your RPi. Finally, back to software! Ha! :-)

Monday Nov 26, 2012

RPi and Java Embedded GPIO: Sensor Hardware for Java Enabled Interface

Now here's the hardware you'll need to make a Java app interface with a static charge sensor connected to your Raspberry Pi via the GPIO port.

It means another Fry's run of course. That's not too bad during Christmas since you can browse all the gadget and toys while doing your shopping for sensor hardware for your RPi.

Here's a your shopping list:

  1 - NTE312 JFET N-channel 
      transistor 
      (this is in place of 
       the MPF-102)
  1 - Set of Jumper Wires
  1 - LED
  1 - 300 ohm resistor
  1 - set of header pins

Grab all that from Fry's or your local hobby electronics shop and come back here for how to connect it together. Oh, and don't go too crazy buying all the other electronic toys and gadgets that catch your eye because of the holiday displays at the store. ;-)

Wednesday Nov 21, 2012

RPi and Java Embedded GPIO: Using Java to read input

Now that we've learned about using Java code to control the output of the Raspberry Pi GPIO ports (by lighting up LEDs from a Java app on the RPi for now and noting in the future the same Java code can be used to drive industrial automation or medical equipment, etc.), let's move on to learn about reading input from the RPi GPIO using Java code.

As before, we need to start out with the necessary hardware. For this exercise we will connect a Static Electricity Detector to the RPi GPIO port and read the value of that sensor using Java code. The circuit we'll use is from William J. Beaty and is described at this Web link.

See:

Static Electricity Detector

He calls it an "Electric Charge" detector, which is a bit misleading. A Field Effect Transistor is subject to nearby electro-magnetic fields, such as a static charge on a nearby object, not really an electric charge.

So, this sensor will detect static electricity (or ghosts if you are into paranormal activity ;-) ). Take a look at the circuit and in the next blog posts we'll step through how to connect it to the GPIO port of your RPi and then how to write Java code to access this fun sensor.

Thursday Nov 15, 2012

RPi and Java Embedded GPIO: Java code to blink more LEDs

Now, it's time to blink the other GPIO ports with the other LEDs connected to them. This is easy using Java Embedded, since the Java programming language is powerful and flexible. Embedded developers are not used to this, since the C programming language is more popular but less easy to develop in.

We just need to use a dynamic Java String array to map to the pinouts of the GPIO port names from the previous diagram posted. This way we can address each "channel" with an index into that String array.

    static String[] GpioChannels =                                
    { "0", "1", "4", "17", "21", "22", "10", "9" };

With this new dynamic array, we can streamline the main() of this Java program to activate all the ports.

    /**
     * @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();
            }

And, then simply add array code to where we blink the LED to make it blink all the LEDS on and off at once.

            /*** 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");
            }
            
            // Loop forever
            while (true) {
                
                for (int channum=0; channum < GpioChannels.length; channum++) {
                    // Set GPIO port ON
                    commandChannels[channum].write(GPIO_ON);
                    commandChannels[channum].flush();
                }
                
                // Wait for a while
                java.lang.Thread.sleep(200);
        
                for (int channum=0; channum < GpioChannels.length; channum++) {
                    // Set GPIO port OFF
                    commandChannels[channum].write(GPIO_OFF);
                    commandChannels[channum].flush();
                }
                
                // Wait for a while
                java.lang.Thread.sleep(200);
            }

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

It's easier than falling off a log... or at least easier than C programming. ;-)

Tuesday Nov 13, 2012

RPi and Java Embedded GPIO: Writing Java code to blink LED

So, you've followed the previous steps to install Java Embedded on your Raspberry Pi , you went to Fry's and picked up some jumper wires, LEDs, and resistors , you hooked up the wires, LED, and resistor to the correct pins , and now you want to start programming in Java on your RPi? Yes? ✔✔✔✔✔✔✔!

OK, then... Here we go.

You can use the following source code to blink your first LED on your RPi using Java. In the code you can see that I'm not using any complicated gpio libraries like wiringpi or pi4j, and I'm not doing any low-level pin manipulation like you can in C. And, I'm not using python (hell no!). This is Java programming, so we keep it simple (and more readable) than those other programming languages.

See: Write Java code to do this

In the Java code, I'm opening up the RPi Debian Wheezy well-defined file handles to control the GPIO ports. First I'm resetting everything using the unexport/export file handles. (On the RPi, if you open the well-defined file handles and write certain ASCII text to them, you can drive your GPIO to perform certain operations. See this GPIO reference). Next, I write a "1" then "0" to the value file handle of the GPIO0 port (see the previous pinout diagram). That makes the LED blink. Then, I loop to infinity. Easy, huh?

/*
 * Java Embedded Raspberry Pi GPIO app
 */

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

/**
 *
 * @author hinkmond
 */
public class JerpiGPIO {
    
    static final String GPIO_OUT = "out";
    static final String GPIO_ON = "1";
    static final String GPIO_OFF = "0";
    static final String GPIO_CH00="0";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        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");

            // 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(GPIO_CH00);   
            exportFile.flush();

            // Open file handle to port input/output control
            FileWriter directionFile =
                    new FileWriter("/sys/class/gpio/gpio"+GPIO_CH00+"/direction");
            
            // Set port for output
            directionFile.write(GPIO_OUT);
            directionFile.flush();
            
            /*** Send commands to GPIO port ***/
            
            // Open file handle to issue commands to GPIO port
            FileWriter commandFile = new FileWriter("/sys/class/gpio/gpio"+GPIO_CH00+
                    "/value");
            
            // Loop forever
            while (true) {
                
                // Set GPIO port ON
                commandFile.write(GPIO_ON);
                commandFile.flush();
        
                // Wait for a while
                java.lang.Thread.sleep(200);
        
                // Set GPIO port OFF
                commandFile.write(GPIO_OFF);
                commandFile.flush();
                
                // Wait for a while
                java.lang.Thread.sleep(200);
            }

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

Compile the above Java source file into a class file JerpiGPIO.class on a host system (such as your desktop/laptop PC where you have the full JDK). Then, copy that compiled class file to your RPi device (Ex. scp JerpiGPIO.class pi@'XXX'.'XXX'.'XXX'.'XXX':/home/pi)

Now, to run, open a Linux shell on the RPi device and make sure to first sudo -i (become root interactively) to gain the correct privilege to use the GPIO ports:

   sudo -i

Then, in that same shell on the RPi run with Java this way:
   java -cp /home/pi JerpiGPIO

That's it. Easy as Pi!

Thursday Nov 08, 2012

RPi and Java Embedded GPIO: Hooking Up Your Wires for Java

So, you bought your blue jumper wires, your LEDs, your resistors, your breadboard, and your fill of Fry's for the day. How do you hook this cool stuff up to write Java code to blink them LEDs?

I'll step you through it. First look at that pinout diagram of the GPIO header that's on your RPi. Find the pins in the corner of your RPi board and make sure to orient it the right way. The upper left corner pin should have the characters "P1" next to it on the board. That pin next to "P1" is your Pin #1 (in the diagram).

Then, you can start counting left, right, next row, left, right, next row, left, right, and so on: Pins # 1, 2, next row, 3, 4, next row, 5, 6, and so on.

Take one blue jumper wire and connect to Pin # 3 (GPIO0). Connect the other end to a resistor and then the other end of the resistor into the breadboard. Each row of grouped-together holes on a breadboard are connected, so plug in the short-end of a common cathode LED (long-end of a common anode LED) into a hole that is in the same grouping as where the resistor is plugged in. Then, connect the other end of the LED back to Pin # 6 (GND) on the RPi GPIO header.

Now you have your first LED connected ready for you to write some Java code to turn it on and off. (As, extra credit you can connect 7 other LEDs the same way to with one lead to Pins # 5, 7, 11, 13, 15, 19 & 21). Whew! That wasn't so bad, was it? Next blog post on this thread will have some Java source code for you to try...

Thursday Nov 01, 2012

RPi and Java Embedded GPIO: Connecting LEDs

Next, we need some low-level peripherals to connect to the Raspberry Pi GPIO header. So, we'll do what's called a "Fry's Run" in Silicon Valley, which means we go shop at the local Fry's Electronics store for parts.

In this case, we'll need some breadboard jumper wires (blue wires in photo), some LEDs, and some resistors (for the RPi GPIO, 150 ohms - 300 ohms would work for the 3.3V output of the GPIO ports). And, if you want to do other projects, you might as well buy a breadboard, which is a development board with lots of holes in it to stick wires and electronic components into. Ask a Fry's clerk for help. Or, better yet, ask the customer standing next to you in the electronics components aisle for help. (Might be easier)

So, go to your local hobby electronics store, or go to Fry's if you have one close by, and come back here to the next blog post to see how to hook these parts up.

About

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

Search

Categories
Archives
« November 2012 »
SunMonTueWedThuFriSat
    
2
3
4
6
7
9
10
11
12
14
16
17
18
19
20
22
23
24
25
27
28
30
 
       
Today