-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdht_log.py
More file actions
executable file
·62 lines (50 loc) · 2.03 KB
/
dht_log.py
File metadata and controls
executable file
·62 lines (50 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python3
# Internet de las Cosas - http://internetdelascosas.cl
#
# Descripcion : Programa que permite obtener la lectura de un sensor DHTxx y escribe los datos en un archivo log
# Lenguaje : Python version 3
# Autor : Jose Zorrilla <jzorrilla@iot.cl>
# Dependencias : Libreria Circuit Python de Adafruit https://github.com/adafruit/Adafruit_CircuitPython_DHT
# Web : https://www.internetdelascosas.cl/2017/05/19/raspberry-pi-conectando-un-sensor-de-temperatura-y-humedad-dht11/
# Importa las librerias necesarias
import time
import datetime
import adafruit_dht
# Log file
log_path = "/var/log/iot/"
# Configuracion del puerto GPIO al cual esta conectado (GPIO 23)
pin = 23
# Crea el objeto para acceder al sensor
# se debe descomentar la linea dependiendo del tipo de sensor (DHT11 o DHT22)
#sensor = adafruit_dht.DHT11(pin)
sensor = adafruit_dht.DHT22(pin)
# Funcion: Escribe un archivo log en log_path con el nombre en el formato yyyy-mm-dd_dht.log
def write_log(text):
log = open(log_path + datetime.datetime.now().strftime("%Y-%m-%d") + "_dht.log","a")
line = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + text + "\n"
log.write(line)
log.close()
# Funcion principal
def main():
# Ciclo principal infinito
while True:
# Intenta ejecutar las siguientes instrucciones, si falla va a la instruccion except
try:
# Obtiene la humedad y la temperatura desde el sensor
humedad = sensor.humidity
temperatura = sensor.temperature
# Si obtiene una lectura del sensor la registra en el archivo log
if humedad is not None and temperatura is not None:
write_log("DHT Sensor - Temperatura: %s" % str(temperatura))
write_log("DHT Sensor - Humedad: %s" % str(humedad))
else:
write_log('Error al obtener la lectura del sensor')
# Se ejecuta en caso de que falle alguna instruccion dentro del try
except RuntimeError as error:
# Imprime en pantalla el error
print(error.args[0])
# Duerme 10 segundos
time.sleep(10)
# Llama a la funcion principal
if __name__ == "__main__":
main()