Interrupt Handling problem

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Nikunj_Vadher
Posts: 8
Joined: Mon Dec 10, 2018 6:37 am

Interrupt Handling problem

Post by Nikunj_Vadher » Tue Jan 15, 2019 7:21 am

I am having a orchid green house and they need humidity and controlled temperature.

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()
 
Last edited by Nikunj_Vadher on Tue Jan 22, 2019 4:47 am, edited 1 time in total.

ThomasChr
Posts: 32
Joined: Sat Nov 25, 2017 7:50 am

Re: Interrupt Handling problem

Post by ThomasChr » Wed Jan 16, 2019 2:10 pm

Maybe you should try RISING or FALING.
Also you could try "hard = True" to use a hard interrupt instead of a soft one.
And may sure that your Button are properly pulled low (or high).

And use Code-Tags next time you're posting code in a forum. Makes it easier to read!

Thomas

tylersuard
Posts: 6
Joined: Mon Jan 21, 2019 4:09 pm

Re: Interrupt Handling problem

Post by tylersuard » Mon Jan 21, 2019 4:19 pm

This looks like a job for debouncing :)

The problem when you press a button is, in the fraction of a second it takes for you to press it down and let it go, the board can perform thousands of operations, so it gets kinda confused when it receives and input so slowly. You can solve that by forcing it to slow down a bit:

import machine
import time
button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
first = button.value()
time.sleep(0.01)
second = button.value()
if first and not second:
print('Button pressed!')
elif not first and second:
print('Button released!')

Hope that helps :)

Post Reply