See:
Last active
November 10, 2022 07:43
-
-
Save projectgus/aa050dd75ecfddeb6c2bfcd82a36bd6d to your computer and use it in GitHub Desktop.
Basic examples of MicroPython LoRa modem driver use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import uasyncio | |
import async_modem | |
# these match the default's in Heltec's library, apart from freq_khz | |
lora_cfg = { | |
"sf": 11, | |
"bw": 125, | |
"freq_khz": 917000, | |
"coding_rate": 5, | |
"preamble_len": 8, | |
"invert_iq_tx": False, | |
"syncword": 0x12, | |
} | |
def modem_sx1276(): | |
from machine import SPI, Pin | |
import sx1276 | |
spi = SPI( | |
0, | |
baudrate=2000_000, | |
polarity=0, | |
phase=0, | |
sck=Pin(6), | |
mosi=Pin(7), | |
miso=Pin(4), | |
) | |
cs = Pin(9, mode=Pin.OUT, value=1) | |
# TODO:" this seems to be a bug in the SX1276 compared to SX1262 | |
lora_cfg["invert_iq_tx"] = not lora_cfg["invert_iq_tx"] | |
return sx1276.SX1276(spi, cs, | |
dio0=Pin(10, mode=Pin.IN), | |
dio1=Pin(11, mode=Pin.IN), | |
radio_reset=Pin(13, mode=Pin.OPEN_DRAIN), | |
lora_cfg=lora_cfg) | |
def modem_sx1262(): | |
from machine import SPI, Pin | |
import sx1262 | |
# Parameters match the Waveshare Pico-LoRa-SX1262 module | |
spi = SPI( | |
1, | |
baudrate=2000_000, | |
polarity=0, | |
phase=0, | |
sck=Pin(10), | |
mosi=Pin(11), | |
miso=Pin(12), | |
) | |
cs = Pin(3, mode=Pin.OUT, value=1) | |
return sx1262.SX1262(spi, cs, | |
busy=Pin(2, mode=Pin.IN), | |
dio1=Pin(20, mode=Pin.IN), | |
radio_reset=Pin(15, mode=Pin.OUT), | |
dio2_rf_sw=True, | |
dio3_tcxo_millivolts=3300, | |
lora_cfg=lora_cfg) | |
def modem_stm32wl5(): | |
import stm32wl5 | |
return stm32wl5.WL55SubGhzModem(lora_cfg) | |
# m = modem_stm32wl5() | |
m = modem_sx1276() | |
# m = modem_sx1262() | |
m = async_modem.AsyncModem(m) | |
async def rxtx_forever_coro(m): | |
print("Running with async...") | |
while True: | |
print("Receiving...") | |
ts = time.ticks_ms() | |
message = await m.receive(2000) | |
if message: | |
print("Message: {!r}".format(message)) | |
else: | |
print("Timeout after {}ms".format(time.ticks_ms() - ts)) | |
print("Transmitting...") | |
await m.transmit(b"Hello, I'm async MicroPython") | |
uasyncio.run(rxtx_forever_coro(m)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
# these match the default's in Heltec's library, apart from freq_khz | |
lora_cfg = { | |
"sf": 11, | |
"bw": 125, | |
"freq_khz": 917000, | |
"coding_rate": 5, | |
"preamble_len": 8, | |
"invert_iq_tx": False, | |
"syncword": 0x12, | |
} | |
def modem_sx1276(): | |
from machine import SPI, Pin | |
import sx1276 | |
spi = SPI( | |
0, | |
baudrate=500_000, | |
polarity=0, | |
phase=0, | |
sck=Pin(6), | |
mosi=Pin(7), | |
miso=Pin(4), | |
) | |
cs = Pin(9, mode=Pin.OUT, value=1) | |
# TODO:" this seems to be a bug in the SX1276 compared to SX1262 | |
lora_cfg["invert_iq_tx"] = not lora_cfg["invert_iq_tx"] | |
return sx1276.SX1276(spi, cs, | |
dio0=Pin(10, mode=Pin.IN), | |
dio1=Pin(11, mode=Pin.IN), | |
radio_reset=Pin(13, mode=Pin.OPEN_DRAIN), | |
lora_cfg=lora_cfg) | |
def modem_sx1262(): | |
from machine import SPI, Pin | |
import sx1262 | |
# Parameters match the Waveshare Pico-LoRa-SX1262 module | |
spi = SPI( | |
1, | |
baudrate=2000_000, | |
polarity=0, | |
phase=0, | |
sck=Pin(10), | |
mosi=Pin(11), | |
miso=Pin(12), | |
) | |
cs = Pin(3, mode=Pin.OUT, value=1) | |
return sx1262.SX1262(spi, cs, | |
busy=Pin(2, mode=Pin.IN), | |
dio1=Pin(20, mode=Pin.IN), | |
radio_reset=Pin(15, mode=Pin.OUT), | |
dio2_rf_sw=True, | |
dio3_tcxo_millivolts=3300, | |
lora_cfg=lora_cfg) | |
def modem_stm32wl5(): | |
import stm32wl5 | |
return stm32wl5.WL55SubGhzModem(lora_cfg) | |
# m = modem_stm32wl5() | |
# m = modem_sx1276() | |
m = modem_sx1262() | |
def rxtx_sync(m): | |
while True: | |
ts = time.ticks_ms() | |
print("Receiving...") | |
message = m.receive(2000) | |
if message: | |
print("Message: {!r}".format(message)) | |
else: | |
print("Timeout after {}ms".format(time.ticks_ms() - ts)) | |
print("Transmitting...") | |
m.transmit(b"Hello, I'm sync MicroPython") | |
rxtx_sync(m) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment