Last active
December 6, 2023 04:36
-
-
Save abuvanth/9ae8ec6f09e4c371108366da71fd20d4 to your computer and use it in GitHub Desktop.
This file contains 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
from machine import Pin, SoftSPI | |
from sx127x import SX127x #https://github.com/abuvanth/micropython-sx127x/blob/master/sx127x.py | |
import neopixel | |
from time import sleep | |
# Define the pins (adjust based on your ESP32 board and wiring) | |
MOSI_PIN = 4 | |
MISO_PIN = 0 | |
SCK_PIN = 1 | |
NSS_PIN = 5 | |
DIO0_PIN = 10 | |
pin = Pin(7, Pin.OUT) # Example pin number | |
np = neopixel.NeoPixel(pin, 1) | |
# Configure the SPI interface | |
spi = SoftSPI(baudrate=10000000, polarity=0, phase=0, bits=8, firstbit=SoftSPI.MSB, sck=Pin(SCK_PIN), mosi=Pin(MOSI_PIN), miso=Pin(MISO_PIN)) | |
# Configure NSS and DIO0 | |
nss = Pin(NSS_PIN, mode=Pin.OUT, value=1) | |
dio0 = Pin(DIO0_PIN, mode=Pin.IN) | |
# LoRa configuration - should match the sender's configuration | |
lora_config = { | |
'frequency': 433E6, | |
'tx_power_level': 20, | |
'signal_bandwidth': 125E3, | |
'spreading_factor': 12, | |
'coding_rate': 5, | |
'preamble_length': 12, | |
'implicit_header': False, | |
'sync_word': 0x12, | |
'enable_crc': True, | |
'invert_iq': False, | |
} | |
# Initialize LoRa | |
lora = SX127x(spi, pins={'miso': MISO_PIN, 'mosi': MOSI_PIN, 'ss': NSS_PIN, 'sck': SCK_PIN, 'dio_0': DIO0_PIN}, parameters=lora_config) | |
# Function to receive a message | |
def on_receive(lora, payload): | |
try: | |
msg = payload.decode() | |
rssi_value = lora.packetRssi() # Get the RSSI value | |
if rssi_value >= -50: | |
np[0] = (0, 255, 0) # Green for very strong signal | |
elif -70 <= rssi_value < -50: | |
np[0] = (0, 0, 255) # blue for strong signal | |
elif -90 <= rssi_value < -70: | |
np[0] = (0, 255, 255) # purple for moderate signal | |
elif -110 <= rssi_value < -90: | |
np[0] = (255, 0, 0) # Red for weak signal | |
else: | |
np[0] = (128, 128, 128) # Grey for very weak or no signal | |
np.write() | |
print("Received message:", msg) | |
except Exception as e: | |
print(e) | |
np[0] = (0, 0, 0) # off | |
np.write() | |
lora.onReceive(on_receive) | |
# Set the LoRa module to listen mode | |
lora.receive() | |
while True: | |
# Your main loop where other tasks can be performed | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment