Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
zhui zhui
Published © TAPR-OHL

Sleep Environment Monitor

A sleep environment detector can be designed to give people feedback whether the current environment is suitable for sleeping

BeginnerFull instructions provided8 hours436
Sleep Environment Monitor

Things used in this project

Hardware components

Seeed Studio SGP30
×1
Seeed Studio DHT11
×1
Wio Terminal
Seeed Studio Wio Terminal
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Sleep Environment Monitor

Arduino
Code Shows, you can reference but no copy please.
#include <Arduino.h>
#include "sensirion_common.h"
#include "sgp30.h"
#include "DHT.h"
#include"TFT_eSPI.h"
TFT_eSPI tft;
TFT_eSprite spr = TFT_eSprite(&tft);
#define DHTTYPE DHT11   
#define DHTPIN A0     
DHT dht(DHTPIN, DHTTYPE);
void setup() 
{                              
    tft.begin();            
    tft.setRotation(3);     
    digitalWrite(LCD_BACKLIGHT, HIGH); 
    Wire.begin();   
    dht.begin();    
    pinMode(WIO_MIC, INPUT); 
    Serial.begin(115200);  
    Serial.println("serial start!!"); 
    while (sgp_probe() != STATUS_OK) 
    { 
     Serial.println("SGP failed");
    while (1);
    }
    sgp_set_absolute_humidity(13000);
    sgp_iaq_init();
}
void loop() 
{                                   
    float temp_hum_val[2] = {0};  
    u16 tvoc_ppb, co2_eq_ppm;  
    s16 err = 0;
    err = sgp_measure_iaq_blocking_read(&tvoc_ppb, &co2_eq_ppm);
    if (err == STATUS_OK) 
    {
      Serial.print("tVOC  Concentration:");
      Serial.println("ppb");
      Serial.print("CO2eq Concentration:");
      Serial.print(co2_eq_ppm);
      Serial.println("ppm");
    } else {
      Serial.println("error reading IAQ values\n");
    }
    if (!dht.readTempAndHumidity(temp_hum_val)) 
    {
      Serial.print("Humidity: ");
      Serial.print(temp_hum_val[0]);    
      Serial.print(" %\t");
      Serial.print("Temperature: ");
      Serial.print(temp_hum_val[1]);    
      Serial.println(" *C");
    } else 
    {
      Serial.println("Failed to get temprature and humidity value.");
    }
    int val = analogRead(WIO_MIC);
    Serial.print("Sound: ");
    Serial.println(val);
    if ((temp_hum_val[1] > 24&&temp_hum_val[1]<28)&&(temp_hum_val[0]>40&&temp_hum_val[0]<70)&&(co2_eq_ppm<700)&&(val<500)) 
    {
      tft.fillScreen(0x0);
      tft.fillRect(0, 0, 320, 50, TFT_BLUE);
      tft.setTextSize(2);
      tft.setTextColor(TFT_GREEN); 
      tft.drawString((String)"(-^O^-) suit for sleep", 30, 20);  
     }
     else 
     {
      tft.fillScreen(0x0);
      tft.fillRect(0, 0, 320, 50, TFT_BLUE);
      tft.setTextSize(2);
      tft.setTextColor(TFT_RED);
      tft.drawString((String)"(-^-) Unsuitable", 60, 20);
     }
     tft.setTextSize(2);
     tft.setTextColor(TFT_WHITE);
     tft.drawString("Sound: ", 30, 70);
     tft.drawNumber(val, 140, 60, 2);
     tft.setTextSize(2);
     tft.setTextColor(TFT_WHITE);
     tft.drawString("Temp: ", 30, 110);
     tft.drawNumber(temp_hum_val[1], 140, 100, 2);
     tft.drawString("Humi: ", 30, 150); 
     tft.drawNumber(temp_hum_val[0], 140, 140, 2);
     tft.drawString("CO2: ", 30, 190); 
     tft.drawNumber(co2_eq_ppm, 140, 180, 2);
     spr.pushSprite(0, 0);
     spr.deleteSprite();
     delay(1500); 
}

Credits

zhui zhui
1 project • 1 follower

Comments