-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
HOST=mqtt.example.com:1883 | ||
USERNAME=example | ||
PASSWORD=root | ||
TOPIC=robot/stdin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "robot-controller" | ||
version = "1.0.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
paho-mqtt = { version = "0.12.1", features=["vendored-ssl"] } | ||
dotenv = "0.15.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
use paho_mqtt as mqtt; | ||
use dotenv::dotenv; | ||
|
||
use std::{env, process, io}; | ||
use std::io::prelude::*; | ||
|
||
|
||
fn main() { | ||
|
||
dotenv().ok(); | ||
|
||
let host = env::var("HOST").unwrap_or_else(|_| { | ||
println!("Host not set"); | ||
process::exit(1); | ||
}); | ||
|
||
let username = env::var("USERNAME").unwrap_or_else(|_| String::from("")); | ||
let password = env::var("PASSWORD").unwrap_or_else(|_| String::from("")); | ||
|
||
let topic = env::var("TOPIC").unwrap_or_else(|_| { | ||
println!("Topic not set"); | ||
process::exit(1); | ||
}); | ||
|
||
let cli = mqtt::AsyncClient::new(host).unwrap_or_else(|err| { | ||
println!("Error creating the client: {}", err); | ||
process::exit(1); | ||
}); | ||
|
||
let mut conn_builder = mqtt::ConnectOptionsBuilder::new_ws(); | ||
|
||
if !username.is_empty() { | ||
conn_builder.user_name(username); | ||
} | ||
|
||
if !password.is_empty() { | ||
conn_builder.password(password); | ||
} | ||
|
||
let conn_opts = conn_builder.finalize(); | ||
|
||
if let Err(err) = cli.connect(conn_opts).wait() { | ||
println!("Unable to connect: {}", err); | ||
process::exit(1); | ||
} | ||
|
||
let stdin = io::stdin(); | ||
for line in stdin.lock().lines() { | ||
let msg = mqtt::Message::new(&topic, line.unwrap(), 0); | ||
let tok = cli.publish(msg); | ||
if let Err(e) = tok.wait() { | ||
println!("Error sending message: {:?}", e); | ||
} | ||
} | ||
} |