Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for GPIO Buttons #1562

Closed
burberius opened this issue Feb 8, 2019 · 3 comments
Closed

Documentation for GPIO Buttons #1562

burberius opened this issue Feb 8, 2019 · 3 comments

Comments

@burberius
Copy link

burberius commented Feb 8, 2019

I had problems with the modules MMM-Buttons and MMM-Button that they didn't recognize button presses or fired several presses. I think the Javascript onoff library to access the GPIOs is very bad.
My button (connected to GND and GPIO) worked without problems when using a simple python script!

So I found the following workaround as a replacement for the button modules:

  1. install MMM-Remote-Control
  2. add the button script printed at the end
  3. add the following line to /etc/rc.local:
    python /home/pi/button.py &

Script

#!/usr/bin/python

import RPi.GPIO as GPIO
import urllib2
import time

# Button number in GPIO numbering
GPIO_BUTTON = 27
# Shutdown time in seconds
SHUTDOWN = 5
# Debounce time in seconds
DEBOUNCE = 0.15


GPIO.setmode(GPIO.BCM)
# Set pin as input
GPIO.setup(GPIO_BUTTON,GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Initialize
start  = 99999999999999

# Callback-Function
def WORK(PIR_GPIO):
  global start
  if GPIO.input(GPIO_BUTTON):
    diff = time.time() - start

    if diff > SHUTDOWN:
      urllib2.urlopen("http://localhost:8080/remote?action=SHUTDOWN").read()
    elif diff > DEBOUNCE:
      urllib2.urlopen("http://localhost:8080/remote?action=NOTIFICATION&notification=PAGE_UP").read()
  else:
    start = time.time()

try:
  GPIO.add_event_detect(GPIO_BUTTON, GPIO.BOTH, callback=WORK)
  while True:
    time.sleep(60)

except KeyboardInterrupt:
  GPIO.cleanup()

Description
The script measures the time the button is pressed. On release it decides if it was a long press, which means a shutdown, a short press which means in my case send a broadcast to change the page or too short which will be ignored.
For shutdown and broadcast it uses the remote control http interface!

@burberius
Copy link
Author

This is a suggestion for the wiki, I think adding it here is better than somewhere at the two button modules.
What do you think?

@edward-shen
Copy link
Contributor

Just an FYI, you might get better feedback in those respective repos than here, since MM core can't really do anything about making sure modules well support the RPi platform.

@MichMich
Copy link
Collaborator

AS @edward-shen states, please open an issue in the respective repo's. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants