MS-Robot
http://ms-robot.com/Forum/

Python: Enregistrement de niveau de batterie dans un fichier
http://ms-robot.com/Forum/viewtopic.php?f=11&t=105
Page 1 sur 1

Auteur :  jpoilux [ Sam Déc 07, 2013 5:41 pm ]
Sujet du message :  Python: Enregistrement de niveau de batterie dans un fichier

Voici maintenant comment créer un script qui relève toutes les minutes le niveau de la batterie (ou de l'alimentation) via l'entrée analogique 6 de la carte Blue Edition.

Ce script créé un fichier dans lequel, toute les minutes est écrit le niveau de la batterie en volts.

#!/usr/bin/python
# __name__ is the script running name
# __args__ is an array with the optional script parameters

from _api import * # do remove this line!!! it for import the OpenBot API !!!!
import datetime, time # import extra python libraries for date time

# here's to get a timestamps for making a log file
start = datetime.datetime.now()
timestamp = start.strftime("%Y%m%d%H%M%S")

# define location for log file
directory = '/home/pi/python/tmp'
filepath = directory + '/' + timestamp + '.txt'

# define delay in second, every each you want to log the measure.
check_each_secondes = 60

counter = 0
# here, loop while the script is not stopped by an API command
while self.script_thread[__name__] == True:

# check each second if it's time to read analogue value of the battery
counter -= 1
if counter <= 0:
counter = check_each_secondes

# read the battery level
battery_state = launch({"function":"i2c_analog.read", "pin":6})['result']
battery_state = float(battery_state / 10)

# prepared the message to write
message = str(battery_state) + " volts\n"

# prepared the time of read to save it in log file
now = datetime.datetime.now()
timestamp = now.strftime("%Y%m%d%H%M%S")

# create the log file if not existing and write message into it
if not os.path.isfile(filepath):
file = open(filepath, "w") # open a new log file
file.write(timestamp + " - " + message) # write the message into file
file.close() # close the file
os.chmod(filepath, 0o777) # give extra R/W right to the new log file

# if log file allready exist, append message into it
else:
file = open(filepath, "a+") # open a log file for appending data
file.write(timestamp + " - " + message) # write the message into file
file.close() # close the file

# loop each seconds to check if script must continue or stop
time.sleep(1)


le fichier complet est disponible via le lien ci-dessous:
http://www.ms-robot.com/%5Bdoc%5D/api/e ... onitor.rar

Merci à vous :)

Page 1 sur 1 Le fuseau horaire est UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/