使用urllib.urlopen调用两次函数

时间:2018-03-03 05:43:04

标签: python function

我在我的覆盆子的特定GPIO上有一小段代码检测事件。当检测到事件时,会调用一个名为increase_counter的函数,如果在函数内部我只保留“print('Flash ...')”我每个事件只有一个打印,但如果我用urllib2调用一个url整个increase_counter执行两次,所以我将计算2次闪烁。

import RPi.GPIO as GPIO
import time
import urllib2,json

def increase_counter(ch):
    print('Flash ... ')
    requete='http://192.168.1.20:8080/json.htm?type=command&param=udevice&idx=52&svalue=1'
    response=urllib2.urlopen(requete)
    data = json.loads(response.read())
    print data

IRPIN = 4


GPIO.setmode(GPIO.BCM) #GPIO SETUP
GPIO.setup(IRPIN,GPIO.IN) #GPIO SETUP

GPIO.add_event_detect(IRPIN, GPIO.FALLING, callback=increase_counter, bouncetime=3) #Calling event when flash is detected

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print(' Stopped !!!')
except:
    print(' Stopped !!!')
    raise
finally:
    GPIO.remove_event_detect(IRPIN)
    GPIO.cleanup(IRPIN) # GPIO cleanup

0 个答案:

没有答案