Skip to content

Commit fecefb8

Browse files
gijsiopeter-pycom
authored andcommitted
included tutorial on nvram usage
1 parent f713aad commit fecefb8

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

config.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ theme = "doc-theme"
302302
identifier = "tutorials@networks@lora@module-module"
303303
parent = "tutorials@networks@lora"
304304
weight = 60
305+
[[menu.main]]
306+
name = "nvram"
307+
url = "/tutorials/networks/lora/nvram/"
308+
identifier = "tutorials@networks@lora@nvram"
309+
parent = "tutorials@networks@lora"
310+
weight = 70
305311

306312
[[menu.main]]
307313
name = "LTE"

content/firmwareapi/pycom/network/lora/_index.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,16 @@ lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=lor
222222

223223
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.
224224

225-
```python
226-
lora.nvram_save()
227-
```
228225

229226
### lora.nvram_restore()
230227

231-
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.
228+
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.
232229

233230
### lora.nvram_erase()
234231

235232
Remove the LoRaWAN state (joined status, network keys, packet counters, etc) from non-volatile memory.
236233

234+
See the [tutorials](/tutorials/networks/lora/nvram/) for an example on how to use nvram
237235

238236
### lora.mesh()
239237

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: "non-volatile RAM"
3+
aliases:
4+
- tutorials/lora/nvram.html
5+
- tutorials/lora/nvram.md
6+
- chapter/tutorials/lora/nvram
7+
---
8+
9+
See the example below on how to use the lora nvram methods:
10+
11+
```python
12+
import machine
13+
import time
14+
from network import LoRa
15+
import socket
16+
import ubinascii
17+
18+
sleep_time = 1000
19+
print("init LoRa")
20+
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
21+
for i in range(0,10):
22+
time.sleep(0.1) #be able to ctrl+c out of this
23+
# create an ABP authentication params
24+
dev_addr = struct.unpack(">l", ubinascii.unhexlify(''))[0]
25+
nwk_swkey = ubinascii.unhexlify('')
26+
app_swkey = ubinascii.unhexlify('')
27+
lora.nvram_restore()
28+
if(lora.has_joined() == False):
29+
print("LoRa not joined yet")
30+
lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))
31+
else:
32+
print("LoRa Joined")
33+
34+
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
35+
# set the LoRaWAN data rate
36+
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
37+
# make the socket blocking
38+
# (waits for the data to be sent and for the 2 receive windows to expire)
39+
s.setblocking(True)
40+
# send some data
41+
print("[send_lora] sending {}".format([0,1,2]))
42+
43+
s.send(bytes([0, 1, 2]))
44+
# make the socket non-blocking
45+
# (because if there's no data received it will block forever...)
46+
s.setblocking(False)
47+
# get any data received (if any...)
48+
data = s.recv(64)
49+
lora.nvram_save()
50+
print("received: {}".format(data))
51+
52+
print("sleeping for {} ms".format(sleep_time))
53+
machine.deepsleep(sleep_time)
54+
```

0 commit comments

Comments
 (0)