Make Pysense save the session keys to join only once through OTAA

I use a pysense to send the temp every 15min.
With my current code the node rejoins ttn every time it wakes up.

I read here
That i should save the key but have no idea where to save the key on the pysense and how to save the key?

Does anybody have any experience on how to this on a pysens?

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

# import the Humidity and Temperature Sensor (SI7006A20)
from SI7006A20 import SI7006A20
from pysense import Pysense

py = Pysense()
si = SI7006A20(py)

# Initialize LoRa in LORAWAN mode.
# Please pick the region that matches where you are using the device:
# Asia = LoRa.AS923
# Australia = LoRa.AU915
# Europe = LoRa.EU868
# United States = LoRa.US915
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)

# create an OTAA authentication parameters
app_eui = binascii.unhexlify('xx xx xx'.replace(' ',''))
app_key = binascii.unhexlify('xx xx xx'.replace(' ',''))

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

# Disable the LED Heartbeat and wait until the module has joined the network
count = 0
while not lora.has_joined():
    # turn the LED red
    # pycom.rgbled(0xff0000)
    # sleep 2 seconds
    # turn the LED black = disable the LED
    print("[" + str(time.time()) + "] not joined the network yet [count=" + str(count) + "]")
    count = count + 1

# 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, 5)

# make the socket blocking
# (waits for the data to be sent and for the 2 receive windows to expire)

# send the temperature and humidity
while True:
    data = "%.4f;%.5f" % (si.temperature(), si.humidity())
    print("[" + str(time.time()) + "] sending Temperature and Relative Humidity! ")
    print("[" + str(time.time()) + "] data sent [" + str(data) + "]")
    # go to deepsleep 

# make the socket non-blocking
# (because if there's no data received it will block forever...)

# get any data received (if any...)
data = s.recv(64)
print("[" + str(time.time()) + "] data received [" + data + "]")

See lora.nvram_save(), lora.nvram_restore() and lora.nvram_erase()

1 Like