program runs once and gives either an error or stop running(executes one part of a program,the other parts don't respond

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
KAG Tech0262
Posts: 10
Joined: Wed Aug 04, 2021 7:04 am

program runs once and gives either an error or stop running(executes one part of a program,the other parts don't respond

Post by KAG Tech0262 » Thu Aug 05, 2021 1:59 pm

The functionality of our program is ,when a sensor is triggered an SMS should be sent as an alarm notification while a feedback response on the shell should say "alarm is triggered" and the process should continuously run ,for in case a different sensor is triggered the same procedure should follow just like how the 1st sensor responded.

Code: Select all

import machine
from machine import UART, Pin
import utime
from time import sleep

sensor_adc1_pir = machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_DOWN)
sensor_adc2_pir2 = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN)
standdown_pir3 = machine.Pin(5, machine.Pin.IN, machine.Pin.PULL_UP)
sensor_dig1_pir4 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
sensor_dig2_pir5 = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
bluelock_pir6 = machine.Pin(6, machine.Pin.IN, machine.Pin.PULL_DOWN)
outputrelay = machine.Pin(4, machine.Pin.OUT)
test_led = machine.Pin(8, machine.Pin.OUT)
activate = machine.Pin(7, machine.Pin.OUT)

def read_all(gsm):
    c = b""
    while gsm.any():
        c += gsm.read(1)
        sleep(0.0007)
    print(c)

# Create the UART connection
gsm = UART(0, 9600, tx=Pin(0), rx=Pin(1))
sleep(0.5)
activate.on()

def pir_handler(pin):
    if pin.value():
        if pin is sensor_adc1_pir:
            print("Alarm! sensor_adc1!")
            test_led.value(1)
            utime.sleep(0.2)
            test_led.value(0)
        elif pin is sensor_adc2_pir2:
            print("Alarm! sensor_adc2!")
            test_led.value(1)
            gsm.write('AT\r\n')
            sleep(1)
            read_all(gsm)
            gsm.write('AT+CSQ\r\n')
            sleep(1)
            read_all(gsm)
            gsm.write('AT+CMGF=1\r\n')
            sleep(1)
            read_all(gsm)
            gsm.write('AT+CMGS="+2768953314562549"\r\nintruder site has been accesed\r\n')
            sleep(1)
            read_all(gsm)
            gsm.write('\x1A')
            sleep(1)
            read_all(gsm)
            utime.sleep(0.2)
            test_led.value(0)
            utime.sleep(0.2)
            test_led.value(0)
        elif pin is sensor_dig1_pir4:
            print("Alarm! sensor_dig1!")
            test_led.value(1)
            utime.sleep(0.2)
            test_led.value(0)
        elif pin is sensor_dig2_pir5:
            print("Alarm! sensor_dig2!")
            gsm.write('AT\r\n')
            sleep(1)
            read_all(gsm)
            gsm.write('AT+CSQ\r\n')
            sleep(1)
            read_all(gsm)
            gsm.write('AT+CMGF=1\r\n')
            sleep(1)
            read_all(gsm)
            gsm.write('AT+CMGS="+276621463611502"\r\nintruder site has been accesed\r\n')
            sleep(1)
            read_all(gsm)
            gsm.write('\x1A')
            sleep(1)
            read_all(gsm)
            test_led.value(1)
            utime.sleep(5)
            test_led.value(0)
            
    elif bluelock_pir6.value() == 0:
        print("bluelock access!")
        outputrelay.value(1)
        gsm.write('AT\r\n')
        sleep(1)
        read_all(gsm)
        gsm.write('AT+CSQ\r\n')
        sleep(1)
        read_all(gsm)
        gsm.write('AT+CMGF=1\r\n')
        sleep(1)
        read_all(gsm)
        gsm.write('AT+CMGS="+2756631452398715"\r\ndoor opened\r\n')
        sleep(1)
        read_all(gsm)
        gsm.write('\x1A')
        sleep(1)
        read_all(gsm)
        utime.sleep(20)
        outputrelay.value(0)
        utime.sleep(0)
        sensor_adc2_pir2.value(0)
        utime.sleep(10)
        sensor_adc1_pir.value(0)
        utime.sleep(10)
        sensor_dig1_pir4.value(0)
        utime.sleep(10)
        sensor_dig2_pir5.value(0)
        utime.sleep(10)
        test_led.value(1)
        utime.sleep(0.2)
        test_led.value(0)
    
    elif standdown_pir3.value() == 0:
        print("door closed!")
        test_led.value(1)
        gsm.write('AT\r\n')
        sleep(1)
        read_all(gsm)

        gsm.write('AT+CSQ\r\n')
        sleep(1)
        read_all(gsm)

        gsm.write('AT+CMGF=1\r\n')
        sleep(1)
        read_all(gsm)

        gsm.write('AT+CMGS="+278626321245695"\r\nDOOR CLOSED\r\n')
        sleep(1)
        read_all(gsm)

        gsm.write('\x1A')
        sleep(1)
        read_all(gsm)
        utime.sleep(10)
        test_led.value(0)
        
sensor_adc1_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
sensor_adc2_pir2.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
sensor_dig1_pir4.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
sensor_dig2_pir5.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
standdown_pir3.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
bluelock_pir6.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: program runs once and gives either an error or stop running(executes one part of a program,the other parts don't res

Post by davef » Thu Aug 05, 2021 7:53 pm

Tell us what error messages you get and where the program stops by showing a list of print statements that you see.

As a guess can you test for any return values from gsm.write()?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: program runs once and gives either an error or stop running(executes one part of a program,the other parts don't res

Post by jimmo » Fri Aug 06, 2021 2:33 am

Generally it's a bad idea to do work inside an IRQ handler as they should complete as fast as possible. Especially sleeping is problematic.

Your IRQ handler should just set a flag indicating what condition has happened, then your main program can inspect these flags (in a loop) and decide what to do.

As davef said, more information about what's going wrong would be helpful.

KAG Tech0262
Posts: 10
Joined: Wed Aug 04, 2021 7:04 am

Re: program runs once and gives either an error or stop running(executes one part of a program,the other parts don't res

Post by KAG Tech0262 » Fri Aug 06, 2021 7:43 am

davef wrote:
Thu Aug 05, 2021 7:53 pm
Tell us what error messages you get and where the program stops by showing a list of print statements that you see.

As a guess can you test for any return values from gsm.write()?
this is the response we get below(code),
so just to summarise the whole issue , we are not seeing any further 'print' output nor receiving the SMS.
*what is happening is we triggered the "elif bluelock_pir6.value==0 and the results are:
+we received a print output "bluelock access"
+received an SMS "door opened"
this whole cycle takes 40 seconds to complete and during the cycle every sensor is on 0 as well as the standown_pir3.value ,and after the 40 seconds an LED will blink with a utime.sleep(0.1) the when it switches off all sensors and standown will be reactivated.

so after we succeeded with the bluelock_pir6 cycle we tried to execute the standown_pir3 cycle ,and no print output nor SMS feedback was received. this goes for the sensors too.

Code: Select all

Alarm! sensor_dig2!
bluelock access!
b'AT\r\r\nOK\r\n'
b'AT+CSQ\r\r\n+CSQ: 14,0\r\n\r\nOK\r\n'
b'AT+CMGF=1\r\r\nOK\r\n'
b'AT+CMGS="+27681703229"\rDOOR OPENED\r\r\n> '
b''

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: program runs once and gives either an error or stop running(executes one part of a program,the other parts don't res

Post by davef » Fri Aug 06, 2021 8:48 am

so after we succeeded with the bluelock_pir6 cycle
As a series of test cases can you run each one of the 6 IRQs by themselves successfully?

Is it when you try to do a 2nd IRQ case, I assume without re-booting, then it will not run?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: program runs once and gives either an error or stop running(executes one part of a program,the other parts don't res

Post by pythoncoder » Fri Aug 06, 2021 8:51 am

I would not use IRQs for this: they are really for situations where you need a microsecond level response. I would use uasyncio, and create a task for each sensor which repeatedly polls the device. Programming with interrupts requires some know-how and attention to detail.
Peter Hinch
Index to my micropython libraries.

Post Reply