Skip to content
forked from ryanzav/LoRaWAN

LoRaWAN implementation in python

Notifications You must be signed in to change notification settings

btemperli/LoRaPy

 
 

Repository files navigation

LoRaWAN

Language: Python TTN: v2 TTN: v3 Adafruit: 4074 Lora: 868MHz

This is an improved LoRaWAN v1.0 implementation in python.

It works with a Raspberry Pi 4 and the Adafruit LoRa Radio Bonnet with OLED - RFM95W @ 868MHz.

Based on the work of ❤️

For reference on LoRa see: lora-alliance: specification

This fork improves the support for the Adafruit LoRA Radio Bonnet with OLED - RFM95W @ 868MHz.

You can find the access to the Helium Network in the repository of ryanzav.


First Steps

  1. Install relevant libraries: adafruit instructions
  2. Rename keys_example.py to keys.py and enter your device information from the TTN Console.
  3. Setup your Raspberry Pi
  4. Connect the Adafruit Lora Bonnet
  5. Add an antenna to the Lora Bonnet p.e. "simple" Wire Antenna
  6. Check your environment and look for a public Lora Gateway

Testing

Sending

$ python3 tx_ttn.py

Sends an uplink to your TTN-Application.

Receiving

$ python3 rx_ttn.py

Receives a downlink from your TTN-Application. Attention: LoRaWAN does not send downlinks fulltime.

Send & Receive

$ python3 txrx_ttn.py

Sends an uplink and receives directly after a downlink.


Usage (TTN v2 and TTS v3)

Now we use this library as a python package. Move the folder to your project: $ mv ~/LoRaPy ~/your-project/LoRaPy. Copy the keys_example.py to your project: $ cp ~/LoRaPy ~/your-project/keys.py and set the correct keys from your TTN-Console.

Example

from LoRaPy.lorapy import LoRaPy
import keys
import time


def receive_callback(payload):
    print(payload)

"""
LoRaPy takes
    :param dev_addr: list. The "Device address" from your device, given by thethings.network.
    :param nw_key: list. The "NwkSKey" from your device, given by thethings.network.
    :param app_key: list. The "AppSKey" from your device, given by thethings.network.
    :param verbose: bool. True if verbose informations should be printed to the console.
    :param callback: function. Callback-function to handle downlinks out of the the things stack.
""" 
lora = LoRaPy(keys.devaddr, keys.nwskey, keys.appskey, True, receive_callback)

while True:
    """
    lora.send(message: string, spreading_factor: integer)
    """
    lora.send('this is your payload-string', 7)
    time.sleep(900)

Downlink check for The Thing Stack (v3)

from LoRaPy.lorapy import LoRaPy
import keys
import time
last_send = 0


def receive_callback(payload):
    global last_send
    print(payload)
    # reset time 
    last_send = time.time()

    
def try_to_send(message):
    # wait at least 900s before sending next message.
    if last_send + 900 > time.time():
        return
    
    # more than 900s since the last sending.
    lora.send(message, 7)


lora = LoRaPy(keys.devaddr, keys.nwskey, keys.appskey, True, receive_callback)

while True:
    time.sleep(30)
    try_to_send('this is your payload-string')

TODO

  • Add a constant uplink-sender
  • Add a constant uplink-sender with short downlink-check.
  • Remove Helium
  • TTN v3
  • Check OTAA for TTN
  • Prepare as python-library (p.e. PIP)
  • move ./LoRaWAN & ./SX127x to git submodule (if possible...)

About

LoRaWAN implementation in python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%