|
| 1 | +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +# Example to send a msgpack'd data packet periodically |
| 5 | +# Author: Jerry Needell, Tim Cocks |
| 6 | +# |
| 7 | +import time |
| 8 | +from io import BytesIO |
| 9 | + |
| 10 | +import board |
| 11 | +import busio |
| 12 | +import digitalio |
| 13 | +import msgpack |
| 14 | + |
| 15 | +# Dictionary object that we will msgpack and send over the radio |
| 16 | +payload_obj = {"counter": 0, "list": [True, False, None, 1, 3.14], "str": "CircuitPython is Fun!"} |
| 17 | + |
| 18 | +# Define radio parameters. |
| 19 | +RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your |
| 20 | +# module! Can be a value like 915.0, 433.0, etc. |
| 21 | + |
| 22 | +# Define pins connected to the chip, use these if wiring up the breakout according to the guide: |
| 23 | +CS = digitalio.DigitalInOut(board.D10) |
| 24 | +RESET = digitalio.DigitalInOut(board.D11) |
| 25 | + |
| 26 | +# Initialize SPI bus. |
| 27 | +spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) |
| 28 | + |
| 29 | +# Initialze RFM radio |
| 30 | +# uncommnet the desired import and rfm initialization depending on the radio boards being used |
| 31 | + |
| 32 | +# Use rfm9x for two RFM9x radios using LoRa |
| 33 | + |
| 34 | +# from adafruit_rfm import rfm9x |
| 35 | + |
| 36 | +# rfm = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ) |
| 37 | + |
| 38 | +# Use rfm9xfsk for two RFM9x radios or RFM9x to RFM69 using FSK |
| 39 | + |
| 40 | +from adafruit_rfm import rfm9xfsk |
| 41 | + |
| 42 | +rfm = rfm9xfsk.RFM9xFSK(spi, CS, RESET, RADIO_FREQ_MHZ) |
| 43 | + |
| 44 | +# Use rfm69 for two RFM69 radios using FSK |
| 45 | + |
| 46 | +# from adafruit_rfm import rfm69 |
| 47 | + |
| 48 | +# rfm = rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ) |
| 49 | + |
| 50 | +# For RFM69 only: Optionally set an encryption key (16 byte AES key). MUST match both |
| 51 | +# on the transmitter and receiver (or be set to None to disable/the default). |
| 52 | +# rfm.encryption_key = None |
| 53 | +# rfm.encryption_key = ( |
| 54 | +# b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08" |
| 55 | +# ) |
| 56 | + |
| 57 | +# for OOK on RFM69 or RFM9xFSK |
| 58 | +# rfm.modulation_type = 1 |
| 59 | + |
| 60 | +# uncommnet to Disable the RadioHead Header |
| 61 | +# rfm.radiohead = False |
| 62 | + |
| 63 | +# in FSK/OOK modes rfo RFM69 or RFM9X - addresss filtering may be enabled |
| 64 | +# rfm.enable_address_filter=True |
| 65 | +# rfm.fsk_node_address=0x2 |
| 66 | +# rfm.fsk_broadcast_address=0xff |
| 67 | + |
| 68 | +# set the time interval (seconds) for sending packets |
| 69 | +transmit_interval = 5 |
| 70 | + |
| 71 | +# Note that the radio is configured in LoRa mode so you can't control sync |
| 72 | +# word, encryption, frequency deviation, or other settings! |
| 73 | + |
| 74 | +# You can however adjust the transmit power (in dB). The default is 13 dB but |
| 75 | +# high power radios like the RFM95 can go up to 23 dB: |
| 76 | +rfm.tx_power = 23 |
| 77 | + |
| 78 | + |
| 79 | +# initialize counter |
| 80 | +counter = 0 |
| 81 | + |
| 82 | +# Wait to receive packets. |
| 83 | +print("Waiting for packets...") |
| 84 | +# initialize flag and timer |
| 85 | +send_reading = False |
| 86 | +time_now = time.monotonic() |
| 87 | +while True: |
| 88 | + # Look for a new packet - wait up to 2 seconds: |
| 89 | + packet = rfm.receive(timeout=2.0) |
| 90 | + # If no packet was received during the timeout then None is returned. |
| 91 | + if packet is not None: |
| 92 | + # Received a packet! |
| 93 | + print("Received (raw data): ", packet) |
| 94 | + |
| 95 | + try: |
| 96 | + # Unpack and print contents |
| 97 | + b = BytesIO() |
| 98 | + b.write(packet) |
| 99 | + b.seek(0) |
| 100 | + unpacked_msg = msgpack.unpack(b) |
| 101 | + print(f"Received (unpacked): {unpacked_msg}") |
| 102 | + except Exception as e: |
| 103 | + print("Unable to unpack message. Exception: ") |
| 104 | + print(e) |
| 105 | + |
| 106 | + # send reading after any packet received |
| 107 | + if time.monotonic() - time_now > transmit_interval: |
| 108 | + # reset timeer |
| 109 | + time_now = time.monotonic() |
| 110 | + # clear flag to send data |
| 111 | + send_reading = False |
| 112 | + counter = counter + 1 |
| 113 | + |
| 114 | + b = BytesIO() |
| 115 | + payload_obj["counter"] = counter |
| 116 | + msgpack.pack(payload_obj, b) |
| 117 | + b.seek(0) |
| 118 | + rfm.send(b.read()) |
0 commit comments