micropythonpycom

micropython: loading data from a file in function causing an endless loop


I am working on a lopy4 from pycom and I have encountered an od problem while loading config data from a txt file:

def loadFromConfigFile():
    f= open('config.txt')
    for line in f:
        if "uuidExpected" in line:
            uuidExpected=line[13:len(line)-1].strip()
        elif "app_eui" in line:
            app_eui = ubinascii.unhexlify((line[8:len(line)-1]).strip())
        elif "app_key" in line:
            app_key = ubinascii.unhexlify((line[8:len(line)-1]).strip())
        elif "syncClockTime" in line:
            syncClockTime=float(line[14:len(line)-1].strip())
        elif "loraJoinTime" in line:
            loraJoinTime=float(line[13:len(line)-1].strip())
        elif "bleScanInterval" in line:
            bleScanInterval=int(line[16:len(line)-1].strip())
        elif "mainLoopWaitTime" in line:
            mainLoopWaitTime=int(line[17:len(line)-1].strip())
        elif "hbInterval" in line:
            hbInterval=int(line[11:len(line)-1].strip())
    f.close()

loadFromConfigFile()

When I use this function my programme gets stuck here:

lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
while not lora.has_joined():
    time.sleep(loraJoinTime)
    print('Not yet joined...')
print("joined!")    
setClock()

The sleep function doesn't work and the print function spams "Not yet joined..." in the terminal window.

f= open('config.txt')
for line in f:
    if "uuidExpected" in line:
        uuidExpected=line[13:len(line)-1].strip()
    elif "app_eui" in line:
        app_eui = ubinascii.unhexlify((line[8:len(line)-1]).strip())
    elif "app_key" in line:
        app_key = ubinascii.unhexlify((line[8:len(line)-1]).strip())
    elif "syncClockTime" in line:
        syncClockTime=float(line[14:len(line)-1].strip())
    elif "loraJoinTime" in line:
        loraJoinTime=float(line[13:len(line)-1].strip())
    elif "bleScanInterval" in line:
        bleScanInterval=int(line[16:len(line)-1].strip())
    elif "mainLoopWaitTime" in line:
        mainLoopWaitTime=int(line[17:len(line)-1].strip())
    elif "hbInterval" in line:
        hbInterval=int(line[11:len(line)-1].strip())
f.close()

When I don't wrap this code into a function everything works. When I write the function after the hardcoded loop everything works as well.


Solution

  • Thanks, to nekomatics comment I solved the issue. I simply wasn't aware of the global keyword in python. To stay with the same logic as before, the code should look like this.

    def loadFromConfigFile():
       global uuidExpected
       global app_eui
       global syncClockTime
       global loraJoinTime
       global bleScanInterval
       global mainLoopWaitTime
       global hbInterval
    
       f= open('config.txt')
       for line in f:
           if "uuidExpected" in line:
               uuidExpected=line[13:len(line)-1].strip()
           elif "app_eui" in line:
               app_eui = ubinascii.unhexlify((line[8:len(line)-1]).strip())
           elif "app_key" in line:
               app_key = ubinascii.unhexlify((line[8:len(line)-1]).strip())
           elif "syncClockTime" in line:
               syncClockTime=float(line[14:len(line)-1].strip())
           elif "loraJoinTime" in line:
               loraJoinTime=float(line[13:len(line)-1].strip())
           elif "bleScanInterval" in line:
               bleScanInterval=int(line[16:len(line)-1].strip())
           elif "mainLoopWaitTime" in line:
               mainLoopWaitTime=int(line[17:len(line)-1].strip())
           elif "hbInterval" in line:
               hbInterval=int(line[11:len(line)-1].strip())
        f.close()