LoRa-Workshop/README.md
2019-11-30 21:18:49 +01:00

3.4 KiB

imaginaerraum LoRa Workshop

This workshop assumes you are familiar with the basics of MicroPython (see apos's MicroPython Workshop for an introduction).

Hardware

We use the Wemos® TTGO LORA32 868/915Mhz board.

Software

We make use of the two MicroPython libraries uPyLora by lemariva and uLoRaWAN by mallagant. uPyLora is a driver for the SX1276 LoRa module which is connected to the ESP32. uLoRaWAN is a library which creates LoRaWAN packages, which you can then send via LoRa to send data to TheThingsNetwork.

I added a wrapper class LoRaTransceiver which makes them a bit easier to use.

Setup

The files you will need are all located in the micropython/ subfolder. To get started, you can either upload the file

  • lora_transceiver.py

and the folders

  • uPyLora/
  • uLoRaWAN/
  • uPySensors/

to the ESP, or just flash this image.

Here are some examples how to use the LoRa module:

1. LoRa sending:

from lora_transceiver import LoRaTransceiver

from uPySensors.ssd1306_i2c import Display

d = Display()

# create transceiver
lora = LoRaTransceiver(display=d)

# send data
lora.send_string('imaginaerraum')

2. LoRa receiving:

from lora_transceiver import LoRaTransceiver

from uPySensors.ssd1306_i2c import Display

d = Display()

# create transceiver 
lora = LoRaTransceiver(display=d)

# start receiving data
#lora.recv()

3. LoRaWAN sending:

from lora_transceiver import LoRaTransceiver

from uPySensors.ssd1306_i2c import Display

import uLoRaWAN
from uLoRaWAN.MHDR import MHDR

d = Display()

# create transceiver for LoRaWAN frequency (channel 0 = 868.1 Mhz)
lora = LoRaTransceiver(frequency=868.1E6, syncword=0x34, display=d)

# change the spreading factor
lora.setSpreadingFactor(9)

# TODO: set address and keys for LoRaWAN
devAddr = [0x00, 0x00, 0x00, 0x00] 
nwkSKey = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
appSKey = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]

devAddr_hex = " ".join(list(map(lambda x: "{0:02x}".format(x), devAddr))).upper()
nwkSKey_hex = " ".join(list(map(lambda x: "{0:02x}".format(x), nwkSKey))).upper()
appSKey_hex = " ".join(list(map(lambda x: "{0:02x}".format(x), appSKey))).upper()
print("devAddr: ", devAddr_hex)
print("nwkSKey: ", nwkSKey_hex)
print("appSKey: ", appSKey_hex)

# lorawan object for conversion of data in LoRaWAN message format
lorawan = uLoRaWAN.new(nwkSKey, appSKey)

def send_lorawan(message, frame_cnt=0):
    lorawan.create(MHDR.UNCONF_DATA_UP, {'devaddr': devAddr, 'fcnt': frame_cnt, 'data': list(map(ord, message)) })
    payload = lorawan.to_raw()

    lora.send(payload)

    print("lorawan message sent")

send_lorawan("Hello world!")

(you can also find these in the examples/ subdirectory)

For more information, have a look at the documentation of the LoRaTransceiver class.