-
Notifications
You must be signed in to change notification settings - Fork 51
enable usage of interrupts with received packets #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Example using Interrupts to send a message and then wait indefinitely for messages | ||
# to be received. Interrupts are used only for receive. sending is done with polling. | ||
# This example is for systems that support interrupts like the Raspberry Pi with "blinka" | ||
# CircuitPython does not support interrupts so it will not work on Circutpython boards | ||
# Author: Tony DiCola, Jerry Needell | ||
import time | ||
import board | ||
import busio | ||
import digitalio | ||
import RPi.GPIO as io | ||
import adafruit_rfm9x | ||
|
||
|
||
# setup interrupt callback function | ||
def rfm9x_callback(rfm9x_irq): | ||
global packet_received #pylint: disable=global-statement | ||
print("IRQ detected ",rfm9x_irq, rfm9x.rx_done) | ||
# check to see if this was a rx interrupt - ignore tx | ||
if rfm9x.rx_done: | ||
packet = rfm9x.receive(timeout = None) | ||
if packet is not None: | ||
packet_received = True | ||
# Received a packet! | ||
# Print out the raw bytes of the packet: | ||
print('Received (raw bytes): {0}'.format(packet)) | ||
print([hex(x) for x in packet]) | ||
print('RSSI: {0}'.format(rfm9x.rssi)) | ||
|
||
|
||
# Define radio parameters. | ||
RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your | ||
# module! Can be a value like 915.0, 433.0, etc. | ||
|
||
# Define pins connected to the chip, use these if wiring up the breakout according to the guide: | ||
CS = digitalio.DigitalInOut(board.CE1) | ||
RESET = digitalio.DigitalInOut(board.D25) | ||
|
||
# Initialize SPI bus. | ||
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) | ||
|
||
# Initialze RFM radio | ||
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ) | ||
|
||
# Note that the radio is configured in LoRa mode so you can't control sync | ||
# word, encryption, frequency deviation, or other settings! | ||
|
||
# You can however adjust the transmit power (in dB). The default is 13 dB but | ||
# high power radios like the RFM95 can go up to 23 dB: | ||
rfm9x.tx_power = 23 | ||
|
||
# configure the interrupt pin and event handling. | ||
RFM9X_G0 = 22 | ||
io.setmode(io.BCM) | ||
io.setup(RFM9X_G0, io.IN,pull_up_down=io.PUD_DOWN) # activate input | ||
io.add_event_detect(RFM9X_G0,io.RISING) | ||
io.add_event_callback(RFM9X_G0,rfm9x_callback) | ||
|
||
packet_received = False | ||
# Send a packet. Note you can only send a packet up to 252 bytes in length. | ||
# This is a limitation of the radio packet size, so if you need to send larger | ||
# amounts of data you will need to break it into smaller send calls. Each send | ||
# call will wait for the previous one to finish before continuing. | ||
rfm9x.send(bytes("Hello world!\r\n","utf-8"), keep_listening = True) | ||
print('Sent Hello World message!') | ||
|
||
# Wait to receive packets. Note that this library can't receive data at a fast | ||
# rate, in fact it can only receive and process one 252 byte packet at a time. | ||
# This means you should only use this for low bandwidth scenarios, like sending | ||
# and receiving a single message at a time. | ||
print('Waiting for packets...') | ||
while True: | ||
time.sleep(.1) | ||
if packet_received: | ||
print('received message!') | ||
packet_received = False |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Example to send a packet periodically | ||
# Author: Jerry Needell | ||
# | ||
import time | ||
import board | ||
import busio | ||
import digitalio | ||
import adafruit_rfm9x | ||
|
||
# set the time interval (seconds) for sending packets | ||
transmit_interval=10 | ||
jerryneedell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Define radio parameters. | ||
RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your | ||
# module! Can be a value like 915.0, 433.0, etc. | ||
|
||
# Define pins connected to the chip. | ||
CS = digitalio.DigitalInOut(board.CE1) | ||
RESET = digitalio.DigitalInOut(board.D25) | ||
|
||
# Initialize SPI bus. | ||
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) | ||
|
||
# Initialze RFM radio | ||
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ) | ||
|
||
# Note that the radio is configured in LoRa mode so you can't control sync | ||
# word, encryption, frequency deviation, or other settings! | ||
|
||
# You can however adjust the transmit power (in dB). The default is 13 dB but | ||
# high power radios like the RFM95 can go up to 23 dB: | ||
rfm9x.tx_power = 23 | ||
|
||
|
||
# initialize counter | ||
counter = 0 | ||
#send a broadcast mesage | ||
rfm9x.send(bytes("message number {}".format(counter),"UTF-8")) | ||
|
||
# Wait to receive packets. | ||
print('Waiting for packets...') | ||
#initialize flag and timer | ||
send_reading=False | ||
time_now=time.monotonic() | ||
while True: | ||
# Look for a new packet - wait up to 5 seconds: | ||
packet = rfm9x.receive(timeout=5.0) | ||
# If no packet was received during the timeout then None is returned. | ||
if packet is not None: | ||
# Received a packet! | ||
# Print out the raw bytes of the packet: | ||
print('Received (raw bytes): {0}'.format(packet)) | ||
# send reading after any packet received | ||
if time.monotonic()-time_now>transmit_interval: | ||
#reset timeer | ||
time_now=time.monotonic() | ||
#clear flag to send data | ||
send_reading=False | ||
counter = counter + 1 | ||
rfm9x.send(bytes("message number {}".format(counter),"UTF-8")) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.