For that I am building a system which monitor green house environment temperature and humidity(stemp, shum) and if it falls below the specific values ( itemp, ihum) then it send a signal to start water pump.
I am using 4 push buttons to update temperature and humidity ( itemp up / down , ihum up / down )
Using internal timer to update environment temperature and display on OLED dsiplay, it's updating temperature and humidity perfectly, no problem at all in update in that code.
the problem I am facing is this code is not handling push button interrupt properly. Sometimes it is updated immediately, sometimes it get updated after 5-6 attempt.
How can I improve this code in such a way that it handle external interrupt properly.
Thank You.
Code: Select all
import config
import dht
import machine
import ssd1306
import time
from machine import Pin
d = dht.DHT22(machine.Pin(12))
i2c = machine.I2C(-1,machine.Pin(5), machine.Pin(4))
oled = ssd1306.SSD1306_I2C(128,32,i2c)
p13 = machine.Pin(13, Pin.OUT)
timer = machine.Timer(-1)
itemp=config.temp
ihum=config.hum
d.measure()
stemp=d.temperature()
shum=d.humidity()
def inctmp(p):
global itemp
if ( itemp < 40):
itemp = itemp + 1
def dectmp(p):
global itemp
if ( itemp > 30):
itemp = itemp - 1
p0 = Pin(0, Pin.IN)
p0.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=inctmp)
p14 = Pin(14, Pin.IN)
p14.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=dectmp)
def new_thz(timer):
time.sleep(2)
d.measure()
stemp=d.temperature()
shum=d.humidity()
td=int(itemp-stemp)
hd=int(ihum-shum)
a="Sensor " + str(stemp) + " " + str(shum)
c="Target " + str(itemp) + " " + str(ihum)
diff="T -> "+ str(td) + " H -> " + str(hd)
oled.fill(0)
oled.text(a,0,0)
oled.text(c,0,10)
oled.text(diff,0,20)
oled.show()
timer.init(period=1000, mode=machine.Timer.PERIODIC, callback=new_thz)
def gen_pwm():
while(stemp != (itemp + 1)):
p13.value(1)
p13.value(0)
while True:
time.sleep(1)
if((itemp-stemp) > 0):
gen_pwm()