feat: add Zigbee-controlled smart light
This commit is contained in:
1 parent
9793bc62af
commit
1aa6ba3c5f
10 files changed
+237
-99
No files matched your search
+77
-16
@@ -9,8 +9,10 @@ use core::ops::Range;
|
||||
|
||||
use embassy_embedded_hal::adapter::BlockingAsync;
|
||||
use embassy_time::Duration;
|
||||
use embassy_time::Instant;
|
||||
use embassy_time::Timer;
|
||||
use esp_storage::FlashStorage;
|
||||
use zigbee::aps::apsde::ApsdeSapConfirmStatus;
|
||||
use zigbee::CurrentPowerMode;
|
||||
use zigbee::CurrentPowerSourceLevel;
|
||||
use zigbee::DeviceConfig;
|
||||
@@ -32,10 +34,15 @@ use zigbee_cluster_library::clusters::general::identify::IdentifyServer;
|
||||
use zigbee_cluster_library::clusters::general::on_off;
|
||||
use zigbee_cluster_library::clusters::general::on_off::OnOffServer;
|
||||
use zigbee_cluster_library::profile;
|
||||
use zigbee_cluster_library::reporting::AttributeReportBuilder;
|
||||
use zigbee_cluster_library::reporting::AttributeReporting;
|
||||
use zigbee_cluster_library::reporting::ReportingTable;
|
||||
use zigbee_cluster_library::sender::ZclReportTarget;
|
||||
use zigbee_cluster_library::sender::ZclSender;
|
||||
use zigbee_cluster_library::server::UnsupportedClusterResponder;
|
||||
use zigbee_mac::esp::EspMlme;
|
||||
use zigbee_types::IeeeAddress;
|
||||
use zigbee_types::ShortAddress;
|
||||
|
||||
use crate::light;
|
||||
|
||||
@@ -46,13 +53,11 @@ use crate::light;
|
||||
/// garbage collection.
|
||||
pub const FLASH_RANGE: Range<u32> = 0x3e_0000..0x3e_4000;
|
||||
|
||||
/// Extended PAN ID of the network to join.
|
||||
///
|
||||
/// Placeholder: replace with the Extended PAN ID of your own coordinator.
|
||||
const EXTENDED_PAN_ID: u64 = 0xabcd_ef01_2345_6789;
|
||||
/// Extended PAN ID of the Home Assistant Zigbee network.
|
||||
const EXTENDED_PAN_ID: u64 = 0xafca_95fc_e132_eeb6;
|
||||
|
||||
/// Channel to scan on; must match the coordinator.
|
||||
const CHANNEL: u8 = 11;
|
||||
const CHANNEL: u8 = 25;
|
||||
|
||||
/// Scan duration exponent (beacon order).
|
||||
const SCAN_DURATION: u8 = 5;
|
||||
@@ -60,10 +65,9 @@ const SCAN_DURATION: u8 = 5;
|
||||
/// The application endpoint this light is served on.
|
||||
const LIGHT_ENDPOINT: u8 = 1;
|
||||
|
||||
/// Mains powered and always listening: allocate address (bit 7) and
|
||||
/// rx-on-when-idle (bit 3), so the coordinator can push commands at any time
|
||||
/// rather than waiting for a poll.
|
||||
const CAPABILITY: u8 = 0x8e;
|
||||
/// Mains powered and always listening: allocate address (bit 7), power source
|
||||
/// (bit 2), and rx-on-when-idle (bit 3); bit 1 stays clear for an end device.
|
||||
const CAPABILITY: u8 = 0x8c;
|
||||
|
||||
static INPUT_CLUSTERS: [u16; 3] = [
|
||||
basic::CLUSTER_ID,
|
||||
@@ -164,12 +168,11 @@ pub fn config() -> StackConfig<'static> {
|
||||
/// parent link, and persistence.
|
||||
#[embassy_executor::task]
|
||||
pub async fn stack_task(stack: &'static Stack) {
|
||||
// returns only once no rejoin can get us back: the network is gone or our
|
||||
// keys are stale, so drop it and re-commission from scratch
|
||||
let outcome = stack.run(embassy_time::Delay).await;
|
||||
log::warn!("off the network ({outcome:?}), forgetting it and rebooting");
|
||||
stack.forget_network().await;
|
||||
esp_hal::system::software_reset();
|
||||
loop {
|
||||
let outcome = stack.run(embassy_time::Delay).await;
|
||||
log::warn!("off the network ({outcome:?}), retrying in 5 seconds");
|
||||
Timer::after(Duration::from_secs(5)).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Mirrors the On/Off attribute onto the strip, both ways.
|
||||
@@ -178,14 +181,21 @@ pub async fn stack_task(stack: &'static Stack) {
|
||||
/// any notification hook, so this samples it. The reverse direction keeps the
|
||||
/// attribute truthful after a local button press.
|
||||
#[embassy_executor::task]
|
||||
pub async fn on_off_task() {
|
||||
pub async fn on_off_task(stack: &'static Stack) {
|
||||
stack.wait_until_joined().await;
|
||||
|
||||
let mut last = ON_OFF.on_off();
|
||||
let mut pending_report = None;
|
||||
let mut sequence_number = 0u8;
|
||||
let started_at = Instant::now();
|
||||
loop {
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
|
||||
let remote = ON_OFF.on_off();
|
||||
if remote != last {
|
||||
last = remote;
|
||||
pending_report = None;
|
||||
log::info!("Zigbee On/Off state changed: {remote}");
|
||||
light::set_on(remote);
|
||||
continue;
|
||||
}
|
||||
@@ -194,6 +204,57 @@ pub async fn on_off_task() {
|
||||
if local != last {
|
||||
last = local;
|
||||
ON_OFF.set_on_off(local);
|
||||
pending_report = Some(local);
|
||||
}
|
||||
|
||||
let Some(value) = pending_report else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let cluster = on_off::CLUSTER.id();
|
||||
let attribute = on_off::ON_OFF.id();
|
||||
let now_secs = started_at.elapsed().as_secs().min(u64::from(u32::MAX)) as u32;
|
||||
let configured = REPORTING.configuration(cluster, attribute).is_some();
|
||||
let due = !configured
|
||||
|| REPORTING.should_report(cluster, attribute, now_secs, i64::from(value));
|
||||
if !due {
|
||||
continue;
|
||||
}
|
||||
|
||||
sequence_number = sequence_number.wrapping_add(1);
|
||||
let mut buffer = [0u8; 32];
|
||||
let report = AttributeReportBuilder::new(&mut buffer, sequence_number)
|
||||
.and_then(|frame| frame.push(&on_off::ON_OFF, value))
|
||||
.and_then(AttributeReportBuilder::finish)
|
||||
.expect("encode On/Off report");
|
||||
let result = stack
|
||||
.device()
|
||||
.send_attribute_report(
|
||||
ZclReportTarget {
|
||||
dst_short: ShortAddress::COORDINATOR.0,
|
||||
src_endpoint: LIGHT_ENDPOINT,
|
||||
dst_endpoint: 1,
|
||||
profile_id: profile::HOME_AUTOMATION,
|
||||
},
|
||||
report,
|
||||
&mut embassy_time::Delay,
|
||||
)
|
||||
.await;
|
||||
|
||||
match result {
|
||||
Ok(confirm) if confirm.status == ApsdeSapConfirmStatus::Success => {
|
||||
REPORTING.reported(cluster, attribute, now_secs, i64::from(value));
|
||||
pending_report = None;
|
||||
log::info!("reported local Zigbee On/Off state: {value}");
|
||||
}
|
||||
Ok(confirm) => {
|
||||
log::warn!("On/Off report failed: {:?}", confirm.status);
|
||||
Timer::after(Duration::from_secs(1)).await;
|
||||
}
|
||||
Err(error) => {
|
||||
log::warn!("On/Off report encoding failed: {:?}", error);
|
||||
Timer::after(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user