5

I am trying to implement the button sample from simplepio. I have made the connection as shown in schematics. After pressing the button I do not get the GPIO callback.

Code I am using is same as that of sample. There are no exceptions only "Starting Activity" gets print in log

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "Starting ButtonActivity");

    PeripheralManagerService service = new PeripheralManagerService();
    try {
        String pinName = BoardDefaults.getGPIOForButton();
        mButtonGpio = service.openGpio(pinName);
        mButtonGpio.setDirection(Gpio.DIRECTION_IN);
        mButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
        mButtonGpio.registerGpioCallback(new GpioCallback() {
            @Override
            public boolean onGpioEdge(Gpio gpio) {
                Log.i(TAG, "GPIO changed, button pressed");
                // Return true to continue listening to events
                return true;
            }
        });
    } catch (IOException e) {
        Log.e(TAG, "Error on PeripheralIO API", e);
    }
}

What I have tried so far:

  1. Verified that the circuit and button are functional by running a python button program in raspbian jessie with following code

    #!/usr/bin/env python
    
    import os
    from time import sleep
    
    import RPi.GPIO as GPIO
    
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP)
    
    while True:
        if (GPIO.input(21) == False):
            print("Button Clicked")
    
        sleep(0.1)
    

    The above code prints "Button Clicked" when button is pressed. So I am sure that the button and GPIO pins on my PI are not an issue.

  2. To make sure there is no issue with logging I also tried modifying the original program to contain a TextView and a counter so as when a button is clicked the counter value is incremented and displayed in TextView but again the callback wasn't received and TextView wasn't updated.
  3. Tried different edge trigger type but onGpioEdge is never called.

Following is the picture of my setup

enter image description here

Anirudha Agashe
  • 3,510
  • 2
  • 32
  • 47
  • 2
    Are you able to confirm with a voltmeter that the voltage level changes from 3.3V to 0V at Pin 40 (BCM 21) on the Raspberry Pi with the Android code running and the button activated. I believe that your python code is enabling the Raspberry Pi's internal pull up. So perhaps the Android Thing's PeripheralIO doesn't and the external pull up on your breadboard is not making a good connection. – Dave McKelvie Jan 16 '17 at 04:43
  • @DaveMcKelvie : Thanks this was very useful not only for this but also for future debugging as well – Anirudha Agashe Jan 17 '17 at 16:39

3 Answers3

3

Is it just me or is your resistor in the wrong breadboard row

wrong

The arrow shows where it is, the circle shows where it should be.

According to the fritzing diagram:

enter image description here

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • I thought that too for a while, but the more I looked at it the more I thought it was actually like this. If you look at the original long enough and squint your eyes a bit and use your imagination you can see the hole it's in is not quite square, there could be a resistor leg going in there. – Dave McKelvie Jan 17 '17 at 04:52
  • [Here's the image](http://android.geek.nz/wordpress/wp-content/uploads/2017/01/resistor_leg.png) – Dave McKelvie Jan 17 '17 at 04:59
  • I think @Blundell is right, I squinted quite a lot and it looks like it is in the wrong place. – shalafi Jan 17 '17 at 14:00
  • I think I must have misaligned the resistance while trying to redo the circuit so as to have different colored wires but I am not sure now if originally I connected it right or wrong. I also found another possible reason that might have caused it – Anirudha Agashe Jan 17 '17 at 16:24
0

I found the Button driver to be quite unreliable on the Raspberry PI with Android things, after all the driver is pretty much the same code you have.

However, ButtonInputDriver worked flawlessly.

In fact, you do not need to address the GPIO directly and can use the drivers layers, which is simpler. The button driver is here: https://github.com/androidthings/contrib-drivers/tree/master/cap12xx

I suggest you give ButtonInputDriver a try.

shalafi
  • 3,926
  • 2
  • 23
  • 27
  • Thanks. I will try that. But when you used the simple io code were you able to see logs consistently on button press ? – Anirudha Agashe Jan 16 '17 at 10:04
  • I only used the driver, not the GPIOs directly (but looked at the code and it does just that) and no, the logs were not consistent or reliable at all. – shalafi Jan 16 '17 at 11:06
  • After getting it to work and playing around I found that logging and button driver are reliable but lack the debounce implementation so yo may get multiple clicks – Anirudha Agashe Jan 17 '17 at 16:22
  • Yes, you are right, you get multiple event down. I believe the button driver debounce implementation is faulty and that's the source of my problems. – shalafi Jan 17 '17 at 17:00
  • Can you report the debounce issue you add against the https://github.com/androidthings/contrib-drivers repo, was it with regular push button or capacitive touch (cap12xx)? – proppy Jan 18 '17 at 19:49
  • It was with the capacity buttons on the Rainbow Hat @proppy – shalafi Jan 19 '17 at 10:05
0

It might be possible that I didn't connect the circuit as per schematics or the resistor might not be making good contact. The best way to debug this is as Dave McKelvie suggested is to measure the voltage using voltmeter.

The reason why the Python code was working because the Raspberry PI 3 has internal pull up resistor which was got used as suggested by Dave McKelvie in the comments.

Another reason that the button might not be working is if the GPIO pin is already being used by another application. The logger show the following error for following scenario

Error on PeripheralIO API
    com.google.android.things.pio.PioException: android.os.ServiceSpecificException: BCM21 is already in use
       at com.google.android.things.pio.GpioImpl.<init>(GpioImpl.java:53)
       at com.google.android.things.pio.PeripheralManagerService.openGpio(PeripheralManagerService.java:169)
       at com.example.androidthings.simplepio.ButtonActivity.onCreate(ButtonActivity.java:129)
       at android.app.Activity.performCreate(Activity.java:6662)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6077)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Caused by: android.os.ServiceSpecificException: BCM21 is already in use
       at android.os.Parcel.readException(Parcel.java:1697)
       at android.os.Parcel.readException(Parcel.java:1636)
       at com.google.android.things.pio.IPeripheralManagerClient$Stub$Proxy.OpenGpio(IPeripheralManagerClient.java:776)
       at com.google.android.things.pio.GpioImpl.<init>(GpioImpl.java:51)
       at com.google.android.things.pio.PeripheralManagerService.openGpio(PeripheralManagerService.java:169) 
       at com.example.androidthings.simplepio.ButtonActivity.onCreate(ButtonActivity.java:129) 
       at android.app.Activity.performCreate(Activity.java:6662) 
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:154) 
       at android.app.ActivityThread.main(ActivityThread.java:6077) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Anirudha Agashe
  • 3,510
  • 2
  • 32
  • 47