Python和gpio覆盆子

时间:2014-03-25 17:09:49

标签: python raspberry-pi gpio

我的项目有问题,我有一个小蟒蛇脚本来读取我的燃气表。 每一次数字旋转,靠近簧片开关的燃气表内的一个小磁铁为我的覆盆子提供输入。问题是当磁铁靠近传感器电路停在高水平时,会导致误输入,这是我的脚本。 有什么建议吗?感谢所有

#!/usr/bin/env python
import time
import datetime
import os.path
import pycurl
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.add_event_detect(10, GPIO.RISING, bouncetime = 5000)

def GetGas(arg):
    time.sleep(0.01)         # need to filter out the false positive of some power fluctuation
    if GPIO.input(10) != GPIO.HIGH:
        return

    gastotale=0
    if os.path.isfile('/var/www/myscripts/gas/gastotale.txt'):
        file = open("/var/www/myscripts/gas/gastotale.txt","r")
        gastotale = float(file.read())
        file.close()
    gastotale = gastotale+0.01

    file = open("/var/www/myscripts/gas/gastotale.txt","w")
    file.write(str(gastotale))
    file.close()

    now = datetime.datetime.now()
    fileday = '/var/www/myscripts/gas/'+now.strftime("%Y-%m-%d")+'.txt'
    gasday = 0

    if os.path.isfile(fileday):
        file = open(fileday,"r")
        gasday = float(file.read())
        file.close()
    gasday = gasday+0.01

    file = open(fileday,"w")
    file.write(str(gasday))
    file.close()

    oem = pycurl.Curl()
    oem.setopt(oem.URL, 'http://emoncms.org/input/post.json?node=0
    csv=0,'+str(gasday)+',0,0,'+str(gastotale)+'&apikey=dfb6ccf')
    oem.perform()

laststate = 0
while True:
    if (GPIO.input(10) == 1 ):
        if (laststate == 0):
            gastotale = gastotale+0.01
            laststate=1
    else:
       laststate=0
    time.sleep(30)

0 个答案:

没有答案