# Pycom lopy lora node code issues

**URL:** <https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833>\
**Category:** Guides\
**Created:** [2 November 2018 09:54 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833 "2018-11-02T09:54:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross70257](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Ross70257](https://forum.core-electronics.com.au/u/Ross70257)\
**Post date:** [2 November 2018 09:54 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833/1 "2018-11-02T09:54:42Z")

</div>

Hello,

not sure what i am missing but i can’t get my lora node on lopy 4 to work. I have pasted my code below but when i run it i first get the error -

> > > Traceback (most recent call last):  
> > > File “”, line 7, in   
> > > ImportError: no module named ‘config’

so i comment out the line ‘import config’ and then i get the next error -

Traceback (most recent call last):  
File “”, line 12, in   
AttributeError: type object ‘LoRa’ has no attribute ‘AU915’

what am i missing?  
I have updated firmware and i successfully built the gateway following your tutorial so not sure what i’m doing wrong this time.

```
from network import LoRa
import socket
import binascii
import struct
import time
import config

LORA_FREQUENCY = 916800000
LORA_NODE_DR = 5

lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.AU915) # Australia

# create an ABP authentication params
dev_addr = struct.unpack(">l", binascii.unhexlify('number i got from TTN'))[0]
nwk_swkey = binascii.unhexlify('a really long number from TTN'')
app_swkey = binascii.unhexlify('as above another long number'')

# remove all the non-default channels
for i in range(0, 72): # Australia
    lora.remove_channel(i)

# set the 3 default channels to the same frequency
lora.add_channel(0, frequency=LORA_FREQUENCY, dr_min=0, dr_max=5)
lora.add_channel(1, frequency=LORA_FREQUENCY, dr_min=0, dr_max=5)
lora.add_channel(2, frequency=LORA_FREQUENCY, dr_min=0, dr_max=5)

# join a network using ABP (Activation By Personalization)
lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))

# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)

# set the LoRaWAN data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, config.LORA_NODE_DR)

# make the socket blocking
s.setblocking(False)

for i in range (200):
    pkt = b'PKT #' + bytes([i])
    print('Sending:', pkt)
    s.send(pkt)
    time.sleep(4)
    rx, port = s.recvfrom(256)
    if rx:
        print('Received: {}, on port: {}'.format(rx, port))
    time.sleep(6)

```

---

<div class="post-metadata">

**Author:** ![Ross70257](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Ross70257](https://forum.core-electronics.com.au/u/Ross70257)\
**Post date:** [2 November 2018 11:13 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833/2 "2018-11-02T11:13:08Z")

</div>

i checked firmware of Lopy4 and my current versions are -

> > > import os  
> > > os.uname()  
> > > (sysname=‘LoPy4’, nodename=‘LoPy4’, release=‘1.9.2.b2’, version=‘v1.8.6-798-gebbcca48-dirty on 2017-10-31’, machine=‘LoPy4 with ESP32’, lorawan=‘1.0.0’)

where do i get the latest firmware from?

---

<div class="post-metadata">

**Author:** ![Graham](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/graham/32/54_2.png) [@Graham](https://forum.core-electronics.com.au/u/Graham)\
**Post date:** [4 November 2018 05:18 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833/3 "2018-11-04T05:18:17Z")

</div>

Hi Ross,

You can get the Pycom update utility for Windows here

[https://docs.pycom.io/gettingstarted/installation/firmwaretool](https://docs.pycom.io/gettingstarted/installation/firmwaretool)

---

<div class="post-metadata">

**Author:** ![Ross70257](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Ross70257](https://forum.core-electronics.com.au/u/Ross70257)\
**Post date:** [5 November 2018 02:20 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833/4 "2018-11-05T02:20:29Z")

</div>

Sorry to be a pain but i have updated firmware and it looks like my node is working as atom is REPL is spitting out to terminal  
Sending: b’PKT #\x1f’  
Sending: b’PKT # ’  
Sending: b’PKT #!’

But i can’t see anything on TTN.  
I have checked settings several times with TTN and i have the following -  
ttn-handler-asia-se

#### Application ID

lora-node-on-ttn

#### Device ID

lora\_node\_1

#### Activation Method

ABP

#### Device EUI

70B3D54998392763

#### Application EUI

70B3D57ED0014168

#### Device Address

26041662

#### Network Session Key

BAC9BE6CF0430EA5C2B945CDB1DF795F

#### App Session Key

C9492BA2988CD549CE41CAD7B90C1421

#### Status

never seen

My node pycom module has the following id numbers on it —  
Device EUI (LoRa): 70b3d54998392763  
Device EUI (WLAN): b’30aea4fffe784c48’

This is my current code -  
from network import LoRa  
import socket  
import binascii  
import struct  
import time  
#import config \<\<\<\<\<coment out this line

```
LORA_FREQUENCY = 916800000
LORA_NODE_DR = 5

lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.AU915) # Australia

# create an ABP authentication params
dev_addr = struct.unpack(">l", binascii.unhexlify('26041662'))[0]
nwk_swkey = binascii.unhexlify('BAC9BE6CF0430EA5C2B945CDB1DF795F')
app_swkey = binascii.unhexlify('C9492BA2988CD549CE41CAD7B90C1421')

# remove all the non-default channels
for i in range(0, 72): # Australia
    lora.remove_channel(i)

# set the 3 default channels to the same frequency
lora.add_channel(0, frequency=LORA_FREQUENCY, dr_min=0, dr_max=5)
lora.add_channel(1, frequency=LORA_FREQUENCY, dr_min=0, dr_max=5)
lora.add_channel(2, frequency=LORA_FREQUENCY, dr_min=0, dr_max=5)

# join a network using ABP (Activation By Personalization)
lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))

# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)

# set the LoRaWAN data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, LORA_NODE_DR)
#removed the word 'config' only from above line

# make the socket blocking
s.setblocking(False)

for i in range (200):
    pkt = b'PKT #' + bytes([i])
    print('Sending:', pkt)
    s.send(pkt)
    time.sleep(4)
    rx, port = s.recvfrom(256)
    if rx:
        print('Received: {}, on port: {}'.format(rx, port))
    time.sleep(6)

i am running same firmware on both lopy4's - 
(sysname='LoPy4', nodename='LoPy4', release='1.18.1.r1', version='v1.8.6-849-b0520f1 on 2018-08-29', machine='LoPy4 with ESP32', lorawan='1.0.2', sigfox='1.0.1')

My gateway details are as follows - 
""" LoPy LoRaWAN Nano Gateway configuration options """

import machine
import ubinascii

WIFI_MAC = ubinascii.hexlify(machine.unique_id()).upper()
# Set the Gateway ID to be the first 3 bytes of MAC address + 'FFFE' + last 3 bytes of MAC address
GATEWAY_ID = WIFI_MAC[:6] + "FFFE" + WIFI_MAC[6:12]

SERVER = 'router.au.thethings.network'
PORT = 1700

NTP = "au.pool.ntp.org"
NTP_PERIOD_S = 3600

WIFI_SSID = 'TPG-RIKL'
WIFI_PASS = 'MySecure password'

# for AU915
LORA_FREQUENCY = 916800000
LORA_GW_DR = "SF7BW125" # DR_5
LORA_NODE_DR = 5

# for EU868
#LORA_FREQUENCY = 868100000
#LORA_GW_DR = "SF7BW125" # DR_5
#LORA_NODE_DR = 5

# for US915
# LORA_FREQUENCY = 903900000
# LORA_GW_DR = "SF7BW125" # DR_3
# LORA_NODE_DR = 3

My gateway is happily ticking away and showing connected every minute.

Any ideas? - i feel myself growing old trying to figure this out.

I have since thought maybe i need a few lines to get an idea where i am getting stuck and i came up with the following in the node code - 

for i in range (200):
    pkt = b'PKT #' + bytes([i])
    print('Sending:', pkt)
    s.send(pkt)
    time.sleep(4)
    rx, port = s.recvfrom(256)
    print('we got this far')
    if rx:
        print('Received: {}, on port: {}'.format(rx, port))
        print("we rx'd something")
    time.sleep(6)
:

```

results are that i am looping - print(‘we got this far’)

Sending: b’PKT #\x00’  
we got this far  
Sending: b’PKT #\x01’  
we got this far  
Sending: b’PKT #\x02’  
we got this far  
Sending: b’PKT #\x03’  
we got this far  
Sending: b’PKT #\x04’  
we got this far

but i am not getting past that line…any ideas?

---

<div class="post-metadata">

**Author:** ![Graham](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/graham/32/54_2.png) [@Graham](https://forum.core-electronics.com.au/u/Graham)\
**Post date:** [5 November 2018 22:19 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833/5 "2018-11-05T22:19:21Z")

</div>

Hi Ross,

What gateway are you using (I am curious why you are using 3 channels)?

---

<div class="post-metadata">

**Author:** ![Ross70257](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Ross70257](https://forum.core-electronics.com.au/u/Ross70257)\
**Post date:** [6 November 2018 02:49 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833/6 "2018-11-06T02:49:35Z")

</div>

I used the previous tutorial ‘lora nano gateway’.  
I had a little trouble understanding what i needed to do with those lines so i left all 3 in.  
I have since commented out the other 2 while trying just 1 line.

My gateway is a lopy4 and running the same firmware as node.  
The code in gateway is -

# for AU915

LORA\_FREQUENCY = 916800000  
LORA\_GW\_DR = “SF7BW125” # DR\_5  
LORA\_NODE\_DR = 5

Any ideas what i am missing?  
Is there a way to ping gateway?

---

<div class="post-metadata">

**Author:** ![Graham](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/graham/32/54_2.png) [@Graham](https://forum.core-electronics.com.au/u/Graham)\
**Post date:** [6 November 2018 04:02 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833/7 "2018-11-06T04:02:01Z")

</div>

I’d recommend using a full LoRaWAN Gateway, especially with TTN. That tutorial was more of an exploration project and you will certainly run into issues if you’ve missed anything along the way. A proper gateway will make things simple, a basic connection would be

```
# Released by: Core Electronics Pty Ltd
# Author: Graham Mitchell
# Attribution-ShareAlike 3.0 Australia (CC BY-SA 3.0 AU)
# https://creativecommons.org/licenses/by-sa/3.0/au/

# these are pycom specific-libraries
from network import LoRa
import pycom
import socket
import binascii
import utime
import machine

# disable LED heartbeat (so we can control the LED)
pycom.heartbeat(False)
# set LED to red
pycom.rgbled(0x110000)

# lora config
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.AS923)
# access info from ttn console (note; we just need the app id & app key)
app_eui = binascii.unhexlify('XXXXXXXXXXXXX')
app_key = binascii.unhexlify('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
replaced with the code inside config.py
# attempt join - continues attempts background every 15 seconds
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)

# wait for a connection to be established
print('Waiting for LoRaWAN network connection...')
while not lora.has_joined():
    utime.sleep(1)
    if utime.time() > 15:
        print("possible timeout or collision")
        machine.reset()
    pass

# tell the world!
print('Network joined!')

# setup the socket - of note is port number is configured with s.bind
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
s.setblocking(False)
s.bind(1)

# set LED to yellow
pycom.rgbled(0x111100)

# send some data
s.send(bytes([1, 2, 3]))
print("Sent!")

# set LED to green
pycom.rgbled(0x001100)

# do nothing forever from here..
while True:
    pass
```

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [7 November 2018 05:45 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833/8 "2018-11-07T05:45:31Z")

</div>

Hey Ross,

Check out our new TTN tutorials section. They show you how to connect using OTAA which is the more desirable connection method. We use the Lopy4 in all the tutorials

> **[Core Electronics | Core Electronics Australia](https://core-electronics.com.au/tutorials/the-things-network-ttn/)**
>
> Core Electronics is a retailer for electronics in Australia including Arduino, Raspberry Pi, Sparkfun and Adafruit products. Fast shipping and support.

---

<div class="post-metadata">

**Author:** ![Ross70257](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Ross70257](https://forum.core-electronics.com.au/u/Ross70257)\
**Post date:** [13 November 2018 04:55 UTC](https://forum.core-electronics.com.au/t/pycom-lopy-lora-node-code-issues/3833/9 "2018-11-13T04:55:37Z")

</div>

Cheers for the update – I look forward to giving it a go.

_Kind Regards_

\*\*Ross
