|
| 1 | +--- |
| 2 | +title: 'Nano Matter Temperature Sensor' |
| 3 | +difficulty: beginner |
| 4 | +compatible-products: [nano-matter] |
| 5 | +description: 'Learn how to build a Matter temperature sensor.' |
| 6 | +tags: |
| 7 | + - IoT |
| 8 | + - Matter |
| 9 | + - BLE |
| 10 | + - Temperature |
| 11 | +author: 'Christopher Méndez' |
| 12 | +hardware: |
| 13 | + - hardware/03.nano/boards/nano-matter |
| 14 | +software: |
| 15 | + - ide-v1 |
| 16 | + - ide-v2 |
| 17 | + - web-editor |
| 18 | + - iot-cloud |
| 19 | +--- |
| 20 | + |
| 21 | +## Overview |
| 22 | + |
| 23 | +This tutorial will teach you how to create a Matter sensor to monitor your room or workplace temperature. |
| 24 | + |
| 25 | +![Thumbnail]() |
| 26 | + |
| 27 | +Thanks to the seamless compatibility of the Nano Matter with almost any Matter network we can easily integrate our sensor with Amazon Alexa, Google Asistant, Apple Home, Home Assistant and even custom assistants. |
| 28 | + |
| 29 | +## Hardware and Software Requirements |
| 30 | +### Hardware Requirements |
| 31 | + |
| 32 | +- [Nano Matter](https://store.arduino.cc/products/nano-matter) (x1) |
| 33 | +- DHT11 Temperature and Humidity sensor (x1) |
| 34 | +- I2C OLED Display SSD1306(x1) |
| 35 | +- Breadboard (x1) |
| 36 | +- Jumper wires |
| 37 | +- Google Nest Hub Max (Thread Border Router) (x1) |
| 38 | +- [USB-C® cable](https://store.arduino.cc/products/usb-cable2in1-type-c) (x1) |
| 39 | + |
| 40 | +### Software Requirements |
| 41 | + |
| 42 | +- [Arduino IDE 2.0+](https://www.arduino.cc/en/software) or [Arduino Cloud Editor](https://create.arduino.cc/editor) |
| 43 | +- [Google Home App](https://home.google.com/get-app/) |
| 44 | +- [U8g2](https://github.com/olikraus/u8g2) library to control the OLED display. You can install it from the Arduino IDE library manager. |
| 45 | +- [DHT](https://github.com/mcmchris/DHT-sensor-library/tree/patch-1) library. Download from this [branch](https://github.com/mcmchris/DHT-sensor-library/tree/patch-1) so it support the Nano Matter. |
| 46 | + |
| 47 | +### Board Core and Libraries |
| 48 | + |
| 49 | +The **Silicon Labs** core contains the libraries and examples you need to work with the board's components, such as its Matter, Bluetooth® Low Energy, and I/Os. To install the Nano Matter core, navigate to **File > Preferences** and in the **Additional boards manager URLs**, add the following: |
| 50 | + |
| 51 | +`https://siliconlabs.github.io/arduino/package_arduinosilabs_index.json` |
| 52 | + |
| 53 | +Now navigate to **Tools > Board > Boards Manager** or click the Boards Manager icon in the left tab of the IDE. In the Boards Manager tab, search for `Nano Matter` and install the latest `Silicon Labs` core version. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## Project Setup |
| 58 | + |
| 59 | +### Schematic Diagram |
| 60 | + |
| 61 | +Use the following conection diagram for the project: |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +### Programming |
| 66 | + |
| 67 | +In the Arduino IDE upper menu, navigate to **Tools > Protocol stack** and select **Matter**. |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +Copy and paste the following sketch: |
| 72 | + |
| 73 | +```arduino |
| 74 | +#include <Matter.h> |
| 75 | +#include <MatterTemperature.h> |
| 76 | +#include "DHT.h" |
| 77 | +#include <U8x8lib.h> |
| 78 | +#include <Wire.h> |
| 79 | +
|
| 80 | +U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/U8X8_PIN_NONE); // OLEDs without Reset of the Display |
| 81 | +
|
| 82 | +MatterTemperature matter_temp_sensor; |
| 83 | +
|
| 84 | +#define DHTPIN D4 // Digital pin connected to the DHT sensor |
| 85 | +
|
| 86 | +#define DHTTYPE DHT11 |
| 87 | +
|
| 88 | +DHT dht(DHTPIN, DHTTYPE); |
| 89 | +
|
| 90 | +float temp = 0; |
| 91 | +
|
| 92 | +void setup() { |
| 93 | + Serial.begin(115200); |
| 94 | + Matter.begin(); |
| 95 | + matter_temp_sensor.begin(); |
| 96 | + dht.begin(); |
| 97 | +
|
| 98 | + u8x8.begin(); |
| 99 | + u8x8.setFont(u8x8_font_profont29_2x3_f); // u8x8_font_chroma48medium8_r |
| 100 | +
|
| 101 | + pinMode(BTN_BUILTIN, INPUT_PULLUP); |
| 102 | + pinMode(LEDR, OUTPUT); |
| 103 | + digitalWrite(LEDR, HIGH); |
| 104 | +
|
| 105 | + Serial.println("Matter temperature sensor"); |
| 106 | +
|
| 107 | + if (!Matter.isDeviceCommissioned()) { |
| 108 | + Serial.println("Matter device is not commissioned"); |
| 109 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 110 | + Serial.printf("Manual pairing code: %s\n", Matter.getManualPairingCode().c_str()); |
| 111 | + Serial.printf("QR code URL: %s\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 112 | + } |
| 113 | + while (!Matter.isDeviceCommissioned()) { |
| 114 | + delay(200); |
| 115 | + decommission_handler(); |
| 116 | + } |
| 117 | +
|
| 118 | + Serial.println("Waiting for Thread network..."); |
| 119 | + while (!Matter.isDeviceThreadConnected()) { |
| 120 | + delay(200); |
| 121 | + decommission_handler(); |
| 122 | + } |
| 123 | + Serial.println("Connected to Thread network"); |
| 124 | +
|
| 125 | + Serial.println("Waiting for Matter device discovery..."); |
| 126 | + while (!matter_temp_sensor.is_online()) { |
| 127 | + delay(200); |
| 128 | + decommission_handler(); |
| 129 | + } |
| 130 | + Serial.println("Matter device is now online"); |
| 131 | +} |
| 132 | +
|
| 133 | +void loop() { |
| 134 | + temp = dht.readTemperature(); |
| 135 | +
|
| 136 | + if (!isnan(temp)) { |
| 137 | + matter_temp_sensor.set_measured_value_celsius(temp); |
| 138 | + Serial.printf("Current CPU temperature: %.02f C\n", temp); |
| 139 | +
|
| 140 | + upDateValue(temp); |
| 141 | + } |
| 142 | +
|
| 143 | + delay(2000); |
| 144 | + decommission_handler(); |
| 145 | +} |
| 146 | +
|
| 147 | +void upDateValue(float t) { |
| 148 | + u8x8.setCursor(0, 0); |
| 149 | + u8x8.print(t); |
| 150 | + u8x8.print(" C"); |
| 151 | +} |
| 152 | +
|
| 153 | +void decommission_handler() { |
| 154 | + if (digitalRead(BTN_BUILTIN) == LOW) { //Push button pressed |
| 155 | + // measures time pressed |
| 156 | + int startTime = millis(); |
| 157 | + while (digitalRead(BTN_BUILTIN) == LOW) { |
| 158 | +
|
| 159 | + int elapsedTime = (millis() - startTime) / 1000.0; |
| 160 | +
|
| 161 | + if (elapsedTime > 10) { |
| 162 | + Serial.printf("Decommissioning!\n"); |
| 163 | + for (int i = 0; i < 10; i++) { |
| 164 | + digitalWrite(LEDR, !(digitalRead(LEDR))); |
| 165 | + delay(100); |
| 166 | + }; |
| 167 | +
|
| 168 | + if (!Matter.isDeviceCommissioned()) { |
| 169 | + Serial.println("Decommission done!"); |
| 170 | + digitalWrite(LEDR, LOW); |
| 171 | + Matter.decommission(); |
| 172 | + } else { |
| 173 | + Serial.println("Matter device is commissioned-> Starting Decommission process"); |
| 174 | + digitalWrite(LED_BUILTIN, LOW); |
| 175 | + Serial.println("Decommission done!"); |
| 176 | + Matter.decommission(); |
| 177 | + } |
| 178 | + break; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +Once you uploaded the example code to the Nano Matter, open the Serial Monitor and reset the board. |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +There you will find the URL that generates the QR for the Matter device commissioning. |
| 190 | + |
| 191 | +### Commissioning |
| 192 | + |
| 193 | +Copy and paste the QR code URL on your favorite web browser and a unique QR code will be generated for your board. |
| 194 | + |
| 195 | +Go to your **Google Home** app, navigate to **devices** and tap on **Add**, select the **Matter-enabled device** option and scan the QR code. |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | +## Final Results |
| 202 | + |
| 203 | +Finally, you will be able to monitor your room temperature from your smartphone, hub or asking to your personal assistant. |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | +You can also see the temperature value on the device OLED display. |
| 208 | + |
| 209 | +## Conclusion |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +### Next Steps |
| 214 | + |
0 commit comments