4 releases

0.2.2 Mar 31, 2024
0.2.1 Mar 31, 2024
0.2.0 May 28, 2023
0.1.0 May 20, 2023

#173 in Embedded development

Download history 1/week @ 2024-02-14 11/week @ 2024-02-21 10/week @ 2024-02-28 162/week @ 2024-03-27 58/week @ 2024-04-03 3/week @ 2024-04-10

223 downloads per month

GPL-3.0-only

10KB
76 lines

libsparkypi

simple library to control 433 Mhz target devices using a Raspberry Pi and a 433 Mhz transmitter module using the excellent rppal crate

An example with a command line interface can be found here:

https://github.com/elkasztano/sparkypi

Usage:

Add to Cargo.toml:

[dependencies]
rppal = "0.17.1"
libsparkypi = "0.2.2"

Example:

use libsparkypi::{Transmission, ProtocolProperties};
use libsparkypi::*;
use rppal::gpio::Gpio;
use std::error::Error;
use std::{thread, time}; 

fn main() -> Result<(), Box<dyn Error>> {
    
    // gpio setup

    let gpio = Gpio::new()?;

    let mut output_pin = gpio.get(17)?.into_output();

    
    // transmission with predefined protocol
    
    let mut my_transmission = Transmission::new();

    my_transmission.sequence.push_str("s000000010101000101011001");
    my_transmission.pulse_length = 170;
    my_transmission.repeats = 5;
    my_transmission.protocol = P1;

    println!("{:?}", &my_transmission);

    my_transmission.send_to(&mut output_pin);

    thread::sleep(time::Duration::from_millis(500));

    
    // transmission with custom protocol
    
    let my_protocol = ProtocolProperties {
        short: 1,
        long: 2,
        sync_bit: 1,
        sync_gap: 11,
    };

    let custom_transmission = Transmission {
        sequence: String::from("s10100110001100100001001100000000"),
        pulse_length: 570,
        repeats: 10,
        protocol: my_protocol,
    };

    println!("{:?}", &custom_transmission);

    custom_transmission.send_to(&mut output_pin);

    Ok(())
}

The above example will transmit the binary sequence '000000010101000101011001' (24 bit) with a leading sync bit and a pulse length of 170 microseconds 5 times using the predefined protocol 1 ('P1').

After a short pause of 500 milliseconds a 32 bit binary sequence will be transmitted 10 times using a custom protocol.

In the above example the data pin of the transmitter module is connected to GPIO pin 17 on the Raspberry Pi.

defining custom protocols and transmissions

example

let my_protocol = ProtocolProperties {
    short: 1, // short pulse is 1 * pulse_length ( = 570 microseconds in the transmission below)
    long: 2, // long pulse is 2 * pulse_length ( = 1140 microseconds)
    sync_bit: 1, // sync_bit is 1 * pulse_length ( = 570 microseconds)
    sync_gap: 11, // sync_gap (pause) is 11 * pulse_length ( = 6270 microseconds)
};

let my_transmission = Transmission {
    
    // Binary sequence. The 's' at the beginning stands for the required sync bit.
    sequence: String::from("s10100110001100100001001100000000"),
    
    // The pulse length is the smallest time unit of the transmission.
    // It will be multiplied by the factors defined in the ProtocolProperties struct above.
    pulse_length: 570,
    
    // Usually the sequence must be repeated several times. This number should be as low as possible.
    repeats: 10,
    
    // Setting the desired protocol. Can be predefined or custom.
    protocol: my_protocol,
};

Notes

  • There are many different protocols for 433 Mhz data transmission.
  • It is easy to implement custom protocols as shown above.
  • Currently, presets for 3 different protocols are implemented. However, I only have the opportunity to test two of them (predefined constants 'P1' and 'XEN' are tested, 'P2' is untested).
  • If you have difficulties finding the right protocol for your target device, you might want to use an RTL SDR dongle to decode the signal of it's counterpart (e.g. remote control, magnetic door sensor etc.). This also helps a lot in finding the right pulse length. Note that many of the available transmitter modules seem to be a bit laggy, so you might need to reduce the pulse length a little bit to compensate.
  • libsparkypi does not rely on wiringpi, which seems to be deprecated.

Dependencies

~370KB