Waveshare SX1262 LoRaWAN Hat

173 views Asked by At

Does anyone know a Python library to communicate with a Waveshare SX1262 LoRaWAN Node Module?

My goal is to communicate over LoRa(WAN?) from one to another Raspberry Pi. As those hats do have an SPI interface I didn't find much for the communication (most posts are using a UART style interface). I'm already able to communicate with the hat and get the status with Python.

#!/usr/bin/env python

import time

import spidev
import RPi.GPIO as GPIO

# SX1262 pinout (BCM)
PIN_MOSI = 10
PIN_MISO = 9
PIN_CLK = 11
PIN_CS = 21
PIN_DIO1 = 16
PIN_DIO4 = 6
PIN_BUSY = 20
PIN_RST = 18

class SX1262:
    pin_cs = 21
    pin_dio1 = 16
    pin_dio4 = 6
    pin_busy = 20
    pin_rst = 18

    def __init__(self):
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.pin_cs, GPIO.OUT)
        GPIO.setup(self.pin_rst, GPIO.OUT)
        GPIO.setup(self.pin_dio1, GPIO.IN)
        GPIO.setup(self.pin_dio4, GPIO.IN)
        GPIO.setup(self.pin_busy, GPIO.IN)

        # Disable CS
        GPIO.output(self.pin_cs, GPIO.HIGH)

        # Reset SX1262
        GPIO.output(self.pin_rst, GPIO.LOW)
        time.sleep(0.1)
        GPIO.output(self.pin_rst, GPIO.HIGH)

        # Setup SPI interface
        self.spi = spidev.SpiDev()
        self.spi.open(0, 0)
        self.spi.no_cs = True
        self.spi.max_speed_hz = 1000000

    def __del__(self):
        self.spi.close()
        GPIO.cleanup()

    def send_raw(self, bytez):
        GPIO.output(self.pin_cs, GPIO.LOW)
        resp = self.spi.xfer2(bytez)
        GPIO.output(self.pin_cs, GPIO.HIGH)
        return resp

    def read_buffer(self):
        return self.send_raw([0x1E, 0x00])

    def get_status(self):
        return self.send_raw([0xC0])

    def get_packet_status(self):
        return self.send_raw([0x14])

    def get_stats(self):
        return self.send_raw([0x10])


def main():
    sx1262 = SX1262()

    try:
        while True:
            print("GetStatus")
            for b in sx1262.get_status():
                print(hex(b))
            print("GetPacketStatus")
            for b in sx1262.get_packet_status():
                print(hex(b))
            print("ReadBuffer")
            for i in range(5):
                for b in sx1262.read_buffer():
                    print(hex(b))
            print()
            time.sleep(1.0)
    except KeyboardInterrupt:
        pass
    del sx1262


if __name__ == "__main__":
    main()

But perhaps there is already a library and it would be much easier to get that implemented instead of implementing it myself?

0

There are 0 answers