Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    deep sleep lopy pysense

    Scheduled Pinned Locked Moved LoPy
    lopydeepsleeppysensepymakr
    4 Posts 3 Posters 1.1k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      han
      last edited by

      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

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jmarcelino @han
        last edited by

        @han

        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)
        
        H 1 Reply Last reply Reply Quote 0
        • J Offline
          jcaron
          last edited by

          Also, I don’t think you need to save the LoRa state independently. Just use lora.nvram_restore directly, and check lora.has_joined to decide whether you should join or not.

          1 Reply Last reply Reply Quote 0
          • H Offline
            han @jmarcelino
            last edited by

            @jmarcelino I use a Lopy direct on the KPN LoRa network.
            What should it be in place of this part of code?

            1 Reply Last reply Reply Quote 0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • 1 / 1
            • First post
              Last post

            Pycom on Twitter

            Pycom