Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
isolume authored Jun 25, 2023
1 parent ad7f6ff commit 95ff8c6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# Enviroment variable files
.env
8 changes: 8 additions & 0 deletions Cargo.toml
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"
56 changes: 56 additions & 0 deletions src/main.rs
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);
}
}
}

0 comments on commit 95ff8c6

Please sign in to comment.