LoRa868 Cap

Cap LoRa868 is a high-performance LoRa communication and GNSS global navigation expansion module designed for the Cardputer-Adv.

Support the following products:

LoRa868Cap

UiFlow2 Example

Sender

Open the cardputer_adv_lora868_cap_sender_example.m5f2 project in UiFlow2.

Use the keyboard to enter the text you want to send and press ENTER to send it.

UiFlow2 Code Block:

cardputer_adv_lora868_cap_sender_example.png

Example output:

None

Receiver

Open the cardputer_adv_lora868_cap_receiver_example.m5f2 project in UiFlow2.

This example receives and displays data.

UiFlow2 Code Block:

cardputer_adv_lora868_cap_receiver_example.png

Example output:

None

GPS Usage

Open the cardputer_adv_lora868_cap_gps_example.m5f2 project in UiFlow2.

This example demonstrates how to use the GPS functionality of the LoRa868 Cap.

UiFlow2 Code Block:

cardputer_adv_lora868_cap_gps_example.png

Example output:

None

MicroPython Example

Sender

Use the keyboard to enter the text you want to send and press ENTER to send it.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import MatrixKeyboard
 9from cap import LoRa868Cap
10from unit import KeyCode
11
12
13kb = None
14cap_lora868 = None
15
16
17keycode = None
18key = None
19buf = None
20
21
22def kb_pressed_event(kb_0):
23    global kb, cap_lora868, keycode, key, buf
24    keycode = kb.get_key()
25    if keycode >= 0x20 and keycode <= 0x7E:
26        key = chr(keycode)
27        buf = str(buf) + str(key)
28        M5.Lcd.printf(key)
29    elif keycode == (KeyCode.KEYCODE_ENTER):
30        cap_lora868.send(buf, None)
31        buf = ""
32        M5.Lcd.fillScreen(0x000000)
33        M5.Lcd.setCursor(0, 0)
34        M5.Lcd.printf("\n")
35        M5.Lcd.printf(">>> ")
36
37
38def setup():
39    global kb, cap_lora868, keycode, key, buf
40
41    M5.begin()
42    Widgets.fillScreen(0x000000)
43
44    cap_lora868 = LoRa868Cap(
45        freq_khz=868000,
46        bw="250",
47        sf=8,
48        coding_rate=8,
49        preamble_len=12,
50        syncword=0x12,
51        output_power=10,
52    )
53    kb = MatrixKeyboard()
54    kb.set_callback(kb_pressed_event)
55    M5.Lcd.setFont(Widgets.FONTS.DejaVu12)
56    M5.Lcd.setTextColor(0xFFFFFF, 0x000000)
57    M5.Lcd.setCursor(0, 0)
58    M5.Lcd.printf(">>> ")
59    buf = ""
60
61
62def loop():
63    global kb, cap_lora868, keycode, key, buf
64    M5.update()
65
66
67if __name__ == "__main__":
68    try:
69        setup()
70        while True:
71            loop()
72    except (Exception, KeyboardInterrupt) as e:
73        try:
74            from utility import print_error_msg
75
76            print_error_msg(e)
77        except ImportError:
78            print("please update to latest firmware")

Example output:

None

Receiver

This example receives and displays data.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8import time
 9from cap import LoRa868Cap
10
11
12cap_lora868 = None
13
14
15lora_data = None
16cur_time = None
17time_buf = None
18rx_text = None
19
20
21def cap_lora868_receive_event(received_data):
22    global cap_lora868, lora_data, cur_time, time_buf, rx_text
23    lora_data = received_data
24    cur_time = time.gmtime()
25    time_buf = ""
26    time_buf = str(time_buf) + str("[")
27    time_buf = str(time_buf) + str((cur_time[3]))
28    time_buf = str(time_buf) + str(":")
29    time_buf = str(time_buf) + str((cur_time[4]))
30    time_buf = str(time_buf) + str(":")
31    time_buf = str(time_buf) + str((cur_time[5]))
32    time_buf = str(time_buf) + str("] -> ")
33    M5.Lcd.print(time_buf, 0x33FF33)
34    rx_text = lora_data.decode()
35    M5.Lcd.print(rx_text, 0xFFFFFF)
36    M5.Lcd.printf("\n")
37
38
39def setup():
40    global cap_lora868, lora_data, cur_time, time_buf, rx_text
41
42    M5.begin()
43    Widgets.fillScreen(0x000000)
44
45    cap_lora868 = LoRa868Cap(
46        freq_khz=868000,
47        bw="250",
48        sf=8,
49        coding_rate=8,
50        preamble_len=12,
51        syncword=0x12,
52        output_power=10,
53    )
54    cap_lora868.set_irq_callback(cap_lora868_receive_event)
55    M5.Lcd.setFont(Widgets.FONTS.DejaVu12)
56    M5.Lcd.setCursor(0, 0)
57    M5.Lcd.setTextScroll(True)
58    cap_lora868.start_recv()
59
60
61def loop():
62    global cap_lora868, lora_data, cur_time, time_buf, rx_text
63    M5.update()
64
65
66if __name__ == "__main__":
67    try:
68        setup()
69        while True:
70            loop()
71    except (Exception, KeyboardInterrupt) as e:
72        try:
73            from utility import print_error_msg
74
75            print_error_msg(e)
76        except ImportError:
77            print("please update to latest firmware")

Example output:

None

GPS Usage

This example demonstrates how to use the GPS functionality of the LoRa868 Cap.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from cap import GPSCap
 9
10
11label0 = None
12label1 = None
13label2 = None
14cap_lora868 = None
15
16
17def setup():
18    global label0, label1, label2, cap_lora868
19
20    M5.begin()
21    Widgets.fillScreen(0x000000)
22    label0 = Widgets.Label("label0", 11, 16, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
23    label1 = Widgets.Label("label1", 11, 46, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
24    label2 = Widgets.Label("label2", 10, 77, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
25
26    cap_lora868 = GPSCap(id=2)
27
28
29def loop():
30    global label0, label1, label2, cap_lora868
31    M5.update()
32    label0.setText(str(cap_lora868.get_latitude()))
33    label1.setText(str(cap_lora868.get_longitude()))
34    label2.setText(str(cap_lora868.get_gps_time()))
35
36
37if __name__ == "__main__":
38    try:
39        setup()
40        while True:
41            loop()
42    except (Exception, KeyboardInterrupt) as e:
43        try:
44            from utility import print_error_msg
45
46            print_error_msg(e)
47        except ImportError:
48            print("please update to latest firmware")

Example output:

None

API

class LoRa868Cap

class cap.lora868.LoRa868Cap(freq_khz=868000, bw='250', sf=8, coding_rate=8, preamble_len=12, syncword=18, output_power=10)

Bases: object

Create a LoRa868Cap object.

Parameters:
  • pin_rst (int) – (RST) Reset pin number.

  • pin_cs (int) – (NSS) Chip select pin number.

  • pin_irq (int) – (IRQ) Interrupt pin number.

  • pin_busy (int) – (BUSY) Busy pin number.

  • freq_khz (int) – LoRa RF frequency in KHz, with a range of 850000 KHz to 930000 KHz.

  • bw (str) –

    Bandwidth, options include:

    • "7.8": 7.8 KHz

    • "10.4": 10.4 KHz

    • "15.6": 15.6 KHz

    • "20.8": 20.8 KHz

    • "31.25": 31.25 KHz

    • "41.7": 41.7 KHz

    • "62.5": 62.5 KHz

    • "125": 125 KHz

    • "250": 250 KHz

    • "500": 500 KHz

  • sf (int) – Spreading factor, range from 7 to 12. Higher spreading factors allow reception of weaker signals but with slower data rates.

  • coding_rate (int) – Forward Error Correction (FEC) coding rate expressed as 4/N, with a range from 5 to 8.

  • preamble_len (int) – Length of the preamble sequence in symbols, range from 0 to 255.

  • syncword (int) – Sync word to mark the start of the data frame, default is 0x12.

  • output_power (int) – Output power in dBm, range from -9 to 22.

UiFlow2 Code Block:

lora_init.png

MicroPython Code Block:

from cap import LoRa868Cap

cap_lora868_0 = LoRa868Cap(5, 1, 10, 2, 868000, '250', 8, 8, 12, 0x12, 10)
set_freq(freq_khz=868000)

Set frequency in kHz.

Parameters:

freq_khz (int) – Frequency in kHz (850000 ~ 930000), default is 868000.

Return type:

None

UiFlow2 Code Block:

set_freq.png

MicroPython Code Block:

module_lora868v12_0.set_freq(868000)
set_sf(sf)

Set spreading factor (SF).

Parameters:

sf (int) – Spreading factor (7 ~ 12)

Return type:

None

UiFlow2 Code Block:

set_sf.png

MicroPython Code Block:

module_lora868v12_0.set_sf(7)
set_bw(bw)

Set bandwidth.

Parameters:

bw (str) – Bandwidth in kHz as string. Must be one of: ‘7.8’, ‘10.4’, ‘15.6’, ‘20.8’, ‘31.25’, ‘41.7’, ‘62.5’, ‘125’, ‘250’, ‘500’.

Return type:

None

UiFlow2 Code Block:

set_bw.png

MicroPython Code Block:

module_lora868v12_0.set_bw(bw)
set_coding_rate(coding_rate)

Set coding rate.

Parameters:

coding_rate (int) – Coding rate (5 ~ 8)

Return type:

None

UiFlow2 Code Block:

set_coding_rate.png

MicroPython Code Block:

module_lora868v12_0.set_coding_rate(coding_rate)
set_syncword(syncword)

Set syncword.

Parameters:

syncword (int) – Sync word (0 ~ 0xFF)

Return type:

None

UiFlow2 Code Block:

set_syncword.png

MicroPython Code Block:

module_lora868v12_0.set_syncword(syncword)
set_preamble_len(preamble_len)

Set preamble length.

Parameters:

preamble_len (int) – Preamble length, range: 0~255.

Return type:

None

UiFlow2 Code Block:

set_preamble_len.png

MicroPython Code Block:

module_lora868v12_0.set_preamble_len(preamble_len)
set_output_power(output_power)

Set output power in dBm.

Parameters:

output_power (int) – Output power in dBm (-9 ~ 22)

Return type:

None

UiFlow2 Code Block:

set_output_power.png

MicroPython Code Block:

module_lora868v12_0.set_output_power(output_power)
send(packet, tx_at_ms=None)

Send data

Parameters:
  • packet (str | list | tuple | int | bytearray) – The data to be sent.

  • tx_at_ms (int) – The timestamp in milliseconds when to send the data (optional). Default is None.

  • packet

Returns:

timestamp

Return type:

int

Send a data packet and return the timestamp after the packet is sent.

UiFlow2 Code Block:

send.png

MicroPython Code Block:

module_lora868v12_0.send()
recv(timeout_ms=None, rx_length=255, rx_packet=None)

Receive data

Parameters:
  • timeout_ms (int) – Timeout in milliseconds (optional). Default is None.

  • rx_length (int) – Length of the data to be read. Default is 0xFF.

  • rx_packet (RxPacket) – An instance of RxPacket (optional) to reuse.

Returns:

Received packet instance

Return type:

RxPacket

Attempt to receive a LoRa packet. Returns None if timeout occurs, or returns the received packet instance.

UiFlow2 Code Block:

recv.png

MicroPython Code Block:

data = module_lora868v12_0.recv()
start_recv()

Start receive data

This method initiates the process to begin receiving data.

UiFlow2 Code Block:

start_recv.png

MicroPython Code Block:

module_lora868v12_0.start_recv()
Return type:

None

set_irq_callback(callback)

Set the interrupt callback function to be executed on IRQ.

Parameters:

callback – The callback function to be invoked when the interrupt is triggered. The callback should not take any arguments and should return nothing.

Return type:

None

UiFlow2 Code Block:

set_irq_callback.png

MicroPython Code Block:

module_lora868v12_0.set_irq_callback()
standby()

Set module to standby mode.

Puts the LoRa module into standby mode, consuming less power.

UiFlow2 Code Block:

standby.png

MicroPython Code Block:

module_lora868v12_0.standby()
Return type:

None

sleep()

Set module to sleep mode.

Reduces the power consumption by putting the module into deep sleep mode.

UiFlow2 Code Block:

sleep.png

MicroPython Code Block:

module_lora868v12_0.sleep()
Return type:

None

irq_triggered()

Check IRQ trigger.

Returns:

Returns True if an interrupt service routine (ISR) has been triggered since the last send or receive started.

Return type:

bool

UiFlow2 Code Block:

irq_triggered.png

MicroPython Code Block:

module_lora868v12_0.irq_triggered()

class GPSCap

class cap.lora868.GPSCap(id=1, rx=None, tx=None, pps=-1)

Bases: ATGM336H

Initialize the GPSCap with a specific UART id and port for communication.

Parameters:
  • id (int) – The UART ID for communication with the GPS module. It can be 0, 1, or 2.

  • rx (int) – The RX pin for UART communication. If None, uses default pin from board definition.

  • tx (int) – The TX pin for UART communication. If None, uses default pin from board definition.

  • pps (int) – The PPS (Pulse Per Second) pin, used for high-precision time synchronization. Default is -1 (not used).

UiFlow2 Code Block:

gps_init.png

MicroPython Code Block:

from cap import GPSCap
gps_0 = GPSCap(id=2)