Last active
November 8, 2024 13:39
-
-
Save thinkier/017df7cc71cde73e4a948f27980e275a to your computer and use it in GitHub Desktop.
An amalgamation of 3 examples for Arduino Nano Matter & SGP30. Reports Indoor Air Quality, Humidity & Temperature.
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
/* | |
This is an amalgamation of 3 examples made by the following people: | |
Author: | |
- Hai Nguyen (Silicon Labs) | |
- Tamas Jozsi (Silicon Labs) | |
- Ciara Jekel (Sparkfun Electronics) | |
*/ | |
#include <Wire.h> | |
#include <Matter.h> | |
#include <MatterAirQuality.h> | |
#include <MatterHumidity.h> | |
#include <MatterTemperature.h> | |
#include <SparkFun_SCD4x_Arduino_Library.h> | |
#include <SparkFun_SGP30_Arduino_Library.h> | |
SCD4x scd4x_sensor; | |
SGP30 sgp30_sensor; | |
MatterAirQuality matter_air_quality_sensor; | |
MatterHumidity matter_humidity_sensor; | |
MatterTemperature matter_temp_sensor; | |
void setup() | |
{ | |
Serial.begin(115200); | |
for(int i = 0; i < 5000; i++) { | |
if (Serial) break; | |
delay(1); | |
} | |
Matter.begin(); | |
matter_air_quality_sensor.begin(); | |
matter_humidity_sensor.begin(); | |
matter_temp_sensor.begin(); | |
Serial.println("Matter air quality, humidity & temperature sensor"); | |
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"); | |
// Initialize I2C | |
Wire.begin(); | |
// Initialize sensor | |
Serial.println("SCD4x sensor initializing..."); | |
if (!scd4x_sensor.begin()) { | |
Serial.println("SCD4x not detected"); | |
Serial.println("Check connections and press the reset button to reinitialize"); | |
while (1) ; // freezing | |
} | |
Serial.println("SCD4x sensor initialize done"); | |
Serial.println("SGP30 sensor initializing..."); | |
if (!sgp30_sensor.begin()) { | |
Serial.println("SGP30 not detected"); | |
Serial.println("Check connections and press the reset button to reinitialize"); | |
while (1) ; // freezing | |
} | |
sgp30_sensor.initAirQuality(); | |
Serial.println("SGP30 sensor initialize done"); | |
Serial.println("Waiting for Matter device discovery..."); | |
while (!matter_air_quality_sensor.is_online()) { | |
sgp30_sensor.measureAirQuality(); | |
delay(1000); | |
} | |
Serial.println("Matter device is now online"); | |
} | |
void loop() | |
{ | |
update(); | |
delay(1000); | |
} | |
void update() | |
{ | |
updateScd4x(); | |
updateSgp30(); | |
} | |
void updateScd4x() | |
{ | |
if (scd4x_sensor.readMeasurement()) // readMeasurement will return true when fresh data is available | |
{ | |
float humidity = scd4x_sensor.getHumidity(); | |
float temp = scd4x_sensor.getTemperature(); | |
matter_humidity_sensor = humidity; | |
matter_temp_sensor = temp; | |
} | |
} | |
void updateSgp30() | |
{ | |
sgp30_sensor.measureAirQuality(); | |
// Handle air quality value - DOI 10.1007/s00103-007-0290-y | |
if (sgp30_sensor.TVOC < 65) { | |
matter_air_quality_sensor.set_air_quality(MatterAirQuality::AirQuality_t::GOOD); | |
Serial.print("Current air quality: good @ "); | |
} else if (sgp30_sensor.TVOC < 220) { | |
matter_air_quality_sensor.set_air_quality(MatterAirQuality::AirQuality_t::FAIR); | |
Serial.print("Current air quality: fair @ "); | |
} else if (sgp30_sensor.TVOC < 660) { | |
matter_air_quality_sensor.set_air_quality(MatterAirQuality::AirQuality_t::MODERATE); | |
Serial.print("Current air quality: moderate @ "); | |
} else if (sgp30_sensor.TVOC < 2200) { | |
matter_air_quality_sensor.set_air_quality(MatterAirQuality::AirQuality_t::POOR); | |
Serial.print("Current air quality: poor @ "); | |
} else if (sgp30_sensor.TVOC < 5500) { | |
matter_air_quality_sensor.set_air_quality(MatterAirQuality::AirQuality_t::VERY_POOR); | |
Serial.print("Current air quality: very poor @ "); | |
} else if (sgp30_sensor.TVOC < 60000) { | |
matter_air_quality_sensor.set_air_quality(MatterAirQuality::AirQuality_t::EXTREMELY_POOR); | |
Serial.print("Current air quality: extremely poor @ "); | |
} else if (sgp30_sensor.TVOC > 60000) { | |
matter_air_quality_sensor.set_air_quality(MatterAirQuality::AirQuality_t::UNKNOWN); | |
Serial.println("Current air quality: unknown"); | |
return; | |
} else { | |
matter_air_quality_sensor.set_air_quality(MatterAirQuality::AirQuality_t::UNDEFINED_VALUE); | |
Serial.println("Current air quality: undefined value"); | |
return; | |
} | |
Serial.print(sgp30_sensor.TVOC); | |
Serial.println("ppb"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment