2

I'm having troubles to get this thing work. Basically i have my raspberry, with apache and django running good, in my views i can turn on and off a led using gpiozero, i can do pretty much every output thing i want with gpio pins.

But there is this thing i can't do: How to get input from gpio pins?

I tried setting up Celery but there are 2 problems, i can't make it work how i want (after 3 days of tests looking 10 different guides, even official ones) and it doesn't do exactly what i want. Celery can do background process only when they are called in a django view.

What i want to do is having a background process that run 24/24 that watch pin input activity. Simple example: When the button connected on gpio 23 is pressed, change a value in a django model or turn on another gpio pin.

Someone with a hint? Thanks all!

3 Answers 3

1

I have worked with Django and Celery but not with the Raspberry Pi, so this might not be an ideal solution. Since you haven't provided your code I'll try to outline how you would go about creating this functionality.

  1. Set up your Django Application and Celery integration

  2. Create the Celery Task to modify your django model value when it's called

  3. Create a new script and import the celery task from your Django project

  4. Implement a function in your script that triggers the Celery Job

  5. Create a button object and add a call to your function to the on_pressed method (see below)

  6. Run your script

Based on code from this article.

from gpiozero import Button
from signal import pause

def trigger_celery_task():
    # Add the code to create a new task here

button = Button(2)

button.when_pressed = trigger_celery_task

# Wait for events
pause()
Sign up to request clarification or add additional context in comments.

2 Comments

So, this script will be running in background without stopping? This seems to be a good solution, i'll have to make celery work. At the moment i cant really make it work
Correct. You will need this script to run in the background and a celery worker that runs in the background as well.
1

You can use GPIOZero Button Interface to check when a button is clicked. Here, each time a button is clicked, the function toggleLight is automatically called. Now, inside this function, you can manipulate your django models, etc.

from gpiozero import LED, Button  #IMPORTS FOR GPIOZERO

# ADD THIS CODE TO YOUR DJANGO APPLICATION OUTSIDE ANY FUNCTION

state = 0

led = LED(17) #LED TO GPIO17
button = Button(23) #BUTTON TO GPIO23

def toggleLight():

    # DO ANY DJANGO RELATED CHANGES HERE, CHANGE VARIABLES ETC.

    # TOGGLE THE LED CONNECTED TO GPIO17
    if (state == 0): #CURRENTLY OFF
        led.on()
        state = 1 
    else: #CURRENTLY ON
        led.off()
        state = 0

button.when_pressed = toggleLight  #EACH TIME THE BUTTON IS PRESSED, THE FUNCTION TOGGLELIGHT IS CALLED

#ENDS HERE

Hope this helps!

Comments

1

Try the 'GPIO.add_event_detect' functionality to define a function to be called when the input changes. See https://pypi.python.org/pypi/RPi.GPIO/0.5.1a

Be carefull when you initialize the add_event_detect functionality because if you don't ensure this functionality is initialized only once and in one process, you will have several calls to the function (one per each process) https://raspberrypi.stackexchange.com/questions/8584/multiple-gpio-add-event-detect-one-callback-function

Regards and good luck!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.