From fecefb89f0e5c59b211f269ce6955d30a1f73330 Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Fri, 4 Dec 2020 10:46:03 +0100 Subject: [PATCH] included tutorial on nvram usage --- config.toml | 6 +++ .../firmwareapi/pycom/network/lora/_index.md | 6 +-- content/tutorials/networks/lora/nvram.md | 54 +++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 content/tutorials/networks/lora/nvram.md diff --git a/config.toml b/config.toml index 3e9ae42..f400c66 100644 --- a/config.toml +++ b/config.toml @@ -302,6 +302,12 @@ theme = "doc-theme" identifier = "tutorials@networks@lora@module-module" parent = "tutorials@networks@lora" weight = 60 +[[menu.main]] + name = "nvram" + url = "/tutorials/networks/lora/nvram/" + identifier = "tutorials@networks@lora@nvram" + parent = "tutorials@networks@lora" + weight = 70 [[menu.main]] name = "LTE" diff --git a/content/firmwareapi/pycom/network/lora/_index.md b/content/firmwareapi/pycom/network/lora/_index.md index 6bec3f6..abe8ad3 100644 --- a/content/firmwareapi/pycom/network/lora/_index.md +++ b/content/firmwareapi/pycom/network/lora/_index.md @@ -222,18 +222,16 @@ lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=lor Save the LoRaWAN state (joined status, network keys, packet counters, etc) in non-volatile memory in order to be able to restore the state when coming out of deepsleep or a power cycle. -```python -lora.nvram_save() -``` ### lora.nvram_restore() -Restore the LoRaWAN state (joined status, network keys, packet counters, etc) from non-volatile memory. State must have been previously stored with a call to `nvram_save` before entering deepsleep. This is useful to be able to send a LoRaWAN message immediately after coming out of deepsleep without having to join the network again. This can only be used if the current region matches the one saved. +Restore the LoRaWAN state (joined status, network keys, packet counters, etc) from non-volatile memory. State must have been previously stored with a call to `nvram_save` before entering deepsleep. This is useful to be able to send a LoRaWAN message immediately after coming out of deepsleep without having to join the network again. This can only be used if the current region matches the one saved. Note that the nvram will be cleared after using this method. ### lora.nvram_erase() Remove the LoRaWAN state (joined status, network keys, packet counters, etc) from non-volatile memory. +See the [tutorials](/tutorials/networks/lora/nvram/) for an example on how to use nvram ### lora.mesh() diff --git a/content/tutorials/networks/lora/nvram.md b/content/tutorials/networks/lora/nvram.md new file mode 100644 index 0000000..f3f6231 --- /dev/null +++ b/content/tutorials/networks/lora/nvram.md @@ -0,0 +1,54 @@ +--- +title: "non-volatile RAM" +aliases: + - tutorials/lora/nvram.html + - tutorials/lora/nvram.md + - chapter/tutorials/lora/nvram +--- + +See the example below on how to use the lora nvram methods: + +```python +import machine +import time +from network import LoRa +import socket +import ubinascii + +sleep_time = 1000 +print("init LoRa") +lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868) +for i in range(0,10): + time.sleep(0.1) #be able to ctrl+c out of this +# create an ABP authentication params +dev_addr = struct.unpack(">l", ubinascii.unhexlify(''))[0] +nwk_swkey = ubinascii.unhexlify('') +app_swkey = ubinascii.unhexlify('') +lora.nvram_restore() +if(lora.has_joined() == False): + print("LoRa not joined yet") + lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey)) +else: + print("LoRa Joined") + +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) +s.setblocking(True) +# send some data +print("[send_lora] sending {}".format([0,1,2])) + +s.send(bytes([0, 1, 2])) +# make the socket non-blocking +# (because if there's no data received it will block forever...) +s.setblocking(False) +# get any data received (if any...) +data = s.recv(64) +lora.nvram_save() +print("received: {}".format(data)) + +print("sleeping for {} ms".format(sleep_time)) +machine.deepsleep(sleep_time) +``` \ No newline at end of file