Build a Matter-Enabled Lightbulb with the Arduino Nano Matter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE); pinMode(RELAY, OUTPUT); digitalWrite(RELAY, LOW); Serial.println("Matter lightbulb"); 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); decommission_handler(); } Serial.println("Connected to Thread network"); Serial.println("Waiting for Matter device discovery..."); while (!matter_bulb.is_online()) { delay(200); decommission_handler(); } Serial.println("Matter device is now online"); } void loop() { decommission_handler(); static bool matter_lightbulb_last_state = false; bool matter_lightbulb_current_state = matter_bulb.get_onoff(); // If the current state is ON and the previous was OFF - turn on the LED if (matter_lightbulb_current_state && !matter_lightbulb_last_state) { matter_lightbulb_last_state = matter_lightbulb_current_state; digitalWrite(LED_BUILTIN, LED_BUILTIN_ACTIVE); digitalWrite(RELAY, HIGH); Serial.println("Bulb ON"); } // If the current state is OFF and the previous was ON - turn off the LED if (!matter_lightbulb_current_state && matter_lightbulb_last_state) { matter_lightbulb_last_state = matter_lightbulb_current_state; digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE); digitalWrite(RELAY, LOW); Serial.println("Bulb OFF"); } } void decommission_handler() { // If the button is not pressed or the device is not commissioned - return if (digitalRead(BTN_BUILTIN) != LOW || !Matter.isDeviceCommissioned()) { return; } // Store the time when the button was first pressed uint32_t start_time = millis(); // While the button is being pressed while (digitalRead(BTN_BUILTIN) == LOW) { // Calculate the elapsed time uint32_t elapsed_time = millis() - start_time; // If the button has been pressed for less than 10 seconds, continue if (elapsed_time < 10000u) { yield(); continue; } // Blink the LED to indicate the start of the decommissioning process for (uint8_t i = 0u; i < 10u; i++) { digitalWrite(LED_BUILTIN, !(digitalRead(LED_BUILTIN))); delay(100); } Serial.println("Starting decommissioning process, device will reboot..."); Serial.println(); digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE); // This function will not return // The device will restart once decommissioning has finished Matter.decommission(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
int soilMoistureValue = 0; int percentage=0; void setup() { pinMode(3,OUTPUT); Serial.begin(9600); } void loop() { soilMoistureValue = analogRead(A0); Serial.println(percentage); percentage = map(soilMoistureValue, 490, 1023, 100, 0); if(percentage < 10) { Serial.println(" pump on"); digitalWrite(3,LOW); } if(percentage >80) { Serial.println("pump off"); digitalWrite(3,HIGH); } } |
1 2 3 4 5 6 7 8 9 10 |
int soilMoistureValue = 0; int percentage=0; void setup() { pinMode(3,OUTPUT); Serial.begin(9600); } void loop() { soilMoistureValue = analogRead(A0); Serial.println(percentage); percentage = map(soilMoistureValue, 490, 1023, 100, 0); |