Pin Handler with an inside "while" function start again and again !



  • Hello to all,
    Pytrack2 with Wipy3 ( all are firmware updated ).
    I use the Pin Handler ( set to 0 ) function an work fine! but start again and again, sometime not ?
    When I check the value of the pin I read "1" and the function start again like if I press the button !
    Is someone know the problem or have a suggestion ?
    Waiting your answers
    Best regards, Jiemde



  • @robert-hh Thanks for your reply.
    Yes "P10" is connected to the Gnd by a simple push button. But I had no idea that the system was logging an interrupt every time I went to Gnd.

    Question: what solution to adopt? - debouncing hardware or software?
    What is the solution adopted on the expansion board 3.1 for the "S1" button ?

    Indeed I only use uart reception. it works great, however i will heed your suggestion of external resistance.



  • @jcaron Thanks for reply,
    I've moved the assign of the pin handler outside the loop, but the result is the same!



  • @DEFRAINE-JM also, I wouldn’t re-assign the pin handler in the loop. Do it just once at startup and be done with it.



  • @DEFRAINE-JM Did you connect P10 to button switch? If yes, that switch will bounce and create multiple interrupts.

    On a side note: I would not create the UART object in the callback over and over again. You can create if beforehand and store the object in a global variable. You would have to declare that in the callback as global, otherwise it is not visible.
    Besides that, P16 and P17 are input only pins. You cannot use them for UART TX. But you do not write to UART in your code, so it might be fine. Better add an external pull-up resistor to the TX pin.



  • @jcaron Thanks for your reply, here is the code of the pin-handler code:

    def pin_handler(arg):
        print("got an interrupt in pin %s" %(arg.id()))
    
        pycom.rgbled(red)
        print("Sequence de lecture RFID !")
    
        rfid = UART(1, baudrate=9600, pins=('P16', 'P17'))
        rfid.init(baudrate=9600, bits=8, parity=None, stop=1, timeout_chars=2, pins=('P16', 'P17'), rx_buffer_size=2048)
    
    
        global uuid                 # = bytearray(16)
        rfid_power.value(1)         # Powering the RFID decoder via a power load
        time.sleep(0.5)
        uuid = rfid.read(17)        # first reading of the rfid decoder buffer for cleaning it
    
        
        time.sleep(0.5)             
        pycom.rgbled(orange)
               
    
        while True:
            uuid = rfid.read(17)     # reading of the buffer of the rfid buffer
            time.sleep(1)
            print('id = ', uuid)
    
            if uuid is not None:        # if buffer has something different than "None"
                global rfid_number
                print("   ID detected !")                    
                rfid_power.value(0)
                pycom.rgbled(green)     # led sequence for seeing that RFID number is detected
                time.sleep(0.1)
                pycom.rgbled(black)
                time.sleep(0.1)
                pycom.rgbled(green)
                time.sleep(0.1)
                pycom.rgbled(black)     
                time.sleep(0.1)
                pycom.rgbled(green)
                time.sleep(0.1)
                pycom.rgbled(black)
                time.sleep(0.1)
                pycom.rgbled(green)
                time.sleep(1.5)
                pycom.rgbled(black)
                rfid_number = uuid
                rfid.deinit()
                print(p_in.value())       # just to know the pin value for verification
                time.sleep(3)
                break                     # breaking the while function  
    

    And the while (True): start with the p_in callback ( P10 ):

    while (True):
    
        p_in = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP)
        p_in.callback(Pin.IRQ_RISING, pin_handler, arg = None)
    
    
        print(y)                                # comptage du nombre de transmission ( en minutes ) avant reset ( test )
        print("RFID", rfid_number[0:14])
    
    
        gps   = l76.coordinates()
        pitch = acc.pitch()
        accel = acc.acceleration()
        roll  = acc.roll()
        batt  = py.read_battery_voltage()
        rfid  = rfid_number[0:14]
        # TempA = sensor.read_ambiant_temp()
        # TempO = sensor.read_object_temp()
    
    

    I use an rfid decoder (134.2Khz ) with uart output RDM660 TTL

    Here is the terminal info:

    1
    got an interrupt in pin P10
    Sequence de lecture RFID !
    id =  None
    id =  b'967000009112695\r\n'
       ID detected !
    1
    got an interrupt in pin P10
    Sequence de lecture RFID !
    id =  None
    id =  b'967000009112695\r\n'
       ID detected !
    3
    RFID b'96700000911269'
    (None, None) - (2021, 11, 30, 10, 9, 33, 345068, None) - 2531328
    Gps:            (None, None)
    Acceleration:   (-0.01611328, -0.00793457, 0.9969482)
    Roll:           0.5656762
    Pitch:          0.4559399
    Batt Volt:      4.455878
    Rfid:           b'96700000911269'
    {"TIME": [2021, 11, 30, 10, 9, 33, 441458, null], "DATAS": {"ROLL": 0.5656762, "BATTERY": 4.455878, "ID": "96700000911269", "GPS": [null, null], "PITCH": 0.4559399, "ACCELERATION": [-0.01611328, -0.00793457, 0.9969482]}}
    1
    WIFI connected succesfully
    SENDING PAYLOAD to NODERED
    MQTTClient disconnected
    WIFI disconnected ! 
    
    got an interrupt in pin P10
    Sequence de lecture RFID !
    id =  None
    id =  b'967000009112695\r\n'
       ID detected !
    1
    got an interrupt in pin P10
    Sequence de lecture RFID !
    id =  None
    id =  b'967000009112695\r\n'
       ID detected !
    1
    

    "1" alone, is the value of the pin(P10).
    You can see that the rfid value is ok in the sequence so the while function must break !
    Sometime it break at the first occurence and sometime it need 8 occurences before breaking !

    Thanks in advance for helping.
    Best regards
    Jiemde



  • @DEFRAINE-JM You'll probably need to share (the relevant parts of) your code, along with any logs and traces and description of what you do.



Pycom on Twitter