Compteur de temps de fonctionnement

Vous venez de déballer votre Raspberry Pi et vous vous posez des questions ? C'est ici que ça se passe !

Modérateur : Francois

Avatar du membre
Manfraid
Raspinaute
Messages : 1402
Enregistré le : ven. 3 oct. 2014 14:50
Contact :

Re: Compteur de temps de fonctionnement

Message par Manfraid » dim. 14 juin 2015 10:26

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()
NAS : DIY OS Debian: DD250Go + 3x2To + 6To
Raspberry pi : 2B OS : Raspbian
Se tromper est humain, Vraiment foutre la merde nécessite le mot de passe de root.

Répondre

Retourner vers « Débutants »