aboutsummaryrefslogtreecommitdiff
path: root/examples/switch.rs
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-12-04 21:14:27 +0000
committerdiogo464 <[email protected]>2025-12-04 21:14:27 +0000
commitabdcb51d7e9d6ebfacdf17b32e5296464e0aa2d4 (patch)
tree69b91ea6271abb8c1080f7bc3abb1bff4a46c32c /examples/switch.rs
parent01eab62d1d6b1ce77e63d29efb7bf57da74da8a1 (diff)
added switch example
Diffstat (limited to 'examples/switch.rs')
-rw-r--r--examples/switch.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/switch.rs b/examples/switch.rs
new file mode 100644
index 0000000..a12b8d0
--- /dev/null
+++ b/examples/switch.rs
@@ -0,0 +1,42 @@
1mod common;
2
3use common::AsyncTcp;
4use embassy_executor::{Executor, Spawner};
5use embassy_time::Timer;
6use static_cell::StaticCell;
7
8static RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new();
9
10#[embassy_executor::task]
11async 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 switch = device.create_switch("switch-id", "Example Switch");
25
26 spawner.must_spawn(switch_task(switch));
27
28 device.run(&mut stream).await;
29}
30
31#[embassy_executor::task]
32async fn switch_task(mut switch: embassy_ha::Switch<'static>) {
33 loop {
34 let state = switch.wait().await;
35 switch.set(state);
36
37 println!("state = {}", state);
38 Timer::after_secs(1).await;
39 }
40}
41
42example_main!();