|
| 1 | +/* |
| 2 | + Matter water valve example |
| 3 | +
|
| 4 | + The example shows how to create a water valve device with the Arduino Matter API. |
| 5 | +
|
| 6 | + The example creates a Matter water valve device that can be opened (optionally for a set |
| 7 | + duration) or closed from a Matter hub, and toggles the builtin LED based on its current state. |
| 8 | + The device has to be commissioned to a Matter hub first. |
| 9 | +
|
| 10 | + Compatible boards: |
| 11 | + - Arduino Nano Matter |
| 12 | + - SparkFun Thing Plus MGM240P |
| 13 | + - xG24 Explorer Kit |
| 14 | + - xG24 Dev Kit |
| 15 | + - Seeed Studio XIAO MG24 (Sense) |
| 16 | +
|
| 17 | + Author: Ludovic BOUE |
| 18 | + */ |
| 19 | +#include <Matter.h> |
| 20 | +#include <MatterWaterValve.h> |
| 21 | + |
| 22 | +MatterWaterValve matter_water_valve; |
| 23 | + |
| 24 | +void setup() |
| 25 | +{ |
| 26 | + pinMode(LED_BUILTIN, OUTPUT); |
| 27 | + digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE); |
| 28 | + Serial.begin(115200); |
| 29 | + Matter.begin(); |
| 30 | + matter_water_valve.begin(); |
| 31 | + |
| 32 | + Serial.println("Matter water valve"); |
| 33 | + |
| 34 | + if (!Matter.isDeviceCommissioned()) { |
| 35 | + Serial.println("Matter device is not commissioned"); |
| 36 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 37 | + Serial.printf("Manual pairing code: %s\n", Matter.getManualPairingCode().c_str()); |
| 38 | + Serial.printf("QR code URL: %s\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 39 | + } |
| 40 | + while (!Matter.isDeviceCommissioned()) { |
| 41 | + delay(200); |
| 42 | + } |
| 43 | + |
| 44 | + Serial.println("Waiting for Thread network..."); |
| 45 | + while (!Matter.isDeviceThreadConnected()) { |
| 46 | + delay(200); |
| 47 | + } |
| 48 | + Serial.println("Connected to Thread network"); |
| 49 | + |
| 50 | + Serial.println("Waiting for Matter device discovery..."); |
| 51 | + while (!matter_water_valve.is_online()) { |
| 52 | + delay(200); |
| 53 | + } |
| 54 | + Serial.println("Matter device is now online"); |
| 55 | +} |
| 56 | + |
| 57 | +void loop() |
| 58 | +{ |
| 59 | + // Run down the countdown of a timed open operation and close the valve once it elapses |
| 60 | + static unsigned long last_tick_ms = 0; |
| 61 | + if ((matter_water_valve.get_remaining_duration() > 0) && (millis() - last_tick_ms >= 1000)) { |
| 62 | + last_tick_ms = millis(); |
| 63 | + matter_water_valve.set_remaining_duration(matter_water_valve.get_remaining_duration() - 1); |
| 64 | + } |
| 65 | + |
| 66 | + // Toggle the LED on/off based on the current valve state |
| 67 | + static MatterWaterValve::valve_state_t state_prev = MatterWaterValve::valve_state_t::VALVE_STATE_CLOSED; |
| 68 | + MatterWaterValve::valve_state_t state = matter_water_valve.get_current_state(); |
| 69 | + if (state_prev != state) { |
| 70 | + if (state == MatterWaterValve::valve_state_t::VALVE_STATE_OPEN) { |
| 71 | + Serial.println("Water valve state: OPEN"); |
| 72 | + digitalWrite(LED_BUILTIN, LED_BUILTIN_ACTIVE); |
| 73 | + } else { |
| 74 | + Serial.println("Water valve state: CLOSED"); |
| 75 | + digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE); |
| 76 | + } |
| 77 | + state_prev = state; |
| 78 | + } |
| 79 | +} |
0 commit comments