rfm9x_rpi_interrupt.py
1 # Example using Interrupts to send a message and then wait indefinitely for messages 2 # to be received. Interrupts are used only for receive. sending is done with polling. 3 # This example is for systems that support interrupts like the Raspberry Pi with "blinka" 4 # CircuitPython does not support interrupts so it will not work on Circutpython boards 5 # Author: Tony DiCola, Jerry Needell 6 import time 7 import board 8 import busio 9 import digitalio 10 import RPi.GPIO as io 11 import adafruit_rfm9x 12 13 14 # setup interrupt callback function 15 def rfm9x_callback(rfm9x_irq): 16 global packet_received # pylint: disable=global-statement 17 print("IRQ detected ", rfm9x_irq, rfm9x.rx_done) 18 # check to see if this was a rx interrupt - ignore tx 19 if rfm9x.rx_done: 20 packet = rfm9x.receive(timeout=None) 21 if packet is not None: 22 packet_received = True 23 # Received a packet! 24 # Print out the raw bytes of the packet: 25 print("Received (raw bytes): {0}".format(packet)) 26 print([hex(x) for x in packet]) 27 print("RSSI: {0}".format(rfm9x.last_rssi)) 28 29 30 # Define radio parameters. 31 RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your 32 # module! Can be a value like 915.0, 433.0, etc. 33 34 # Define pins connected to the chip, use these if wiring up the breakout according to the guide: 35 CS = digitalio.DigitalInOut(board.CE1) 36 RESET = digitalio.DigitalInOut(board.D25) 37 38 # Initialize SPI bus. 39 spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) 40 41 # Initialze RFM radio 42 rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ) 43 44 # Note that the radio is configured in LoRa mode so you can't control sync 45 # word, encryption, frequency deviation, or other settings! 46 47 # You can however adjust the transmit power (in dB). The default is 13 dB but 48 # high power radios like the RFM95 can go up to 23 dB: 49 rfm9x.tx_power = 23 50 51 # configure the interrupt pin and event handling. 52 RFM9X_G0 = 22 53 io.setmode(io.BCM) 54 io.setup(RFM9X_G0, io.IN, pull_up_down=io.PUD_DOWN) # activate input 55 io.add_event_detect(RFM9X_G0, io.RISING) 56 io.add_event_callback(RFM9X_G0, rfm9x_callback) 57 58 packet_received = False 59 # Send a packet. Note you can only send a packet up to 252 bytes in length. 60 # This is a limitation of the radio packet size, so if you need to send larger 61 # amounts of data you will need to break it into smaller send calls. Each send 62 # call will wait for the previous one to finish before continuing. 63 rfm9x.send(bytes("Hello world!\r\n", "utf-8"), keep_listening=True) 64 print("Sent Hello World message!") 65 66 # Wait to receive packets. Note that this library can't receive data at a fast 67 # rate, in fact it can only receive and process one 252 byte packet at a time. 68 # This means you should only use this for low bandwidth scenarios, like sending 69 # and receiving a single message at a time. 70 print("Waiting for packets...") 71 while True: 72 time.sleep(0.1) 73 if packet_received: 74 print("received message!") 75 packet_received = False