Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

Building IoT with Java ME 8 on Raspberry Pi: Part 1 – GPIO Sensor Integration

Discover how to connect and control sensors on a Raspberry Pi using Java ME 8, unlocking powerful IoT capabilities.

Published September 2014

The latest release of Java ME 8 introduces a robust Device I/O API that simplifies control of LEDs, relays, LCDs, sensors, motors, and switches.

This article starts a three‑part series on integrating electronic sensors with the Raspberry Pi Model B via GPIO, I²C, SPI, or UART interfaces.

By harnessing Java ME 8 to drive devices through these interfaces, you can build a fully functional IoT ecosystem.

In this installment we focus on GPIO and provide ready‑to‑use Java ME 8 classes for:

Note: The complete NetBeans IDE 8.0 project, including all source files, can be downloaded here.

Device I/O API Overview

The Device I/O API specification defines a generic peripheral I/O interface for Java applications on embedded platforms. It covers common devices such as:

Key Circuit Considerations

All Raspberry Pi GPIO pins operate at 3.3 V. Before connecting a sensor, verify its voltage requirements. If a device expects 5 V, use a logic‑level converter (e.g., a BSS138 MOSFET‑based shifter) to translate signals safely.

Connecting the Flame Detector

The DFR0076 flame sensor detects light wavelengths between ~760 nm and 1100 nm, with a detection range of 20–100 cm. When flame is present, its signal pin pulls high.

Wire the sensor to the Pi’s 3.3 V, GND, and GPIO 22 as illustrated in Figure 3, then create a Java ME 8 class to control it.

Listing 1 – DFR0076Device class

public class DFR0076Device {
    private GPIOPin pin = null;  // Pin for flame sensor control
}

Listing 2 – Initializing the GPIO pin

public DFR0076Device(int pinGPIO) {
    pin = (GPIOPin) DeviceManager.open(new GPIOPinConfig(
        0, pinGPIO, GPIOPinConfig.DIR_INPUT_ONLY,
        GPIOPinConfig.MODE_INPUT_PULL_UP,
        GPIOPinConfig.TRIGGER_RISING_EDGE, false));
}

Listing 3 – Listener registration

public void setListener(PinListener flameListener) {
    if (pin != null) {
        pin.setInputListener(flameListener);
    }
}

Listing 4 – Resource cleanup

public void close() {
    if (pin != null) {
        pin.setInputListener(null);
        pin.close();
    }
}

Listing 5 – MIDlet that uses the flame sensor

public class TestSensors extends MIDlet {
    private DFR0076Device flame;
    private static final int FLAME_DETECTOR_PIN = 22;

    public void startApp() {
        flame = new DFR0076Device(FLAME_DETECTOR_PIN);
        flame.setListener(new FlameSensor());
    }

    public void destroyApp(boolean unconditional) {
        flame.close();
    }

    private static int waitNext = 1;

    private class FlameSensor implements PinListener {
        public void valueChanged(PinEvent event) {
            if (event.getValue() && --waitNext == 0) {
                System.out.println("WARNING Flame detected!!!");
                waitNext = 10;
            }
        }
    }
}

Connecting the Motion Detector

Integrate the HC‑SR501 PIR sensor, which outputs a 3.3 V pulse when motion is detected. The sensor’s three pins are GND, VCC (3–5 V), and digital out.

Adjust the PIR’s sensitivity and delay via its potentiometers, and set the jumper to “L” for single triggers or “H” for repeatable triggers.

Wire the PIR to 5 V, GND, and GPIO 24, then extend the MIDlet with a dedicated HCSR501Device class.

Listing 6 – HCSR501Device class

public class HCSR501Device {
    private GPIOPin pin = null;
}

Listing 7 – Pin initialization with a 3‑second delay

public HCSR501Device(int pinGPIO) {
    pin = (GPIOPin) DeviceManager.open(new GPIOPinConfig(
        0, pinGPIO, GPIOPinConfig.DIR_INPUT_ONLY,
        GPIOPinConfig.MODE_INPUT_PULL_DOWN,
        GPIOPinConfig.TRIGGER_RISING_EDGE, false));
    I2CUtils.I2Cdelay(3000); // Wait for sensor stabilization
}

Listing 8 – Registering a motion listener

public void setListener(PinListener pirListener) {
    if (pin != null) {
        pin.setInputListener(pirListener);
    }
}

Listing 9 – Cleanup

public void close() {
    if (pin != null) {
        pin.setInputListener(null);
        pin.close();
    }
}

Extend the TestSensors MIDlet to handle motion events:

Listing 10 – MIDlet with PIR support

// Initialize PIR sensor
private HCSR501Device pir;
private static final int MOTION_DETECTOR_PIN = 24;

@Override
public void startApp() {
    pir = new HCSR501Device(MOTION_DETECTOR_PIN);
    pir.setListener(new PirSensor());
}

@Override
public void destroyApp(boolean unconditional) {
    pir.close();
}

private class PirSensor implements PinListener {
    public void valueChanged(PinEvent event) {
        if (event.getValue()) {
            System.out.println("WARNING Motion detected!!!");
        }
    }
}

For a deeper dive, read the full series: Java ME 8 + Raspberry Pi + Sensors = IoT World (Part 1).

Manufacturing process

  1. Java Hello World: Your First Program
  2. Building a Multichannel Data Logger with Raspberry Pi – Part 1: Requirements & Architecture
  3. Building IoT with Java ME 8 on Raspberry Pi: Part 1 – GPIO Sensor Integration
  4. Reading a Linear Hall Sensor with Raspberry Pi: Java Guide & ADC0832 Insights
  5. Mastering Raspberry Pi Sensors: A Practical Guide to Electronics and Communication Protocols
  6. Installing Windows 10 IoT Core on Raspberry Pi 3 Model B+: A Step‑by‑Step Guide
  7. IoT Security: Unpacking the Unique Threat Landscape for Connected Devices (Part 1)
  8. Industrial IoT Security: Rising Threats and the Need for Integrated IT/OT Protection – Part 1
  9. The Five Core Challenges of IoT: A Deep Dive into the 5 Cs – Part 1
  10. Closing the IoT Blind Spot in a Post‑Pandemic World