Crate rpi_sim868

source ·
Expand description

§RPi SIM868

RPi SIM868 is a Rust crate designed to simplify interaction with the Waveshare SIM868 HAT for Raspberry Pi. It utilizes the tokio runtime for managing asynchronous tasks and includes its own task scheduler based on a priority queue. Each method call initiates a new task, which is enqueued with a priority to ensure swift execution as soon as the serial port becomes available.

Methods (except for hat::Hat::turn_on) return TaskJoinHandle<T>, where T represents the type resulting from parsing and analyzing the serial output, if applicable. Tasks related to phone calls are treated as first-class citizens with high priority, reducing delays in answering or concluding calls.

RPi SIM868 was conceived following a high-altitude balloon launch where the HAT served as a backup tracking device. The initial software, written in Python, lacked the performance and safety synonymous with Rust.

Tested SIM868 UART selection switch: A - ttyUSBx port, and B - ttySx port.

Tested devices: RPi 3 Model B, RPi 4 Model B, RPi Zero W, RPi Zero 2 W.

§Example usage

use rpi_sim868::{SIM868, TaskJoinHandle};
use tokio::time::sleep;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let sim: SIM868 = SIM868::new("/dev/ttyS0", 115200, rpi_sim868::LogLevelFilter::Error);

    sim.hat.turn_on().await?;

    // waiting for the GSM network connection...
    while let Ok(strength) = sim.hat.network_strength().await? {
        if strength > 0 {
            break;
        }
        sleep(Duration::from_secs(2)).await;
    }

    // task is spawned by tokio::spawn and starts in the background
    let send_sms: TaskJoinHandle<()> = sim.sms.send("+4799999999", "Hello!");

    /*
        Some other operations...
    */

    // the .await? returns the task Result or errors with tokio::task::JoinError
    match send_sms.await? {
        Ok(_) => println!("the SMS has been sent."),
        Err(e) => println!("Problem with sending the SMS: {e:?}"),
    }

    sim.hat.turn_off().await??;

    Ok(())
}

Modules§

Structs§

Enums§

Type Aliases§