#!/usr/bin/python # -*- coding: utf-8 -*- # Pour lire le .ini import configparser import json import subprocess # Pour faire des sleep import time # Pour la date from datetime import datetime # Pour le debug import pprint import csv import os.path import random # ~ import RPi.GPIO as GPIO # ~ GPIO.setmode(GPIO.BCM) # ~ gpio_number = 25 # ~ GPIO.setup(gpio_number, GPIO.OUT) # ~ print("GPIO no " + str(gpio_number) + ": " + str(GPIO.input(gpio_number))) # On charge le config.ini config = configparser.RawConfigParser() # On créé un nouvel objet "config" config.read('config.ini') # On lit le fichier de paramètres csvfile=config.get('general', 'csv_dir').strip('"') + '/' + time.strftime ('%Y-%m-%d_%H-%M') + '.csv' if (os.path.exists(csvfile)) : csvfile=config.get('general', 'csv_dir').strip('"') + '/' + time.strftime ('%Y-%m-%d_%H-%M') + str(random.randint(1, 100)) + '.csv' print('#############################################################################') print('######## Banc AFPMA ECS / Aquisition des mesures') print('#############################################################################') print() print('La configuration (config.ini) détermine : ') print(' * que le fichier csv de relevé est : ',csvfile) print(' * que l\'interval entre 2 mesures sera de ', int(config.get('general', 'delai')), 'secondes') print() # Init colonne CSV with open(csvfile, 'w', newline='') as fichier: # on déclare un objet writer ecrivain = csv.writer(fichier) # écrire une ligne dans le fichier: ecrivain.writerow(['T', 'DATE', 'ENERGY_WH', 'VOLUME_FLOW', 'POWER_W', 'FLOW_TEMPERATURE', 'RETURN_TEMPERATURE']) fin = False now = datetime.now() chronoDepart=datetime.timestamp(now) prochainReleve=chronoDepart while fin == False: now = datetime.now() nowTimestamp=datetime.timestamp(now) if (nowTimestamp >= prochainReleve): print("Relevé " + str(now)) prochainReleve=nowTimestamp+float(config.get('general', 'delai')) dt_string = now.strftime("%d/%m/%Y %H:%M:%S") temps_sec=round(datetime.timestamp(now)-chronoDepart, 0) data = [temps_sec, dt_string] print('T='+str(temps_sec), end=" ") print('DATE='+str(dt_string), end=" ") ## Récupérer les données : result = subprocess.Popen(["/usr/bin/python3", "/opt/banc/mbus-serial-request-data.py", "/dev/ttyAMA0"], stdout=subprocess.PIPE) result_request_data = result.communicate()[0] return_code = result.returncode try: #convert string to object request_data_object = json.loads(result_request_data) except ValueError: return_code = 255 if (return_code == 0): ## Parse des données for records in request_data_object['body']['records']: if (records['type'] == 'VIFUnit.ENERGY_WH'): try: if (records['device'] != None): # On ne récupère pas si le device est rensigné pass except KeyError as ke: print('ENERGY_WH='+str(round(records['value'], 2)), end=" ") data.append(round(records['value'], 2)) elif (records['type'] == 'VIFUnit.VOLUME_FLOW'): print('VOLUME_FLOW='+str(round(records['value'], 2)), end=" ") data.append(round(records['value'], 2)) elif (records['type'] == 'VIFUnit.POWER_W'): print('POWER_W='+str(round(records['value'], 2)), end=" ") data.append(round(records['value'], 2)) elif (records['type'] == 'VIFUnit.FLOW_TEMPERATURE'): print('FLOW_TEMPERATURE='+str(round(records['value'], 2)), end=" ") data.append(round(records['value'], 2)) elif (records['type'] == 'VIFUnit.RETURN_TEMPERATURE'): print('RETURN_TEMPERATURE='+str(round(records['value'], 2)), end=" ") data.append(round(records['value'], 2)) print(""); else: print('Erreur : aucune communicatoin avec le compteur d\'énergie') data.append('Erreur') data.append('Erreur') data.append('Erreur') data.append('Erreur') data.append('Erreur') # ouverture en écriture (w, première lettre de write) d'un fichier with open(csvfile, 'a', newline='') as fichier: # on déclare un objet writer ecrivain = csv.writer(fichier) # écrire une ligne dans le fichier: ecrivain.writerow(data) print('... Fin de la mesure, on patiente', int(config.get('general', 'delai')), 's') print('... Pour quiter, appuyer sur CTRL + C') print(""); time.sleep(float(config.get('general', 'sleep')))