proyectos:monitorclima_raspberry
#!/usr/bin/python3 import smbus import datetime import time import sys from firebase import firebase #Para conexion con firebase import urllib3 #Para conexion con thingspeak import sqlite3 # Definimos la comunicacion con el arduino bus = smbus.SMBus(1) address = 0x04 cantidad=4 #Cantidad de bytes que vamos a leer datos=[0]*cantidad #Inicializamos el array donde guardaremos los datos #store the Host ID(provided in firebase database) in variable where you want to send the real time sensor data. firebase= firebase.FirebaseApplication('https://monitorinv-57616.firebaseio.com/') #Parar thingspeak tsAPI = 'CBF1XMKC01QMDZJH' # URL a donde se enviaran los datos. baseURL = 'https://api.thingspeak.com/update?api_key=%s' % tsAPI time.sleep(2) while True: try: #Leemos los datos desde el ardino #I2C for i in range(0, cantidad): datos[i]= bus.read_byte(address) print (datos) #Guardamos el timestamp, y asi lo guardamos Para obtener un objeto de hora a partir del string hacemos: #prueba=datetime.datetime.strptime("2019-03-30 15:58:00.623603",'%Y-%m-%d %H:%M:%S.%f') #https://stackabuse.com/converting-strings-to-datetime-in-python/ now = datetime.datetime.now() #print ("Fecha y hora de lectura: %s" % now.strftime("%c") ) #Convertimos los datos a flotante, ya que recibimos dos enteros(0-255. cTemp=datos[0]+float(datos[1])/100 humidity=datos[2]+float(datos[3])/100 #print (cTemp) #Enviamos los resutados a firebase #store the readings in variable and convert it into string and using firbase.post then data will be posted to databse of firebase result = firebase.post('monitorinv', {'cTemp':str(cTemp), 'humidity':str(humidity),'time':str(now.strftime("%Y%m%d%H%M%S"))}) print(result) #Realizamos la conexion con ThingSpeak http = urllib3.PoolManager() url=baseURL + '&field1=%s&field2=%s&field3=%s' % (cTemp, humidity,now.strftime("%Y%m%d%H%M%S")) print(url) response = http.request('GET',url) print (response) time.sleep(5) #Esperamos 5 segundos para la siguiente lectura except Exception as e: continue print(e) time.sleep(10);
proyectos/monitorclima_raspberry.txt · Última modificación: por manuel.floresv