Last active
December 6, 2023 04:35
-
-
Save abuvanth/5ed444001157aad89b09cb28e5bba77e 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, SPI | |
from sx127x import SX127x #https://github.com/abuvanth/micropython-sx127x/blob/master/sx127x.py | |
import time | |
import neopixel | |
# 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 = SPI(baudrate=10000000, polarity=0, phase=0, bits=8, firstbit=SPI.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 | |
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) | |
# Example function to send a message | |
def send_message(): | |
np[0] = (255, 0, 0) # Red color | |
np.write() | |
message = "Hello LoRa from 1" | |
lora.println(message) | |
np[0] = (0, 0, 0) # Red color | |
np.write() | |
print("Message sent: ", message) | |
while 1: | |
send_message() | |
time.sleep(2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment