from machine import Pin
from machine import Timer
from micropython import const
from micropython import schedule
import _thread
import dht
import line
import time
DHT_PIN = const(15)
TICK = const(60)
class Climate():
def __init__(self, dht_gpio=DHT_PIN, tick=TICK, callback=None, expire=TICK):
self.dht = dht.DHT11(Pin(dht_gpio))
self.tick = tick
self.lock = _thread.allocate_lock()
self.timer = Timer(period=tick*1000, mode=Timer.PERIODIC, callback=self._timer_handler)
self._humidity = 0
self._temperature = 0
self._measurement_dt = 0
self.measure()
self._callback_func = callback
self._expire = expire
self._counter = 0
def set_callback(self, callback, expire):
self._callback_func = callback
self._expire = expire
def measure(self, arg=0):
self.dht.measure()
self.lock.acquire()
self._humidity = self.dht.humidity()
self._temperature = self.dht.temperature()
self._measurement_dt = time.time()
self.lock.release()
def _timer_handler(self,t):
schedule(self.measure, 0)
self._counter -= 1
if self._counter <= 0:
self._counter = self._expire
if self._callback_func is not None:
schedule(self._callback_func, self)
@property
def temp(self):
self.lock.acquire()
rval = self._temperature
self.lock.release()
return rval
@property
def hum(self):
self.lock.acquire()
rval = self._humidity
self.lock.release()
return rval
@property
def measurement_date_time(self):
self.lock.acquire()
rval = self._measurement_dt
self.lock.release()
return rval
def mycalback(c):
tm = time.localtime(c.measurement_date_time)
line.notify("temp=%d,hum=%d@%4d/%02d/%02d_%02d:%02d:%02d" % (c.temp, c.hum, tm[0],tm[1],tm[2],tm[3],tm[4],tm[5]))
if __name__ == '__main__':
cl = Climate(callback = mycalback)
while(True):
machine.idle()