"""
mdb : Lecturas de sensor permanentes
Revisar: https://forums.raspberrypi.com/viewtopic.php?t=342045
"""
from time import sleep
from machine import Pin, I2C, Timer
import _thread
BME280 = False
BMP085 = False
DHT22 = True
"""
assert not (BME280 and BMP085)
assert (BME280 or BMP085)
"""
if BME280:
print("importar BME280")
import bme280
if BMP085:
print("importar BMP085")
# https://github.com/robert-hh/BMP085_BMP180
import bmp085
if DHT22:
print("importar DHT22")
import dht
#initialize I2C
print("agregar la inicializacion de i2c")
class Sensor:
#periodically read sensor data#
def __init__(self, period=5000, callback = None):
if BME280:
self.bme = bme280.BME280()
if BMP085:
self.bmp = bmp085.BMP085()
if DHT22:
self.DHT = dht.DHT22(Pin(15))
timerDHT = Timer(1)
timerDHT.init(period=period, mode=Timer.PERIODIC, callback=self._process_sensor)
self.callback = callback
def _process_sensor( self, t):
self._read_sensor()
if self.callback != None:
self.callback( temp = self.temp, pressure=self.pressure, humidity=self.humidity)
def _read_sensor(self):
global temp, pressure, humidity
if BME280:
values = self.bme.values
self.temp = values[0]
self.pressure = values[1]
self.humidity = values[2]
if BMP085:
self.temp = self.bmp.temperature
self.pressure = self.bmp.pressure
self.humidity = 0.0
if DHT22:
self.temp = self.DHT.temperature()
self.pressure = 0.9
self.humidity = self.DHT.humidity()
#ensure that writing, reading of 'reading'-variable are synchronized by a lock
reading = "undefined"
lock = _thread.allocate_lock()
def get_reading():
#synchronized with a lock#
with lock:
return reading # here a clone operation would be perfect.
def callback(temp, pressure, humidity):
#synchronized with a lock, partially
print('Temperatura=',temp, 'Presion=',pressure, 'Humedad=',humidity)
global reading
with lock:
reading = f"Temperature: { temp }, Humidity: {humidity}, Pressure: {pressure}"
#
# display on ssd1306
#
# ...
# ...
print('Instancia Sensor')
sensor = Sensor(period=3_333, callback=callback)