【问题标题】:Disable GPIO by default and temporarily activate it via python script默认禁用GPIO,通过python脚本临时激活
【发布时间】:2016-06-09 09:11:59
【问题描述】:

我正在使用带有 DietPi 的 Raspberry Pi 2,并将警报模块插入 GPIO 14(引脚 08)。

我现在想默认禁用此 Pin 图,只允许它在我的 python 脚本中临时打开。

到目前为止,我已经尝试过使用 RPi.GPIO,但它不起作用并取消导出/导出,但这最终只会将我的操作系统损坏到我不得不重新安装它的程度。

显然我做错了什么。有没有人有这方面的经验并且可以指出我正确的方向?

【问题讨论】:

  • 您对“禁用” GPIO 有何期望?你没有明确定义问题。这个 GPIO 是用于输入还是输出?如果连接了一个设备并且 GPIO 用作输出,那么“禁用” GPIO 是错误的做法。 IE。您允许输出处于未定义状态,因此失去了对设备的控制。

标签: python raspberry-pi raspbian raspberry-pi2 gpio


【解决方案1】:

我没有 rpi,但您可以尝试使用它。 第一个函数导出,3个后续检查时间

import time
import sys

def unexport(gpio):
  value = open("/sys/class/gpio/unexport","w")
  value.write(gpio)
  value.close()

def checkHours:
    return int(time.strftime("%l"))

def checkMinutes:
    return int(time.strftime("%M"))

def checkAMorPM:
    return time.strftime("%p")

def disableGpio3AM46:
    if (checkHours == 3) and (checkMinutes == 46) and (checkAMorPM == 'PM'):
        print 'amazing'
        unexport(gpio)

【讨论】:

    【解决方案2】:

    自从发布这个问题以来,我已经想出了通过 python 禁用和启用 pin/gpio 的方法:

    import RPi.GPIO as GPIO
    import time
    
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(8, GPIO.OUT)
    
    try:
        while True:
        GPIO.output(8,1)
        time.sleep(0.5)
        GPIO.output(8,0)
        time.sleep(0.5)
    
    except KeyboardInterrupt:
        GPIO.cleanup()
        print "Bye"
    

    我现在要弄清楚的是如何默认禁用 pin,以便在代码运行之前不会播放警报

    【讨论】:

      猜你喜欢
      • 2019-08-25
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 2011-10-20
      相关资源
      最近更新 更多