1
0
Fork 0

Initial commit based on rumqttc example

gh-action
Bo Jeanes 2022-07-15 17:11:38 +10:00
commit c63072715d
4 changed files with 1173 additions and 0 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
/target

1097
Cargo.lock generated 100644

File diff suppressed because it is too large Load Diff

12
Cargo.toml 100644
View File

@ -0,0 +1,12 @@
[package]
name = "modbus-mqtt"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "3.2.12", features = ["derive", "env"] }
rumqttc = { version = "0.13.0", features = ["url"], git = "https://github.com/bytebeamio/rumqtt" }
tokio = { version = "1.20.0", features = ["rt", "rt-multi-thread"] }
tokio-modbus = "0.5.3"

63
src/main.rs 100644
View File

@ -0,0 +1,63 @@
use rumqttc::{self, AsyncClient, MqttOptions, QoS};
use std::error::Error;
use std::time::Duration;
use tokio::{task, time};
use clap::Parser;
#[derive(Parser)]
struct Cli {
mqtt_host: String,
#[clap(short = 'n', long, default_value = "modbus")]
mqtt_name: String,
#[clap(short = 'p', long, default_value_t = 1883)]
mqtt_port: u16,
#[clap(short = 'u', long, env = "MQTT_USER")]
mqtt_user: Option<String>,
#[clap(short = 'P', long, env)]
mqtt_password: Option<String>,
}
#[tokio::main(worker_threads = 1)]
async fn main() -> Result<(), Box<dyn Error>> {
let args = Cli::parse();
let mut mqttoptions = MqttOptions::new("mqtt", args.mqtt_host.as_str(), args.mqtt_port);
if let (Some(u), Some(p)) = (args.mqtt_user, args.mqtt_password) {
mqttoptions.set_credentials(u, p);
}
mqttoptions.set_keep_alive(Duration::from_secs(5));
let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
task::spawn(async move {
requests(client).await;
time::sleep(Duration::from_secs(3)).await;
});
loop {
let event = eventloop.poll().await;
println!("{:?}", event.unwrap());
}
}
async fn requests(client: AsyncClient) {
client
.subscribe("hello/world", QoS::AtMostOnce)
.await
.unwrap();
for i in 1..=10 {
client
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; i])
.await
.unwrap();
time::sleep(Duration::from_secs(1)).await;
}
time::sleep(Duration::from_secs(120)).await;
}