PIR interrupt and send
Posted: Thu Jul 22, 2021 3:40 am
Hello, I am novice to micropython as well as to programming. I have to write a program with esp32 where it connects to dht sensor and a pir sensor, then sends data to asksensor. The dht data is updated every 10 minutes while PIR should send data as soon as motion is detected(interrupt) and set to zero after a while. I came up with this code but somehow I feel it's not optimized as asksensor first shows on/off correctly then just stays off. All required libraries imported correctly and connection to asksensor also avialable.
Code: Select all
def handle_interrupt(pin):
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
UPDATE_TIME_INTERVAL = 600000 # in ms
last_update = time.ticks_ms()
motion = 0
while True:
if time.ticks_ms() - last_update >= UPDATE_TIME_INTERVAL:
dht1.measure()
temp=dht1.temperature
hum=dht1.humidity
request = urequests.get( 'https://api.asksensors.com/write/'+'APIkey'+'module1='+str(dht_temp)+'&module2='++str(hum))
request.close()
last_update = time.ticks_ms()
elif motion:
print('motion detected')
motion_start= time.ticks_ms()
motion_time = 1000
motion = 1
request = urequests.get( 'https://api.asksensors.com/write/'+'APIkey'+'module3='+str(motion))
request.close()
if time.ticks_ms() - motion_start >= motion_time:
motion = 0
request = urequests.get( 'https://api.asksensors.com/write/'+'APIkey'+'module3='+str(motion))
request.close()