Simple test

Ensure your device works with this simple test.

examples/tinylora_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import busio
 6import digitalio
 7import board
 8from adafruit_tinylora.adafruit_tinylora import TTN, TinyLoRa
 9
10# Board LED
11led = digitalio.DigitalInOut(board.D13)
12led.direction = digitalio.Direction.OUTPUT
13
14spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
15
16# RFM9x Breakout Pinouts
17cs = digitalio.DigitalInOut(board.D5)
18irq = digitalio.DigitalInOut(board.D6)
19rst = digitalio.DigitalInOut(board.D4)
20
21# Feather M0 RFM9x Pinouts
22# cs = digitalio.DigitalInOut(board.RFM9X_CS)
23# irq = digitalio.DigitalInOut(board.RFM9X_D0)
24# rst = digitalio.DigitalInOut(board.RFM9X_RST)
25
26# TTN Device Address, 4 Bytes, MSB
27devaddr = bytearray([0x00, 0x00, 0x00, 0x00])
28
29# TTN Network Key, 16 Bytes, MSB
30nwkey = bytearray(
31    [
32        0x00,
33        0x00,
34        0x00,
35        0x00,
36        0x00,
37        0x00,
38        0x00,
39        0x00,
40        0x00,
41        0x00,
42        0x00,
43        0x00,
44        0x00,
45        0x00,
46        0x00,
47        0x00,
48    ]
49)
50
51# TTN Application Key, 16 Bytess, MSB
52app = bytearray(
53    [
54        0x00,
55        0x00,
56        0x00,
57        0x00,
58        0x00,
59        0x00,
60        0x00,
61        0x00,
62        0x00,
63        0x00,
64        0x00,
65        0x00,
66        0x00,
67        0x00,
68        0x00,
69        0x00,
70    ]
71)
72
73ttn_config = TTN(devaddr, nwkey, app, country="US")
74
75lora = TinyLoRa(spi, cs, irq, rst, ttn_config)
76
77while True:
78    data = bytearray(b"\x43\x57\x54\x46")
79    print("Sending packet...")
80    lora.send_data(data, len(data), lora.frame_counter)
81    print("Packet sent!")
82    led.value = True
83    lora.frame_counter += 1
84    time.sleep(1)
85    led.value = False

Using TinyLora with a Sensor

Using TinyLoRa with a Si7021 Sensor

examples/tinylora_simpletest_si7021.py
  1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
  2# SPDX-License-Identifier: MIT
  3
  4"""Using TinyLoRa with a Si7021 Sensor.
  5"""
  6import time
  7import busio
  8import digitalio
  9import board
 10import adafruit_si7021
 11from adafruit_tinylora.adafruit_tinylora import TTN, TinyLoRa
 12
 13# Board LED
 14led = digitalio.DigitalInOut(board.D13)
 15led.direction = digitalio.Direction.OUTPUT
 16
 17# Create library object using our bus i2c port for si7021
 18i2c = busio.I2C(board.SCL, board.SDA)
 19sensor = adafruit_si7021.SI7021(i2c)
 20
 21# Create library object using our bus SPI port for radio
 22spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
 23
 24# RFM9x Breakout Pinouts
 25cs = digitalio.DigitalInOut(board.D5)
 26irq = digitalio.DigitalInOut(board.D6)
 27rst = digitalio.DigitalInOut(board.D4)
 28
 29# Feather M0 RFM9x Pinouts
 30# cs = digitalio.DigitalInOut(board.RFM9X_CS)
 31# irq = digitalio.DigitalInOut(board.RFM9X_D0)
 32# rst = digitalio.DigitalInOut(board.RFM9X_RST)
 33
 34# TTN Device Address, 4 Bytes, MSB
 35devaddr = bytearray([0x00, 0x00, 0x00, 0x00])
 36
 37# TTN Network Key, 16 Bytes, MSB
 38nwkey = bytearray(
 39    [
 40        0x00,
 41        0x00,
 42        0x00,
 43        0x00,
 44        0x00,
 45        0x00,
 46        0x00,
 47        0x00,
 48        0x00,
 49        0x00,
 50        0x00,
 51        0x00,
 52        0x00,
 53        0x00,
 54        0x00,
 55        0x00,
 56    ]
 57)
 58
 59# TTN Application Key, 16 Bytess, MSB
 60app = bytearray(
 61    [
 62        0x00,
 63        0x00,
 64        0x00,
 65        0x00,
 66        0x00,
 67        0x00,
 68        0x00,
 69        0x00,
 70        0x00,
 71        0x00,
 72        0x00,
 73        0x00,
 74        0x00,
 75        0x00,
 76        0x00,
 77        0x00,
 78    ]
 79)
 80
 81ttn_config = TTN(devaddr, nwkey, app, country="US")
 82
 83lora = TinyLoRa(spi, cs, irq, rst, ttn_config)
 84
 85# Data Packet to send to TTN
 86data = bytearray(4)
 87
 88while True:
 89    temp_val = sensor.temperature
 90    humid_val = sensor.relative_humidity
 91    print("Temperature: %0.2f C" % temp_val)
 92    print("relative humidity: %0.1f %%" % humid_val)
 93
 94    # Encode float as int
 95    temp_val = int(temp_val * 100)
 96    humid_val = int(humid_val * 100)
 97
 98    # Encode payload as bytes
 99    data[0] = (temp_val >> 8) & 0xFF
100    data[1] = temp_val & 0xFF
101    data[2] = (humid_val >> 8) & 0xFF
102    data[3] = humid_val & 0xFF
103
104    # Send data packet
105    print("Sending packet...")
106    lora.send_data(data, len(data), lora.frame_counter)
107    print("Packet Sent!")
108    led.value = True
109    lora.frame_counter += 1
110    time.sleep(2)
111    led.value = False

Using a single Channel

Ensure your device works with this simple test.

examples/tinylora_simpletest_single_channel.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import busio
 6import digitalio
 7import board
 8from adafruit_tinylora.adafruit_tinylora import TTN, TinyLoRa
 9
10# Board LED
11led = digitalio.DigitalInOut(board.D13)
12led.direction = digitalio.Direction.OUTPUT
13
14spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
15
16# RFM9x Breakout Pinouts
17cs = digitalio.DigitalInOut(board.D5)
18irq = digitalio.DigitalInOut(board.D6)
19rst = digitalio.DigitalInOut(board.D4)
20
21# Feather M0 RFM9x Pinouts
22# cs = digitalio.DigitalInOut(board.RFM9X_CS)
23# irq = digitalio.DigitalInOut(board.RFM9X_D0)
24# rst = digitalio.DigitalInOut(board.RFM9X_RST)
25
26# TTN Device Address, 4 Bytes, MSB
27devaddr = bytearray([0x00, 0x00, 0x00, 0x00])
28
29# TTN Network Key, 16 Bytes, MSB
30nwkey = bytearray(
31    [
32        0x00,
33        0x00,
34        0x00,
35        0x00,
36        0x00,
37        0x00,
38        0x00,
39        0x00,
40        0x00,
41        0x00,
42        0x00,
43        0x00,
44        0x00,
45        0x00,
46        0x00,
47        0x00,
48    ]
49)
50
51# TTN Application Key, 16 Bytess, MSB
52app = bytearray(
53    [
54        0x00,
55        0x00,
56        0x00,
57        0x00,
58        0x00,
59        0x00,
60        0x00,
61        0x00,
62        0x00,
63        0x00,
64        0x00,
65        0x00,
66        0x00,
67        0x00,
68        0x00,
69        0x00,
70    ]
71)
72
73ttn_config = TTN(devaddr, nwkey, app, country="US")
74
75# Broadcasting on channel 0 in US Region - 903.9 MHz
76lora = TinyLoRa(spi, cs, irq, rst, ttn_config, channel=0)
77
78while True:
79    data = bytearray(b"\x43\x57\x54\x46")
80    print("Sending packet...")
81    lora.send_data(data, len(data), lora.frame_counter)
82    print("Packet sent!")
83    led.value = True
84    lora.frame_counter += 1
85    time.sleep(1)
86    led.value = False