bonjour
voici mon script python pour le comptage du temps de fonctionnement de ma chaudière fioul
et conversion du temps en litre
envoyer sur des sensors dymmy texte pour les litres et horaire et inter on/off pour indiquer le marche arrêt
détection sur GPIO 17
à modifier selon votre usage
dommage qu'il y a rien de prévu dans domoticz pour cette usage ce serai mieux que des dummy texte
Code : Tout sélectionner
#!/usr/bin/env python2.7
# Comptage horaire + conso chaudiere fioul
# script by Asem
# Les imports
import urllib2
import math
import os
import json
from time import time
from time import sleep
import RPi.GPIO as GPIO
# Config GPIO RPI
GPIO.setmode(GPIO.BCM) # utilisation de numero BCM GPIO
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP) # config du GPIO 17 en input
# Config a parametrer
G_galUS = 0.40 # calibre de la buse en Galon US par heure
galUS = 3.78541178 # 1 litre = 3.78541178 galon
NomFichier = '/home/pi/aprog/fioul.txt' #chemin du fichier totaliseur
# Les variables
tmarche = 0 #timer marche
tarret = 0 #timer arret
tdiff = 0 #difference entre marche et arret
ttotal = 0 #temp total
litreSeconde = ((galUS * G_galUS) / (60*60) ) #calcul de la consommation a la sec
consoCycle = 0 #consommation du dernier cycle
consoTotal = 0 #consommation total
#Fonction convertion num string vers int ou float
def num2(s):
try:
return int(s)
except ValueError:
s = float(s)
return s
# Fonction mise a jour info domoticz
def udateDCZ(cmdDCZ):
try:
urlwatt = urllib2.urlopen(cmdDCZ)
sleep(0.05)
except urllib2.URLError, (err):
print "URL error(%s)" % (err)
# Fonction lecture du fichier
def lectureFichier():
global NomFichier
Fichier = open(NomFichier,'r')
valeurLu = Fichier.read()
Fichier.close()
valeurLu = num2(valeurLu)
return valeurLu
# Fonction Ecriture dans fichier
def ecritureFichier(valeurAecrire):
global NomFichier
valeurAecrire = str(valeurAecrire)
Fichier = open(NomFichier,'w')
Fichier.write(valeurAecrire)
Fichier.close()
# Fonction lors d'un evenement
def my_callback(channel):
global tmarche, tarret, tdiff, ttotal, litreSeconde, consoCycle, consoTotal, NomFichier
if GPIO.input(17): # si port xx == 1
# Timer fin marche et calcul de la difference
tarret = time()
tdiff = tarret - tmarche
tdiff = round(tdiff, 1)
# Lecture du fichier contenant le dernier total
ttotal = lectureFichier()
# Mise a jour du total
ttotal += tdiff
ttotal = round(ttotal, 1)
# Calcul des litre consommer par rapport au temp de marche
consoCycle = litreSeconde * tdiff
consoTotal = litreSeconde * ttotal
# Affichage pour le debug
#print "____ARRET___"
#print "Cycle: " + str(tdiff)
#print "Total: " + str(ttotal)
#print "conso Cycle: " + str(round(consoCycle,5))
#print "conso Total: " + str(round(consoTotal,5))
# Ecriture du nouveau total dans le fichier
ecritureFichier(ttotal)
# Mise a jour modul dummy text Horaire
majwatt = "http://192.168.1.118:8080/json.htm?type=command¶m=udevice&idx=12&nvalue=0&svalue="+str(ttotal)
udateDCZ(majwatt)
# Mise a jour modul dummy test Litre
majwatt = "http://192.168.1.118:8080/json.htm?type=command¶m=udevice&idx=11&nvalue=0&svalue="+str(consoTotal)
udateDCZ(majwatt)
# Mise a jour modul dummy chaudiere Arret
majCH = "http://192.168.1.118:8080/json.htm?type=command¶m=switchlight&idx=10&switchcmd=Off"
udateDCZ(majCH)
sleep(0.01)
else: # si non port xx != 1
# Timer debut marche
tmarche = time()
# Affichage pour le debug
#print "____MARCHE___"
# Mise a jour modul dummy chaudiere marche
majCH = "http://192.168.1.118:8080/json.htm?type=command¶m=switchlight&idx=10&switchcmd=On"
udateDCZ(majCH)
# /Fin fonction evenement
# Attente evenement sur port BCM 17 et appel appel de fonction
GPIO.add_event_detect(17, GPIO.BOTH, callback=my_callback, bouncetime=1000)
# Tourne en boucle
while True:
sleep(1);
il y a surement des amélioration à faire si un pythonien passe par là

RPI B+ & B2 | Domoticz | 1-wire | Danfoss | Synology | arduino | esp8266 | modbus | camera | IPXv4