简体   繁体   English

Raspberry Pi和Django - 背景检查GPIO按钮

[英]Raspberry Pi and Django - Background check GPIO Button

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. 基本上我有我的覆盆子,apache和django运行良好,在我看来我可以使用gpiozero打开和关闭一个led,我几乎可以用gpio引脚做我想要的所有输出

But there is this thing i can't do: How to get input from gpio pins? 但有一件事我不能做:如何从gpio引脚获取输入

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. 我尝试设置芹菜,但有两个问题,我不能让它工作我想要的(经过3天的测试看10个不同的指南,甚至官方的),它并没有完全符合我的要求。 Celery can do background process only when they are called in a django view. Celery只有在django视图中调用它们时才能进行后台处理。

What i want to do is having a background process that run 24/24 that watch pin input activity. 我想做的是让后台进程24/24运行,观看引脚输入活动。 Simple example: When the button connected on gpio 23 is pressed, change a value in a django model or turn on another gpio pin. 简单示例:当按下连接在gpio 23上的按钮时,更改django模型中的值或打开另一个gpio引脚。

Someone with a hint? 有提示的人? Thanks all! 谢谢大家!

You can use GPIOZero Button Interface to check when a button is clicked. 您可以使用GPIOZero Button界面检查单击按钮的时间。 Here, each time a button is clicked, the function toggleLight is automatically called. 在这里,每次单击一个按钮时, toggleLight自动调用toggleLight函数。 Now, inside this function, you can manipulate your django models, etc. 现在,在这个函数中,你可以操纵你的django模型等。

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! 希望这可以帮助!

I have worked with Django and Celery but not with the Raspberry Pi, so this might not be an ideal solution. 我曾经使用过Django和Celery,但没有使用Raspberry Pi,所以这可能不是一个理想的解决方案。 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 设置Django应用程序和Celery集成

  2. Create the Celery Task to modify your django model value when it's called 创建Celery任务以在调用时调整django模型值

  3. Create a new script and import the celery task from your Django project 创建一个新脚本并从Django项目中导入celery任务

  4. Implement a function in your script that triggers the Celery Job 在脚本中实现一个触发Celery作业的函数

  5. Create a button object and add a call to your function to the on_pressed method (see below) 创建一个按钮对象并将函数调用添加到on_pressed方法(参见下文)

  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()

Try the 'GPIO.add_event_detect' functionality to define a function to be called when the input changes. 尝试使用'GPIO.add_event_detect'功能来定义输入更改时要调用的函数。 See https://pypi.python.org/pypi/RPi.GPIO/0.5.1a 请参阅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 初始化add_event_detect功能时要小心,因为如果您不确保此功能仅在一个进程中初始化一次,那么您将有多次调用该函数(每个进程一个) https://raspberrypi.stackexchange.com/问题/ 8584 /多GPIO加事件-检测-一个回调函数

Regards and good luck!! 问候,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM