deep sleep lopy pysense
-
Hello,
I wrote this code for my pysense and lopy combination.
The plan is to send data every half hour and between sending data the lopy has to be in deep sleep mode.FOr that i need to save the lora connection settings with the nv ram save and restore commands.
# LoRaWAN (ABP) from network import LoRa from mqtt import MQTTClient import socket import binascii import struct import time import pycom from deepsleep import DeepSleep import ujson import gc from pysense import Pysense from SI7006A20 import SI7006A20 from LTR329ALS01 import LTR329ALS01 from MPL3115A2 import MPL3115A2,ALTITUDE,PRESSURE py = Pysense() mp = MPL3115A2(py,mode=ALTITUDE) mpp = MPL3115A2(py,mode=PRESSURE) si = SI7006A20(py) lt = LTR329ALS01(py) ds = DeepSleep() gc.enable() loraSaved = pycom.nvs_get('loraSaved') if not loraSaved: # Initialize LoRa in LORAWAN mode. lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868) for i in range(3, 16): lora.remove_channel(i) # create an ABP authentication params dev_addr = struct.unpack(">l", binascii.unhexlify('14 20 3D xx'))[0] nwk_swkey = binascii.unhexlify('ac 6d 18 07 7f 5e 30 a1 4a 2e 3c 90 c1 58 27 xx') app_swkey = binascii.unhexlify('27 22 bd dc ec fd 1d 80 b0 ed a3 01 17 99 f3 xx') # join a network using ABP (Activation By Personalization) lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey)) print('Joining the LoRa network') # wait until the module has joined the network while not lora.has_joined(): time.sleep(2.5) print('Not joined yet...') machine.idle() # 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, 3) # make the socket blocking s.setblocking(False) time.sleep(5) s.send('LoRa Saved') time.sleep() lora.nvram_save() pycom.nvs_set('loraSaved', 1) print("Going to DeepSleep") ds.enable_pullups('P17') # can also do ds.enable_pullups(['P17', 'P18']) ds.enable_wake_on_fall('P17') # can also do ds.enable_wake_on_fall(['P17', 'P18']) ds.go_to_sleep(30*60) # go to sleep for 15 minutes else: lora = LoRa(mode=LoRa.LORAWAN) lora.nvram_restore() s = socket.socket(socket.AF_LORA, socket.SOCK_RAW) s.setsockopt(socket.SOL_LORA, socket.SO_DR, 3) s.setblocking(False) mqData = {} gc.collect() mqData ["m"] = {} mqData ["m"]["T"] = mp.temperature() mpp = MPL3115A2(py,mode=PRESSURE) mqData ["m"]["P"] = (mpp.pressure()/100) mqData ["m"]["H"] = si.humidity() mqData ["m"]["B"] = py.read_battery_voltage() enc = ujson.dumps(mqData) gc.collect() s.send(enc) pycom.nvs_set('loraSaved', 0) # reset for next test print('STA node sending packet ...') print(enc) print('LoRa data saved') time.sleep(5) print("Going to DeepSleep") ds.enable_pullups('P17') # can also do ds.enable_pullups(['P17', 'P18']) ds.enable_wake_on_fall('P17') # can also do ds.enable_wake_on_fall(['P17', 'P18']) ds.go_to_sleep(30*60) # go to sleep for 15 minutes # get any data received... data = s.recv(64) print(data)
But at the end there is an error message on line 37. Does anybody know what i am doing wrong?
Please let me know.
With kind regards, Han
-
@jmarcelino I use a Lopy direct on the KPN LoRa network.
What should it be in place of this part of code?
-
Also, I don’t think you need to save the LoRa state independently. Just use
lora.nvram_restore
directly, and checklora.has_joined
to decide whether you should join or not.
-
I don't understand why you are trying to remove channels 3-16 in the EU868 region? What type of gateway do you have?
In any case the indentation seems wrong in the for loop
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868) for i in range(3, 16): lora.remove_channel(i)