Unable to Connect LoPy to LoRaWAN with OTAA

Hello Everyone,

This seems to be a topic that comes up frequently so apologies for adding to the tedium.

I am new to LoRa and TTN in general and I am looking for a little outside help to start trouble shooting.

Some Info:

  • I am using a LoPy 1.0r in both an Expansion Board 2 and PyTrack. All latest Firmware, LoPys are running the latest (1.12.0.b0 at time of writing)

  • Device added to Application on TTN

  • I allegedly can connect via ABP, though I am sceptical. I am just running the PyCom example found here and it does print some data out though it may likely be noise.

  • Europe Region (i.e. LoRa.EU868)

  • Device EUI, Application EUI and App Key have all been triple checked

My code is nothing fancy, just try to join and print a message on lora.has_joined() returning true.

from network import LoRa
import time
import binascii

# OTAA authentication parameters
# dev_eui = binascii.unhexlify('XXXXXXXXXXXXXXXX')
# app_eui = binascii.unhexlify('XXXXXXXXXXXXXXXX')
# app_key = binascii.unhexlify('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')

# join a network using OTAA (Over the Air Activation)
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key, dev_eui), timeout=0)

# wait until the module has joined the network
while not lora.has_joined():
    time.sleep(1)
    print('Not yet joined...')

print('Joined!!')

Any suggestions for debugging would be greatly appreciated.

thanks very much

did you use search ? :sunglasses:

Sadly, in this case no previous thread has concluded with a solution that works for myself.

As far as I can tell the common reasons for not being able to connect are

  • Incorrect Device EUI, Application EUI or App Key

  • Incompatible Firmware

  • Not having registered the device on an Application (i.e. Dev EUI not provided to TTN)

  • Or the problem relates to setting the LoPy as a Nano Gateway and not a node in an application.

I have exhausted the above but to no avail. I failed to mention that my LoPys are running the latest (1.12.0.b0 at time of writing). I will edit that into my original post.

Had the same issue. And gave up, bought an IMST gateway. Now all is working well. The issue is in the gateway not the node as this works fine now.
What I experienced is that the OTAA only worked if I rebooted the LoPy gateway from the repl by uploading a new code (just make a little change in a comment line). In all other cases it was luck for it to work.
I guess Pycom should be aware of this issue by now.
Good luck!

This is what worked for me some monthes ago :

import binascii
import pycom
import socket
import time
from network import LoRa

# Colors
off = 0x000000
red = 0x7f0000
green = 0x007f00
blue = 0x00007f

# Turn off hearbeat LED
pycom.heartbeat(False)

# Initialize LoRaWAN radio
lora = LoRa(mode=LoRa.LORAWAN)

# Set network keys
app_eui = binascii.unhexlify('XXXXXXXXXXXXXXXXX')
app_key = binascii.unhexlify('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')

# Join the network
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
pycom.rgbled(0x1f0000)

# Loop until joined
while not lora.has_joined():
    print('Not OTAA joined yet...')
    pycom.rgbled(0x1f0000)
    time.sleep(0.1)
    pycom.rgbled(off)
    time.sleep(2.4)

print('OTAA Joined')
pycom.rgbled(0x00001f)

s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
s.setblocking(True)

i = 0
while True:
    pycom.rgbled(0x00003f)
    count = s.send(bytes([i % 256]))
    print('Sent %s bytes' % count)
    time.sleep(0.1)
    pycom.rgbled(0x001f00)
    time.sleep(59.9)
    i += 1

Differences I notice in the lora related code :

  • Mine instantiate lora with mode=LoRa.LORAWAN, yours not.
  • Yours pass the dev_eui for auth, mine not.
2 Likes

Thanks @DeuxVis, I had accidentally left out the lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868) line from the example code I will add that in to the original question.

With regard to the inclusion of the dev_eui, sadly it did not make a difference included or left out.

The code is essentially a stripped down version of what Pycom provides here

The original post had the EUI and Key lines commented out, this was just a typo during posting.

from network import LoRa
import time
import binascii

# OTAA authentication parameters
dev_eui = binascii.unhexlify('XXXXXXXXXXXXXXXX')
app_eui = binascii.unhexlify('XXXXXXXXXXXXXXXX')
app_key = binascii.unhexlify('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')

# join a network using OTAA (Over the Air Activation)
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)

# wait until the module has joined the network
while not lora.has_joined():
    time.sleep(1)
    print('Not yet joined...')

print('Joined!!')

Hello @f_vista,

If you have not already, I would run a firmware update on your device.

As part of the firmware update you should get the option to choose your LoRa region. This should mean you do not need to specify a region attribute when initialising the LoRa object, you may simply type something like lora = LoRa(mode=LoRa.LORAWAN)

1 Like

@f_vista

At that stage, if you have double checked all relevant details, the last issue can only really be a lack of coverage.

What I would recommend, at that point, is to search and join your closest TTN Community.

Coverage is not guaranteed so it may be a case that you are out of range or just not turned on. You should be able to find the operator of your closest gateway and ask very, very nicely if you could bother them. This is my current situation.

After a little difficulty I have finally managed to get connected. It appears to be a combination of the local topography and the availability of TTN gateways in my area.

What has made is setting the data rate to 0 when trying to join. As I understand it, this is essentially setting the spreading factor to it’s farthest reach (i.e. SF 12) for the initially OTAA join request.

NB: this was not available in some earlier firmware for the LoPy. So, as always, make sure the firmware is up to date.

The time to join is a little erratic and can be anything from 20 - 120 seconds. This seems to go against the typical join time of around 5 - 10 seconds. Basically, your mileage may and probably will vary.

So, as it stands the simplest and best chance code for connecting to LoRaWAN gateways is:

from network import LoRa
import time
import binascii

# LoRa region will be set when you update the firmware
# it does not need to be explicitly stated  
lora = LoRa(mode=LoRa.LORAWAN)

# OTAA authentication parameters
app_eui = binascii.unhexlify('XXXXXXXXXXXXXXXX')
app_key = binascii.unhexlify('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')

# join a network using OTAA (Over the Air Activation)
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0, dr=0)

# wait until the module has joined the network
while not lora.has_joined():
    pass # no delays, just keeps looping until .has_joined() returns true

print('Joined!!')

Not stating the dev_eui will result in the .join() defaulting to the devices MAC, which is what the device will be registered with in a TTN application.

Beyond that, your checklist for connecting should still be:

  • check coverage of your local area on TTN
  • Update firmware
  • check your device is registered to an application
  • check App Eui and App Key are correct for your device
  • test connection in different areas/buildings, in open ground and different elevations.

Failing all of that, and if there should be coverage in your area, reach out to and join your local TTN community as they are a helpful bunch.

My first project will now be to wander around with the PyTrack and see if I can map out where I can get a signal.

1 Like