-
Notifications
You must be signed in to change notification settings - Fork 42
Add Matter water valve support and example sketch #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ludovic BOUÉ (lboue)
wants to merge
3
commits into
SiliconLabsSoftware:main
Choose a base branch
from
lboue:Matter-WaterValve
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
libraries/Matter/examples/matter_water_valve/matter_water_valve.ino
This file contains hidden or 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,89 @@ | ||
| /* | ||
| Matter water valve example | ||
|
|
||
| The example shows how to create a water valve device with the Arduino Matter API. | ||
|
|
||
| The example creates a Matter water valve device that can be opened (optionally for a set | ||
| duration) or closed from a Matter hub, and toggles the builtin LED based on its current state. | ||
| The device has to be commissioned to a Matter hub first. | ||
|
|
||
| Compatible boards: | ||
| - Arduino Nano Matter | ||
| - SparkFun Thing Plus MGM240P | ||
| - xG24 Explorer Kit | ||
| - xG24 Dev Kit | ||
| - Seeed Studio XIAO MG24 (Sense) | ||
|
|
||
| Author: Ludovic BOUÉ | ||
| */ | ||
| #include <Matter.h> | ||
| #include <MatterWaterValve.h> | ||
|
|
||
| MatterWaterValve matter_water_valve; | ||
|
|
||
| void setup() | ||
| { | ||
| pinMode(LED_BUILTIN, OUTPUT); | ||
| digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE); | ||
| Serial.begin(115200); | ||
| Matter.begin(); | ||
| matter_water_valve.begin(); | ||
|
|
||
| Serial.println("Matter water valve"); | ||
|
|
||
| if (!Matter.isDeviceCommissioned()) { | ||
| Serial.println("Matter device is not commissioned"); | ||
| Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); | ||
| Serial.printf("Manual pairing code: %s\n", Matter.getManualPairingCode().c_str()); | ||
| Serial.printf("QR code URL: %s\n", Matter.getOnboardingQRCodeUrl().c_str()); | ||
| } | ||
| while (!Matter.isDeviceCommissioned()) { | ||
| delay(200); | ||
| } | ||
|
|
||
| Serial.println("Waiting for Thread network..."); | ||
| while (!Matter.isDeviceThreadConnected()) { | ||
| delay(200); | ||
| } | ||
| Serial.println("Connected to Thread network"); | ||
|
|
||
| Serial.println("Waiting for Matter device discovery..."); | ||
| while (!matter_water_valve.is_online()) { | ||
| delay(200); | ||
| } | ||
| Serial.println("Matter device is now online"); | ||
| } | ||
|
|
||
| void loop() | ||
| { | ||
| // Run down the countdown of a timed open operation, log the remaining time, and close the valve once it elapses | ||
| static unsigned long last_tick_ms = 0; | ||
| if ((matter_water_valve.get_remaining_duration() > 0) && (millis() - last_tick_ms >= 1000)) { | ||
| last_tick_ms = millis(); | ||
| uint32_t remaining_duration = matter_water_valve.get_remaining_duration() - 1; | ||
| matter_water_valve.set_remaining_duration(remaining_duration); | ||
| Serial.printf("Water valve remaining duration: %lu s\n", (unsigned long)remaining_duration); | ||
| } | ||
|
|
||
| // Log the default open duration whenever it's changed remotely, e.g. from a Matter hub | ||
| static uint32_t default_open_duration_prev = matter_water_valve.get_default_open_duration(); | ||
| uint32_t default_open_duration = matter_water_valve.get_default_open_duration(); | ||
| if (default_open_duration_prev != default_open_duration) { | ||
| Serial.printf("Water valve default open duration changed: %lu s\n", (unsigned long)default_open_duration); | ||
| default_open_duration_prev = default_open_duration; | ||
| } | ||
|
|
||
| // Toggle the LED on/off based on the current valve state | ||
| static MatterWaterValve::valve_state_t state_prev = MatterWaterValve::valve_state_t::VALVE_STATE_CLOSED; | ||
| MatterWaterValve::valve_state_t state = matter_water_valve.get_current_state(); | ||
| if (state_prev != state) { | ||
| if (state == MatterWaterValve::valve_state_t::VALVE_STATE_OPEN) { | ||
| Serial.println("Water valve state: OPEN"); | ||
| digitalWrite(LED_BUILTIN, LED_BUILTIN_ACTIVE); | ||
| } else { | ||
| Serial.println("Water valve state: CLOSED"); | ||
| digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE); | ||
| } | ||
| state_prev = state; | ||
| } | ||
|
silabs-bozont marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.