GithubHelp home page GithubHelp logo

meshtastic / rust Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 11.0 179 KB

A Rust library for connecting to and configuring Meshtastic radios.

Home Page: https://meshtastic.org/

License: GNU General Public License v3.0

Rust 100.00%
library meshtastic rust-lang

rust's Introduction

Meshtastic.rs

Overview

Meshtastic.rs is a crate that allows you to interact with Meshtastic devices in Rust. This crate is designed to be used on a desktop environment, and currently supports connecting to radios via USB serial and TCP.

This crate is designed to be used within the tokio asynchronous runtime.

Crates.io Documentation License

Installation

You can add this crate to your project using the following command:

cargo add meshtastic

Usage

This crate provides basic TCP and serial connection examples within the /examples directory. You can run these examples using the following commands:

cargo run --example basic_tcp
cargo run --example basic_serial

TCP Example

This example requires a Meshtastic with an exposed IP port, or a simulated radio via the Meshtastic Docker instance (see here).

/// This example connects to a TCP port on the radio, and prints out all received packets.
/// This can be used with a simulated radio via the Meshtastic Docker firmware image.
/// https://meshtastic.org/docs/software/linux-native#usage-with-docker

use std::io::{self, BufRead};

use meshtastic::api::StreamApi;
use meshtastic::utils;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let stream_api = StreamApi::new();

    println!("Enter the address of a TCP port to connect to, in the form \"IP:PORT\":");

    let stdin = io::stdin();
    let entered_address = stdin
        .lock()
        .lines()
        .next()
        .expect("Failed to find next line")
        .expect("Could not read next line");

    let tcp_stream = utils::stream::build_tcp_stream(entered_address).await?;
    let (mut decoded_listener, stream_api) = stream_api.connect(tcp_stream).await;

    let config_id = utils::generate_rand_id();
    let stream_api = stream_api.configure(config_id).await?;

    // This loop can be broken with ctrl+c, or by unpowering the radio.
    while let Some(decoded) = decoded_listener.recv().await {
        println!("Received: {:?}", decoded);
    }

    // Note that in this specific example, this will only be called when
    // the radio is disconnected, as the above loop will never exit.
    // Typically you would allow the user to manually kill the loop,
    // for example with tokio::select!.
    let _stream_api = stream_api.disconnect().await?;

    Ok(())
}

Serial Example

This example requires a powered and flashed Meshtastic radio connected to the host machine via a USB serial port.

/// This example connects to a radio via serial, and prints out all received packets.
/// This example requires a powered and flashed Meshtastic radio.
/// https://meshtastic.org/docs/supported-hardware

use std::io::{self, BufRead};

use meshtastic::api::StreamApi;
use meshtastic::utils;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let stream_api = StreamApi::new();

    let available_ports = utils::stream::available_serial_ports()?;
    println!("Available ports: {:?}", available_ports);
    println!("Enter the name of a port to connect to:");

    let stdin = io::stdin();
    let entered_port = stdin
        .lock()
        .lines()
        .next()
        .expect("Failed to find next line")
        .expect("Could not read next line");

    let serial_stream = utils::stream::build_serial_stream(entered_port, None, None, None)?;
    let (mut decoded_listener, stream_api) = stream_api.connect(serial_stream).await;

    let config_id = utils::generate_rand_id();
    let stream_api = stream_api.configure(config_id).await?;

    // This loop can be broken with ctrl+c, or by disconnecting
    // the attached serial port.
    while let Some(decoded) = decoded_listener.recv().await {
        println!("Received: {:?}", decoded);
    }

    // Note that in this specific example, this will only be called when
    // the radio is disconnected, as the above loop will never exit.
    // Typically you would allow the user to manually kill the loop,
    // for example with tokio::select!.
    let _stream_api = stream_api.disconnect().await?;

    Ok(())
}

Stats

Alt

Contributing

Contributions are welcome! If you find a bug or want to propose a new feature, please open an issue or submit a pull request.

License

This project is licensed under the GPL-3.0 License.

rust's People

Contributors

ajmcquilkin avatar lukipuki avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

rust's Issues

Using protoc-bin-vendored doesn't work when building on NixOS

NixOS is a Linux distribution which does not obey the FHS, and as such, the protoc supplied by protoc-bin-vendored-linux will fail to run:

error: failed to run custom build command for `meshtastic v0.1.6`
note: To improve backtraces for build dependencies, set the CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.

Caused by:
  process didn't exit successfully: `/home/ablyve/my/83e_meshtastic-bot/target/debug/build/meshtastic-bf53c0c18fbe98d1/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=src/protobufs/

  --- stderr
  thread 'main' panicked at /home/ablyve/.cargo/registry/src/index.crates.io-6f17d22bba15001f/meshtastic-0.1.6/build.rs:49:54:
  called `Result::unwrap()` on an `Err` value: Custom { kind: NotFound, error: "failed to invoke protoc (hint: https://docs.rs/prost-build/#sourcing-protoc): (path: \"/home/ablyve/.cargo/registry/src/index.crates.io-6f17d22bba15001f/protoc-bin-vendored-linux-x86_64-3.0.0/bin/protoc\"): No such file or directory (os error 2)" }

If we run the binary manually:

/home/ablyve/.cargo/registry/src/index.crates.io-6f17d22bba15001f/protoc-bin-vendored-linux-x86_64-3.0.0/bin/protoc
bash: /home/ablyve/.cargo/registry/src/index.crates.io-6f17d22bba15001f/protoc-bin-vendored-linux-x86_64-3.0.0/bin/protoc: cannot execute: required file not found

As a workaround, doing this patchelf manually on NixOS lets the build finish:

patchelf --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) /home/ablyve/.cargo/registry/src/index.crates.io-6f17d22bba15001f/protoc-bin-vendored-linux-x86_64-3.0.0/bin/protoc

Pulling USB out is not detected and eats 100% CPU

Steps to reproduce

  • Run basic_serial example and enter port name of plugged-in device
  • Wait for console to calm down and pull the USB out
  • Observe example loop is still running, console is clean, 1 CPU core is 100% utilized

Possible reason

This line is getting executed endlessly:

Ok(0) => continue,
If I return an error here the example starts to work, but I'm not sure it will not break something else.

Send message

Thanks for this nice library!

I'm having some trouble understanding how to declare the packet_router in the snippet explaining how to use the send_text function:

let stream_api = StreamApi::new();
let tcp_stream = build_tcp_stream("localhost:4403".to_string()).await?;
let (_decoded_listener, stream_api) = stream_api.connect(tcp_stream).await;

let config_id = generate_rand_id();
let mut stream_api = stream_api.configure(config_id).await?;

stream_api.send_text(packet_router, "Hello world!".to_string(), PacketDestination::Broadcast, true, 0).await?;

Any hints? Thanks!

please link against newer libssl

Binary builds of this project link against libssl1.1 which is gone since Debian oldstable.

Please update to link against libssl3

error: protoc failed: Missing input file.

When I execute this command

cargo run --example basic_tcp

I got this message:

--- start message ---

me@fir:~/rust/rust$ cargo run --example basic_tcp
Compiling meshtastic v0.1.6 (/home/me/rust/rust)
error: failed to run custom build command for meshtastic v0.1.6 (/home/me/rust/rust)

Caused by:
process didn't exit successfully: /home/me/rust/rust/target/debug/build/meshtastic-463c990848a5a623/build-script-build (exit status: 101)
--- stdout
cargo:rerun-if-changed=src/protobufs/

--- stderr
thread 'main' panicked at build.rs:55:54:
called Result::unwrap() on an Err value: Custom { kind: Other, error: "protoc failed: Missing input file.\n" }
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
me@fir:~/rust/rust$

--- end message ---

Linux Mint 21.2

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.