diff options
Diffstat (limited to 'examples/temperature.rs')
| -rw-r--r-- | examples/temperature.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/examples/temperature.rs b/examples/temperature.rs new file mode 100644 index 0000000..32f4887 --- /dev/null +++ b/examples/temperature.rs | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | mod common; | ||
| 2 | |||
| 3 | use common::AsyncTcp; | ||
| 4 | use embassy_executor::{Executor, Spawner}; | ||
| 5 | use embassy_time::Timer; | ||
| 6 | use static_cell::StaticCell; | ||
| 7 | |||
| 8 | static RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new(); | ||
| 9 | |||
| 10 | #[embassy_executor::task] | ||
| 11 | async fn main_task(spawner: Spawner) { | ||
| 12 | let mut stream = AsyncTcp::connect(std::env!("MQTT_ADDRESS")); | ||
| 13 | |||
| 14 | let mut device = embassy_ha::Device::new( | ||
| 15 | RESOURCES.init(Default::default()), | ||
| 16 | embassy_ha::DeviceConfig { | ||
| 17 | device_id: "example-device-id", | ||
| 18 | device_name: "Example Device Name", | ||
| 19 | manufacturer: "Example Device Manufacturer", | ||
| 20 | model: "Example Device Model", | ||
| 21 | }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | let constant_temperature_sensor = device.create_temperature_sensor( | ||
| 25 | "constant-temperature-sensor-id", | ||
| 26 | "Constant Temperature Sensor", | ||
| 27 | embassy_ha::TemperatureUnit::Celcius, | ||
| 28 | ); | ||
| 29 | |||
| 30 | let random_temperature_sensor = device.create_temperature_sensor( | ||
| 31 | "random-temperature-sensor-id", | ||
| 32 | "Random Temperature Sensor", | ||
| 33 | embassy_ha::TemperatureUnit::Celcius, | ||
| 34 | ); | ||
| 35 | |||
| 36 | spawner.must_spawn(constant_temperature_task(constant_temperature_sensor)); | ||
| 37 | spawner.must_spawn(random_temperature_task(random_temperature_sensor)); | ||
| 38 | |||
| 39 | device.run(&mut stream).await; | ||
| 40 | } | ||
| 41 | |||
| 42 | #[embassy_executor::task] | ||
| 43 | async fn constant_temperature_task(mut sensor: embassy_ha::TemperatureSensor<'static>) { | ||
| 44 | loop { | ||
| 45 | sensor.publish(42.0); | ||
| 46 | Timer::after_secs(1).await; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | #[embassy_executor::task] | ||
| 51 | async fn random_temperature_task(mut sensor: embassy_ha::TemperatureSensor<'static>) { | ||
| 52 | loop { | ||
| 53 | sensor.publish(rand::random_range(0.0..50.0)); | ||
| 54 | Timer::after_secs(1).await; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | example_main!(); | ||
