Page 3 sur 3

Re: Compteur de temps de fonctionnement

Posté : dim. 14 juin 2015 10:26
par Manfraid
Salut,

désolé main je n'avais pas eu le temps de regarder ce qui était possible

voici une nouvelle version

Code : Tout sélectionner

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
Created on Thu May 14 20:36:01 2015

@author: jerome
"""


from time import time, sleep       # pour gestion du temps
import RPi.GPIO as GPIO     # gestion IO


btreset = 17         # entrée pour reset compteur horaire
infomarche = 18      # entrée pour comptage horaire

# a adapter selon besoin
GPIO.setmode(GPIO.BCM)
GPIO.setup(btreset, GPIO.INPUT)
GPIO.setup(infomarche, GPIO.INPUT)

total = 0           # total horaire en seconde
tstart = 0




def detect(channel):
    global tstart, total
    if GPIO.input(channel,GPIO.LOW):
        total += time() - tstart
        print("{} heure(s) de fonctionnement.".format(total))
        tstart = 0
    else:
        tstart = time()

def reset(channel):
    global total
    total = 0


GPIO.add_event_detect(infomarche, GPIO.BOTH, callback=start, bouncetime=200)
GPIO.add_event_detect(btreset, GPIO.RISING, callback=reset, bouncetime=200)

try:
    while True:
        sleep(0.1)
except KeyboardInterrupt:
    GPIO.remove_event_detect(btreset)
    GPIO.remove_event_detect(infomarche)
    GPIO.cleanup()