0

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.

Dennis
  • 1
  • 3
  • What are the actual values of `app_eui`, `app_key` and `loraJoinTime` in each case? Print them to the console before your `lora.join` command… – nekomatic Jan 21 '22 at 09:44
  • Okay, right before lora.join() these values were not changed and I already found a fix to the problem. I wasn't aware of the "global" keyword in python. Now I got it working. Thank you for the hint :) – Dennis Jan 22 '22 at 19:32

1 Answers1

0

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()
Dennis
  • 1
  • 3