Crates.io | mijia |
lib.rs | mijia |
version | 0.7.1 |
source | src |
created_at | 2020-10-30 22:21:40.711888 |
updated_at | 2023-09-02 12:40:23.781461 |
description | A library for connecting to Xiaomi Mijia 2 Bluetooth temperature/humidity sensors. |
homepage | |
repository | https://github.com/alsuren/mijia-homie/ |
max_upload_size | |
id | 307111 |
size | 77,152 |
A library for connecting to Xiaomi Mijia 2 Bluetooth temperature/humidity sensors.
Currently only supports running on Linux, as it depends on BlueZ for Bluetooth.
// Create a new session. This establishes the D-Bus connection. In this case we ignore the join
// handle, as we don't intend to run indefinitely.
let (_, session) = MijiaSession::new().await?;
// Start scanning for Bluetooth devices, and wait a few seconds for some to be discovered.
session.bt_session.start_discovery().await?;
time::sleep(Duration::from_secs(5)).await;
// Get the list of sensors which are currently known.
let sensors = session.get_sensors().await?;
for sensor in sensors {
// Connect to the sensor
session.bt_session.connect(&sensor.id).await?;
// Print some properties of the sensor.
let sensor_time: DateTime<Utc> = session.get_time(&sensor.id).await?.into();
let temperature_unit = session.get_temperature_unit(&sensor.id).await?;
let comfort_level = session.get_comfort_level(&sensor.id).await?;
println!(
"Time: {}, Unit: {}, Comfort level: {}",
sensor_time, temperature_unit, comfort_level
);
// Subscribe to readings from the sensor.
session.start_notify_sensor(&sensor.id).await?;
}
// Print readings from all the sensors we subscribed to.
let mut events = session.event_stream().await?;
while let Some(event) = events.next().await {
if let MijiaEvent::Readings { id, readings } = event {
println!("{}: {}", id, readings);
}
}
For some more complete examples, see the examples directory.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.