forked from lcsfelix/reading-xiaomi-temp
-
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.
Merge branch 'master' into bluez-generated
Cargo.lock changes may cause dbus to fail to build.
- Loading branch information
Showing
9 changed files
with
458 additions
and
925 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,34 @@ | ||
use homie::{Datatype, HomieDevice, Node, Property}; | ||
use rumqttc::MqttOptions; | ||
use std::error::Error; | ||
|
||
#[tokio::main(core_threads = 2)] | ||
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> { | ||
pretty_env_logger::init(); | ||
|
||
let mqttoptions = MqttOptions::new("homie_example", "test.mosquitto.org", 1883); | ||
|
||
let mut builder = HomieDevice::builder("homie/example", "Homie light example", mqttoptions); | ||
builder.set_update_callback(|node_id, property_id, value| async move { | ||
println!("{}/{} is now {}", node_id, property_id, value); | ||
Some(value) | ||
}); | ||
let (mut homie, homie_handle) = builder.spawn().await?; | ||
|
||
let node = Node::new( | ||
"light", | ||
"Light", | ||
"light", | ||
vec![ | ||
Property::new("power", "On", Datatype::Boolean, true, None, None), | ||
Property::new("colour", "Colour", Datatype::Color, true, None, Some("rgb")), | ||
], | ||
); | ||
homie.add_node(node).await?; | ||
|
||
homie.ready().await?; | ||
println!("Ready"); | ||
|
||
// This will only resolve (with an error) if we lose connection to the MQTT server. | ||
homie_handle.await | ||
} |
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,71 @@ | ||
use futures::FutureExt; | ||
use homie::{Datatype, HomieDevice, Node, Property}; | ||
use rand::random; | ||
use rumqttc::MqttOptions; | ||
use std::error::Error; | ||
use std::time::Duration; | ||
use tokio::task::{self, JoinHandle}; | ||
use tokio::{time, try_join}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> { | ||
pretty_env_logger::init(); | ||
|
||
let mqttoptions = MqttOptions::new("homie_example", "test.mosquitto.org", 1883); | ||
|
||
let (mut homie, homie_handle) = | ||
HomieDevice::builder("homie/example", "Homie sensor example", mqttoptions) | ||
.spawn() | ||
.await?; | ||
|
||
homie | ||
.add_node(Node::new( | ||
"sensor", | ||
"Sensor", | ||
"Environment sensor", | ||
vec![ | ||
Property::new( | ||
"temperature", | ||
"Temperature", | ||
Datatype::Float, | ||
false, | ||
Some("ºC"), | ||
None, | ||
), | ||
Property::new( | ||
"humidity", | ||
"Humidity", | ||
Datatype::Integer, | ||
false, | ||
Some("%"), | ||
None, | ||
), | ||
], | ||
)) | ||
.await?; | ||
|
||
let handle: JoinHandle<Result<(), Box<dyn Error + Send + Sync>>> = task::spawn(async move { | ||
homie.ready().await?; | ||
println!("Ready"); | ||
|
||
loop { | ||
let temperature: f32 = random::<f32>() * 40.0; | ||
let humidity: u8 = (random::<f32>() * 100.0) as u8; | ||
println!("Update: {}ºC {}%", temperature, humidity); | ||
homie | ||
.publish_value("sensor", "temperature", temperature) | ||
.await?; | ||
homie.publish_value("sensor", "humidity", humidity).await?; | ||
|
||
time::delay_for(Duration::from_secs(10)).await; | ||
} | ||
}); | ||
|
||
// Poll everything to completion, until the first one bombs out. | ||
let res: Result<_, Box<dyn Error + Send + Sync>> = try_join! { | ||
homie_handle, | ||
handle.map(|res| Ok(res??)), | ||
}; | ||
res?; | ||
Ok(()) | ||
} |
Oops, something went wrong.