Exemplo n.º 1
0
  public static void main(String[] args) throws InterruptedException {

    System.out.println("<--Pi4J--> GPIO Trigger Example ... started.");

    // create gpio controller
    final GpioController gpio = GpioFactory.getInstance();

    // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
    final GpioPinDigitalInput myButton =
        gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);

    System.out.println(" ... complete the GPIO #02 circuit and see the triggers take effect.");

    // setup gpio pins #04, #05, #06 as an output pins and make sure they are all LOW at startup
    GpioPinDigitalOutput myLed[] = {
      gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "LED #1", PinState.LOW),
      gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05, "LED #2", PinState.LOW),
      gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06, "LED #3", PinState.LOW)
    };

    // create a gpio control trigger on the input pin ; when the input goes HIGH, also set gpio pin
    // #04 to HIGH
    myButton.addTrigger(new GpioSetStateTrigger(PinState.HIGH, myLed[0], PinState.HIGH));

    // create a gpio control trigger on the input pin ; when the input goes LOW, also set gpio pin
    // #04 to LOW
    myButton.addTrigger(new GpioSetStateTrigger(PinState.LOW, myLed[0], PinState.LOW));

    // create a gpio synchronization trigger on the input pin; when the input changes, also set gpio
    // pin #05 to same state
    myButton.addTrigger(new GpioSyncStateTrigger(myLed[1]));

    // create a gpio pulse trigger on the input pin; when the input goes HIGH, also pulse gpio pin
    // #06 to the HIGH state for 1 second
    myButton.addTrigger(new GpioPulseStateTrigger(PinState.HIGH, myLed[2], 1000));

    // create a gpio callback trigger on gpio pin#4; when #4 changes state, perform a callback
    // invocation on the user defined 'Callable' class instance
    myButton.addTrigger(
        new GpioCallbackTrigger(
            new Callable<Void>() {
              public Void call() throws Exception {
                System.out.println(" --> GPIO TRIGGER CALLBACK RECEIVED ");
                return null;
              }
            }));

    // keep program running until user aborts (CTRL-C)
    for (; ; ) {
      Thread.sleep(500);
    }

    // stop all GPIO activity/threads by shutting down the GPIO controller
    // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
    // gpio.shutdown();   <--- implement this method call if you wish to terminate the Pi4J GPIO
    // controller
  }
Exemplo n.º 2
0
  @Before
  public void setup() {
    // create a mock gpio provider and controller
    provider = MockGpioFactory.getMockProvider();
    gpio = MockGpioFactory.getInstance();

    // provision pins for testing
    inputPin = gpio.provisionDigitalInputPin(MockPin.DIGITAL_INPUT_PIN, "digitalInputPin");
    outputPin = gpio.provisionDigitalOutputPin(MockPin.DIGITAL_OUTPUT_PIN, "digitalOutputPin");

    // create triggers
    triggerHigh = new GpioSetStateTrigger(PinState.HIGH, outputPin, PinState.HIGH);
    triggerLow = new GpioSetStateTrigger(PinState.LOW, outputPin, PinState.LOW);

    // add triggers to input pin
    inputPin.addTrigger(triggerHigh);
    inputPin.addTrigger(triggerLow);
  }
  @BeforeClass
  public static void setup() {
    // create a mock gpio provider and controller
    provider = MockGpioFactory.getMockProvider();
    gpio = MockGpioFactory.getInstance();

    // provision pins for testing
    inputPin = gpio.provisionDigitalInputPin(MockPin.DIGITAL_INPUT_PIN, "digitalInputPin");

    // create trigger
    trigger =
        new GpioCallbackTrigger(
            new Callable<Void>() {
              @Override
              public Void call() throws Exception {
                callbackCounter++;
                return null;
              }
            });

    // add trigger to input pin
    inputPin.addTrigger(trigger);
  }
Exemplo n.º 4
0
  public static void main(String[] args) throws InterruptedException {

    // START SNIPPET: usage-create-controller-snippet
    // create gpio controller instance
    final GpioController gpio = GpioFactory.getInstance();
    // END SNIPPET: usage-create-controller-snippet

    // START SNIPPET: usage-provision-input-pin-snippet
    // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
    // (configure pin edge to both rising and falling to get notified for HIGH and LOW state
    // changes)
    GpioPinDigitalInput myButton =
        gpio.provisionDigitalInputPin(
            RaspiPin.GPIO_02, // PIN NUMBER
            "MyButton", // PIN FRIENDLY NAME (optional)
            PinPullResistance.PULL_DOWN); // PIN RESISTANCE (optional)

    // END SNIPPET: usage-provision-input-pin-snippet
    // START SNIPPET: usage-provision-output-pin-snippet
    // provision gpio pins #04 as an output pin and make sure is is set to LOW at startup
    GpioPinDigitalOutput myLed =
        gpio.provisionDigitalOutputPin(
            RaspiPin.GPIO_04, // PIN NUMBER
            "My LED", // PIN FRIENDLY NAME (optional)
            PinState.LOW); // PIN STARTUP STATE (optional)
    // END SNIPPET: usage-provision-output-pin-snippet

    // START SNIPPET: usage-shutdown-pin-snippet
    // configure the pin shutdown behavior; these settings will be
    // automatically applied to the pin when the application is terminated
    // ensure that the LED is turned OFF when the application is shutdown
    myLed.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF);
    // END SNIPPET: usage-shutdown-pin-snippet

    // START SNIPPET: usage-control-pin-snippet
    // explicitly set a state on the pin object
    myLed.setState(PinState.HIGH);

    // use convenience wrapper method to set state on the pin object
    myLed.low();
    myLed.high();

    // use toggle method to apply inverse state on the pin object
    myLed.toggle();

    // use pulse method to set the pin to the HIGH state for
    // an explicit length of time in milliseconds
    myLed.pulse(1000);
    // END SNIPPET: usage-control-pin-snippet

    // START SNIPPET: usage-read-pin-snippet
    // get explicit state enumeration for the GPIO pin associated with the button
    PinState myButtonState = myButton.getState();

    // use convenience wrapper method to interrogate the button state
    boolean buttonPressed = myButton.isHigh();
    // END SNIPPET: usage-read-pin-snippet

    // START SNIPPET: usage-register-listener-snippet
    // create and register gpio pin listener
    myButton.addListener(new GpioUsageExampleListener());
    // END SNIPPET: usage-register-listener-snippet

    // START SNIPPET: usage-trigger-snippet
    // create a gpio synchronization trigger on the input pin
    // when the input state changes, also set LED controlling gpio pin to same state
    myButton.addTrigger(new GpioSyncStateTrigger(myLed));
    // END SNIPPET: usage-trigger-snippet

    // keep program running until user aborts (CTRL-C)
    while (true) {
      Thread.sleep(500);
    }

    // stop all GPIO activity/threads by shutting down the GPIO controller
    // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
    // gpio.shutdown();   <--- implement this method call if you wish to terminate the Pi4J GPIO
    // controller
  }
  /**
   * @param args --none-
   * @throws InterruptedException
   */
  public static void main(String[] args) throws InterruptedException {

    // create Pi4J console wrapper/helper
    // (This is a utility class to abstract some of the boilerplate code)
    final Console console = new Console();

    // print program title/header
    console.title("<-- The Pi4J Project -->", "IFTTT Maker Channel Trigger Example");

    // allow for user to exit program using CTRL-C
    console.promptForExit();

    // create gpio controller
    final GpioController gpio = GpioFactory.getInstance();

    // provision gpio pin as an input pin
    final GpioPinDigitalInput input =
        gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, "MyInputPin", PinPullResistance.PULL_DOWN);

    // provide 500ms of pin debounce protection
    input.setDebounce(500);
    ;

    // -----------------------------------------
    // Add the IFTTT Maker Channel Trigger
    // (This is where the magic happens)
    // -----------------------------------------
    input.addTrigger(
        new IFTTTMakerChannelTrigger(
            IFTTT_MAKER_CHANNEL_API_KEY, // <<-- PROVIDE YOUR ACCOUNT SPECIFIC IFTTT MAKER CHANNEL
                                         // API KEY
            IFTTT_MAKER_CHANNEL_EVENT_NAME, // <<-- PROVIDE THE IFTTT MAKER CHANNEL EVENT NAME
                                            // (defined in your IFTTTT recipe)
            PinState.HIGH, // <<-- OPTIONALLY DEFINE A SPECIFIC STATE TO TRIGGER ON

            // OPTIONALLY REGISTER A TRIGGER CALLBACK LISTENER
            // (Note: this callback parameter is not required for basic functionality)
            new IFTTTMakerChannelTriggerListener() {
              @Override
              public boolean onTriggered(IFTTTMakerChannelTriggerEvent event) {

                // The IFTTT Maker Channel API accepts three value parameters (value1, value2, and
                // value3)
                // By default, Pi4J applies the following values to each:
                //
                // "value1" = {pin-name}
                // "value2" = {pin-state-value} (as an Integer; 0==LOW, 1==HIGH)
                // "value3" = {json-payload}  Example:
                // {
                //    "pin": {
                //        "name": "MyInputPin",
                //        "address": "0",
                //        "provider": "RaspberryPi GPIO Provider",
                //        "mode": "input",
                //        "direction": "IN",
                //        "pull": "down"
                //    },
                //    "state": {
                //        "name": "HIGH",
                //        "value": "1",
                //        "is-high": "true",
                //        "is-low": "false"
                //    },
                //    "timestamp": "2016-04-15T17:32:49.666-0400"
                // }
                //
                // However, you can override any of these defaults in your callback listener by
                // applying new string values via the 'setValueX()' methods on the event object.
                //
                // Example:
                if (event.getValue2().equals("1")) {
                  event.setValue2("ON");
                } else {
                  event.setValue2("OFF");
                }

                // display event trigger details on screen
                console.println(" --> IFTTT MAKER CHANNEL EVENT TRIGGER");
                console.println("     - GPIO PIN            : " + event.getPin());
                console.println("     - PIN STATE           : " + event.getState());
                console.println("     - IFTTT EVENT NAME    : " + event.getEventName());
                console.println("     - IFTTT EVENT VALUE 1 : " + event.getValue1());
                console.println("     - IFTTT EVENT VALUE 2 : " + event.getValue2());
                console.println("     - IFTTT EVENT VALUE 3 : " + event.getValue3());
                console.emptyLine();

                // MAKE SURE TO RETURN 'true' TO CONTINUE WITH THE IFTTT MAKER CHANNEL API CALL
                // (you can optionally return 'false' if you want to abort the IFTTT API call)
                return true;
              }
            }));

    // set shutdown state for this pin: unexport the pin
    input.setShutdownOptions(true);

    // prompt user that we are ready
    console.println(
        "Successfully provisioned ["
            + input
            + "] with PULL resistance = ["
            + input.getPullResistance()
            + "]");
    console.emptyLine();

    // wait for user to exit by pressing CTRL-C
    console.waitForExit();

    // stop all GPIO activity/threads by shutting down the GPIO controller
    // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
    gpio.shutdown();
  }
Exemplo n.º 6
0
  public static void main(String args[]) throws InterruptedException {

    System.out.println("<--Pi4J--> GPIO Listen Example ... started.");

    // create gpio controller
    final GpioController gpio = GpioFactory.getInstance();

    // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
    final GpioPinDigitalInput myButton =
        gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);

    // create custom Olimex GPIO provider
    final OlimexAVRIOGpioProvider olimexProvider =
        new OlimexAVRIOGpioProvider(Serial.DEFAULT_COM_PORT);

    // provision gpio input pin #01 from Olimex
    final GpioPinDigitalInput myInput =
        gpio.provisionDigitalInputPin(olimexProvider, OlimexAVRIOPin.IN_01);

    // create gpio pin listener
    GpioPinListenerDigital listener =
        new GpioPinListenerDigital() {
          @Override
          public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
            // display pin state on console
            System.out.println(
                " --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
          }
        };

    // register gpio pin listener for each input pin
    myButton.addListener(listener);
    myInput.addListener(listener);

    // setup gpio pins #04, #05, #06 as an output pins and make sure they are all LOW at startup
    GpioPinDigitalOutput myRelays[] = {
      gpio.provisionDigitalOutputPin(
          olimexProvider, OlimexAVRIOPin.RELAY_01, "RELAY #1", PinState.LOW),
      gpio.provisionDigitalOutputPin(
          olimexProvider, OlimexAVRIOPin.RELAY_02, "RELAY #2", PinState.LOW),
      gpio.provisionDigitalOutputPin(
          olimexProvider, OlimexAVRIOPin.RELAY_03, "RELAY #3", PinState.LOW),
      gpio.provisionDigitalOutputPin(
          olimexProvider, OlimexAVRIOPin.RELAY_04, "RELAY #4", PinState.LOW)
    };

    // create a gpio control trigger on the input pin ; when the input goes HIGH, also set gpio pin
    // #04 to HIGH
    myButton.addTrigger(new GpioSetStateTrigger(PinState.HIGH, myRelays[0], PinState.HIGH));

    // create a gpio control trigger on the input pin ; when the input goes LOW, also set gpio pin
    // #04 to LOW
    myButton.addTrigger(new GpioSetStateTrigger(PinState.LOW, myRelays[0], PinState.LOW));

    // create a gpio synchronization trigger on the input pin; when the input changes, also set gpio
    // pin #05 to same state
    myButton.addTrigger(new GpioSyncStateTrigger(myRelays[1]));

    // create a gpio synchronization trigger on the input pin; when the input changes, also set gpio
    // pin #05 to same state
    myButton.addTrigger(new GpioSyncStateTrigger(myRelays[2]));

    // create a gpio pulse trigger on the input pin; when the input goes HIGH, also pulse gpio pin
    // #06 to the HIGH state for 1 second
    myButton.addTrigger(new GpioPulseStateTrigger(PinState.HIGH, myRelays[3], 1000));

    System.out.println(
        " ... complete the GPIO #02 circuit and see the listener feedback here in the console.");

    // keep program running until user aborts (CTRL-C)
    // or we reach 60 seconds
    for (int seconds = 0; seconds < 60; seconds++) {
      Thread.sleep(1000);
    }

    System.out.println(" ... exiting program.");

    // stop all GPIO activity/threads by shutting down the GPIO controller
    // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
    gpio.shutdown();
  }