Giter VIP home page Giter VIP logo

sx127x_lora's Introduction

sx127x_lora

A platform-agnostic driver for Semtech SX1276/77/78/79 based boards. It supports any device that implements the embedded-hal traits. Devices are connected over SPI and require an extra GPIO pin for RESET. This cate works with any Semtech based board including:

  • Modtronix inAir4, inAir9, and inAir9B
  • HopeRF RFM95W, RFM96W, and RFM98W

Examples

Raspberry Pi Basic Send

Utilizes a Raspberry Pi to send a message. The example utilizes the linux_embedded_hal crate.

#![feature(extern_crate_item_prelude)]
extern crate sx127x_lora;
extern crate linux_embedded_hal as hal;

use hal::spidev::{self, SpidevOptions};
use hal::{Pin, Spidev};
use hal::sysfs_gpio::Direction;
use hal::Delay;

const LORA_CS_PIN: u64 = 8;
const LORA_RESET_PIN: u64 = 21;
const FREQUENCY: i64 = 915;

fn main(){

    let mut spi = Spidev::open("/dev/spidev0.0").unwrap();
    let options = SpidevOptions::new()
        .bits_per_word(8)
        .max_speed_hz(20_000)
        .mode(spidev::SPI_MODE_0)
        .build();
    spi.configure(&options).unwrap();

    let cs = Pin::new(LORA_CS_PIN);
    cs.export().unwrap();
    cs.set_direction(Direction::Out).unwrap();

    let reset = Pin::new(LORA_RESET_PIN);
    reset.export().unwrap();
    reset.set_direction(Direction::Out).unwrap();

    let mut lora = sx127x_lora::LoRa::new(
        spi, cs, reset,  FREQUENCY, Delay)
        .expect("Failed to communicate with radio module!");

    lora.set_tx_power(17,1); //Using PA_BOOST. See your board for correct pin.

    let message = "Hello, world!";
    let mut buffer = [0;255];
    for (i,c) in message.chars().enumerate() {
        buffer[i] = c as u8;
    }

    let transmit = lora.transmit_payload(buffer,message.len());
    match transmit {
        Ok(packet_size) => println!("Sent packet with size: {}", packet_size),
        Err(()) => println!("Error"),
    }
}

STM32F429 Blocking Receive

Utilizes a STM32F429 to receive data using the blocking poll_irq(timeout) function. It prints the received packet back out over semihosting. The example utilizes the stm32f429_hal, cortex_m, and panic_semihosting crates.

#![no_std]
#![no_main]

extern crate sx127x_lora;
extern crate stm32f429_hal as hal;
extern crate cortex_m;
extern crate panic_semihosting;

use sx127x_lora::MODE;
use cortex_m_semihosting::*;
use hal::gpio::GpioExt;
use hal::flash::FlashExt;
use hal::rcc::RccExt;
use hal::time::MegaHertz;
use hal::spi::Spi;
use hal::delay::Delay;

const FREQUENCY: i64 = 915;

#[entry]
fn main() -> !{
    let cp = cortex_m::Peripherals::take().unwrap();
    let p = hal::stm32f429::Peripherals::take().unwrap();

    let mut rcc = p.RCC.constrain();
    let mut flash = p.FLASH.constrain();
    let clocks = rcc
        .cfgr
        .sysclk(MegaHertz(64))
        .pclk1(MegaHertz(32))
        .freeze(&mut flash.acr);

    let mut gpioa = p.GPIOA.split(&mut rcc.ahb1);
    let mut gpiod = p.GPIOD.split(&mut rcc.ahb1);
    let mut gpiof = p.GPIOF.split(&mut rcc.ahb1);

    let sck = gpioa.pa5.into_af5(&mut gpioa.moder, &mut gpioa.afrl);
    let miso = gpioa.pa6.into_af5(&mut gpioa.moder, &mut gpioa.afrl);
    let mosi = gpioa.pa7.into_af5(&mut gpioa.moder, &mut gpioa.afrl);
    let reset = gpiof.pf13.into_push_pull_output(&mut gpiof.moder, &mut gpiof.otyper);
    let cs = gpiod.pd14.into_push_pull_output(&mut gpiod.moder, &mut gpiod.otyper);

    let spi = Spi::spi1(
        p.SPI1,
        (sck, miso, mosi),
        MODE,
        MegaHertz(8),
        clocks,
        &mut rcc.apb2,
    );

    let mut lora = sx127x_lora::LoRa::new(
        spi, cs, reset, FREQUENCY,
        Delay::new(cp.SYST, clocks)).unwrap();

    loop {
        let poll = lora.poll_irq(Some(30)); //30 Second timeout
        match poll {
            Ok(size) =>{
               hprint!("with Payload: ");
               let buffer = lora.read_packet(); // Received buffer. NOTE: 255 bytes are always returned
               for i in 0..size{
                   hprint!("{}",buffer[i] as char).unwrap();
               }
               hprintln!();
            },
            Err(()) => hprintln!("Timeout").unwrap(),
        }
    }
}

Interrupts

The crate currently polls the IRQ register on the radio to determine if a new packet has arrived. This would be more efficient if instead an interrupt was connect the the module's DIO_0 pin. Once interrupt support is available in embedded-hal, then this will be added. It is possible to implement this function on a device-to-device basis by retrieving a packet with the read_packet() function.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

sx127x_lora's People

Contributors

akloboucnik avatar janno avatar michaelbeaumont avatar mr-glt avatar mvniekerk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

sx127x_lora's Issues

Code on GitHub differs from published code on crates.io

Thanks so much for making this crate!

I just noticed that the latest version on crates.io has is_transmitting but that function name never occurred in the git history of this repository. transmitting exists and does the same thing, I guess.

This also makes me wonder: are there other differences between the version on crates.io and this repository?

For ESP32

Hello

I am looking to implement this library for a project I am doing with a ESP32.

The issue is the following when I try to instantiate this library in the SPI pin from the ESP32.

    let spi = peripherals.spi2;

    let mut lora = sx127x_lora::LoRa::new(spi, sc, rst, FREQUENCY, delay);

It simply gives me this error:

let spi: SPI2
Go to SPI2

the trait bound `esp_idf_hal::gpio::Gpio14<esp_idf_hal::gpio::Unknown>: embedded_hal::digital::v1::OutputPin` is not satisfied
required because of the requirements on the impl of `embedded_hal::digital::v2::OutputPin` for `esp_idf_hal::gpio::Gpio14<esp_idf_hal::gpio::Unknown>`rustcE0277
the trait bound `esp_idf_hal::spi::SPI2: embedded_hal::blocking::spi::transfer::Default<u8>` is not satisfied
required because of the requirements on the impl of `embedded_hal::blocking::spi::Transfer<u8>` for `esp_idf_hal::spi::SPI2`rustcE0277
main.rs(43, 20): required by a bound introduced by this call
lib.rs(190, 29): required by a bound in `sx127x_lora::LoRa::<SPI, CS, RESET, DELAY>::new`
the trait bound `esp_idf_hal::spi::SPI2: embedded_hal::blocking::spi::write::Default<u8>` is not satisfied
required because of the requirements on the impl of `embedded_hal::blocking::spi::Write<u8>` for `esp_idf_hal::spi::SPI2`rustcE0277
main.rs(43, 20): required by a bound introduced by this call
lib.rs(190, 52): required by a bound in `sx127x_lora::LoRa::<SPI, CS, RESET, DELAY>::new`

Is this anything you have experience with by any chance?

Sincerely

frequency

FREQUENCY in megahertz needs to be a float, not i64, otherwise only the nominal channel can be used, for example, channel 12 in the 915MHz band. Another option might be to leave it integer but in kHz or Hz. Here is a list of channels copied from some python code.

#https://www.rfwireless-world.com/Tutorials/LoRa-channels-list.html
channels = {
   'CH_00_900': 903.08, 'CH_01_900': 905.24, 'CH_02_900': 907.40,
   'CH_03_900': 909.56, 'CH_04_900': 911.72, 'CH_05_900': 913.88,
   'CH_06_900': 916.04, 'CH_07_900': 918.20, 'CH_08_900': 920.36,
   'CH_09_900': 922.52, 'CH_10_900': 924.68, 'CH_11_900': 926.84, 'CH_12_900': 915.00,

   'CH_10_868': 865.20, 'CH_11_868': 865.50, 'CH_12_868': 865.80,
   'CH_13_868': 866.10, 'CH_14_868': 866.40, 'CH_15_868': 866.70,
   'CH_16_868': 867   , 'CH_17_868': 868   ,   
   }

VersionMismatch due to what?

Hi, we're trying to use lora in conjunction with an esp32.

we're passing an spi2 to to the loramodule, and when we instantiate the object, it returns this error:

VersionMismatch(0)

Is this likely due to our spi, or hardwiring of our pins?

status?

I am managing to get a small example to build and run, that is not panic, on different MCUs, but am having no luck with communication to a receiver. I think I have all parameters set the same on sender and receiver, so I am wondering if I really should be expecting that this package is working. I have been playing with release, this git version, and the git version https://github.com/mvniekerk/sx127x_lora which seems more active recently.

Does anyone actual have send and receive examples working in a way that they communicate?

The fork at https://github.com/mvniekerk/sx127x_lora does not have issues enabled. I think that is usually an indication (hope) that a pull request will eventually merge that fork back here, so issues should be tracked here. Is that a fair assumption?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.