LoPy OTAA Example

Since there’s a LoPy ABP Example thread, I thought I’d add one for OTAA:

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

# Colors
off = 0x000000
red = 0xff0000
green = 0x00ff00
blue = 0x0000ff

# Turn off hearbeat LED
pycom.heartbeat(False)

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

# Set network keys
app_eui = binascii.unhexlify('0000000000000000')
app_key = binascii.unhexlify('00000000000000000000000000000000')

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

# Loop until joined
while not lora.has_joined():
    print('Not joined yet...')
    pycom.rgbled(off)
    time.sleep(0.1)
    pycom.rgbled(red)
    time.sleep(2)

print('Joined')
pycom.rgbled(blue)

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:
    count = s.send(bytes([i % 256]))
    print('Sent %s bytes' % count)
    pycom.rgbled(green)
    time.sleep(0.1)
    pycom.rgbled(blue)
    time.sleep(9.9)
    i += 1

Set your own hex App EUI / App Key of course.

The script turns on with a red LED. It then joins the network. Once the join succeeded, the LED turns blue. Then an incrementing counter byte will be sent every 10 seconds, starting at 0, wrapping back to 0 after 255. Every time a byte is sent, the LED flashes green.

Hope this helps. It took me a while until I got it to run, since there seems to be no central place for documentation.

I tested this successfully on the new v2 backend.

7 Likes

Hi dbrgn

I tried the OTAA example with the firmware 1.0.0.b2 on my LoPy. I did change the app_eui and app_key. Now if I run the script it generates a TimeoutError: timed out on line 25.
If I change the value of the timeout from 60 to 0 the error disappears but I just get “Not joined yet…” on the console and the LED is flashing in red.

Are there any jumper I need to set on the baseboard?

Any hints are welcome and thx in advance.
affoltep

edited: I have tried to use the ABP connection.This was working without any problems. So I guess it needs to be a problem with the new LoPy firmware 1.0.0.b2.

The problem still there in version 1.0.0.b8 any ideas?

The problem still remains with 1.1.0.b1. I placed a post at the forum of pycom.

I’ve got the example to work now! You need to read the Dev EUI out of the LoPy device:

from network import LoRa
import binascii
lora = LoRa(mode=LoRa.LORAWAN)
print(binascii.hexlify(lora.mac()).upper().decode(‘utf-8’))

Then use this Dev EUI to declare your device at the TTN application.

4 Likes

Hi

I tried the same example but i can’t connect to the gateway
I build the gateway using Rpi3 +ic880a spi
the gateway is up and running but the lopy never connects I might missing something with gateway configuration

# RF packets received by concentrator: 36

> Jan 14 23:21:08 ttn-gateway ttn-gateway[4794]: # CRC_OK: 0.00%, CRC_FAIL: 100.00%, NO_CRC: 0.00%

I see CRC failed 100%
any ideas would be great thanks

I tried this example tonight and I have version ‘1.7.1.b2’

I’m getting
Traceback (most recent call last):
File “”, line 26, in
TimeoutError: timed out

Can anyone tell me if these are MSB first or LSB first i.e Do I have to reverse the numbers I get from TTN or leave them as they are on the Device overview screen?

For the Device ID Code above.

Did you just paste this into the Device Overview Screen or did you need to reverse the numbers (LSB to MSB)?

Just paste it.

I see the same issue as @ksamuelson with the provided sample.

Traceback (most recent call last):                                                  
  File "<stdin>", line 25, in <module>                                              
TimeoutError: timed out

Just set the timeout=0 in the lora.join function

Ok, Thank you @affoltep I am now getting joined. I had to put the device ID into TTN and it worked. I’m still not getting the send. I get:

Not joined yet…
Joined
Traceback (most recent call last):
File “”, line 40, in
OSError: [Errno 95] EOPNOTSUPP

Lines around 40 are:
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
s.setblocking(True)

Then the While for the send
I’m using Pymakr setup through Atom. I hope I set it up correctly but may not have. Any help appreciated.

So the answer to this issue is that in the US the DR needs to be 3 not 5. Hope this helps someone else save some time.

“SO_DR (datarate) 5 is only available in LoRa 868Mhz (EU868). Are you in a different region? For US915 it should be 3.” The Answer was found here:

Another note that might be useful. If you are using Atom with the Pymakr plug in it seems you have to save before you run the file or it runs the last file. Possible this is not the case (I’m still learning) but this seemed to be the way it worked as I installed it.

1 Like

Hi,

I’m having trouble getting OTAA to work. I’ve tried every example I could come across, but I never get a Join Accepted.

I’m using the LoPy with (AFAIK) the latest firmware:

os.uname().release
‘1.8.0.b1’

My sketch is as simple as possible:

As you can see I keep getting

Not joined yet…

My gateway is a Lorix One and according to the console I do receive the Join Requests:

I’m not sure if I should be able to see the Join Accepts in the console? Also I’m new to much of this, so I haven’t figured out how to monitor the Lorix traffic to see what happens at the gateway (any help here would be appreciated). I’ve worked with an iC880A+Raspi before where I used tcpdump on port 1700 to see what happened, maybe something similar can be used here?

Anyways. Does anyone have a reliably working OTAA LoPy node? If so, can you point me in a the right direction?

Thank you!

edit: the ABP examples are working and I am receiving the bytes in my application.

To debug: check the application data page to see if the application shows the join request as well.

Good point. As you can see in the screenshot, I do not see any application data. Does this imply that there is something wrong with the keys? I’ve double checked, and the app key and EUI should be the right ones…

I was sure that I checked everything! But apparently I made a mistake in the dev eui… I dont quite understand this, because I was able to do the ABP… Anyways - now I joined - awesome!

Glad you got it working.
The dev eui is only used for OTAA. ABP uses the device address.

1 Like

I got a LoPy on a PyTrack running, based on the code in the PyCom github
pycom-libraries\examples\lorawan-nano-gateway and the source otaa_node.py using settings from config.py. And configured the source for OTAA with the dev eui, app eui and app key as in the TTN console. It takes the latitude and longitude from the PyTrack, and sends it as bytes, and the payload is then decoded in the console, based on the lab https://www.thethingsnetwork.org/labs/story/gps-tracker.

I just tried to connect my Lopy to someones Gateway with this code.
I can see the join requests in my TTN Application data but my Lopy keeps sending those request instead of printing “Joined” and start sending the payload. I had the exact same problem with another example code before.

Can someone help me?

I am trying this here in Switzerland and therefore changed the example to:
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)