mr-glt / sx127x_lora

A platform-agnostic driver for Semtech SX1276/77/78/79 based boards.
Apache License 2.0
41 stars 38 forks source link

For ESP32 #13

Open DavidCarl opened 2 years ago

DavidCarl commented 2 years ago

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

bernii commented 2 years ago

I just wanted to confirm that this lib is working with TTGO ESP32 LORA v2.0 (esp32 based).

Example quick &dirty code

    let config = <spi::config::Config as Default>::default().baudrate(200.kHz().into());
    let sclk = pins.gpio5;
    let miso = pins.gpio27;
    let mosi = pins.gpio19;
    let cs = pins.gpio18;
    let rst  = pins.gpio12;
    let spi = peripherals.spi2;

    let spi = spi::Master::<spi::SPI2, _, _, _, _>::new(
        spi,
        spi::Pins {
            sclk,
            sdo: miso,
            sdi: Some(mosi),
            cs: Option::<gpio::Gpio18<gpio::Unknown>>::None,
        },
        config,
    ).unwrap();

     let mut lora = sx127x_lora::LoRa::new(
        spi,
        cs.into_output().unwrap(),
        rst.into_input_output().unwrap(),
        868,
        Ets
    );

    match lora {
        Ok(_) => info!("lora succes"),
        Err(ref x) => error!("error {:?}", x),
    };

    let mut lora = lora.unwrap();

    let message = b"Hello, world!";
    let mut buff = [0u8; 255];
    buff[..message.len()].clone_from_slice(message);
    let transmit = lora.transmit_payload(
        buff,
        message.len()
    );

    match transmit {
        Ok(_) => info!("Sent packet"),
        Err(e) => error!("Error: {:?}", e),
    }

    loop {
        let poll = lora.poll_irq(Some(30)); //30 Second timeout
        match poll {
            Ok(size) =>{
               info!("with Payload: ");
               let buffer = lora.read_packet().unwrap(); // Received buffer. NOTE: 255 bytes are always returned
               for i in 0..size{
                    print!("{}", buffer[i] as char);
               }
               println!();
            },
            Err(err) => (), //info!("Timeout {:?}", err),
        }
        thread::sleep(Duration::from_millis(100));
    }
S3j5b0 commented 2 years ago

Hi, @bernii, were you able to send messages with the module using this code?

gmelodie commented 1 month ago

I added a full example that I made for the Lora32 board in #18