Minyu
Published

Aerobic Working

Do you realize your feeling sleepy in the office may strongly related to concentration of carbon dioxide? Follow me to explore it!

IntermediateShowcase (no instructions)2 hours85
Aerobic Working

Things used in this project

Story

Read more

Code

co2 detector

C/C++
#include <Arduino.h>

#include "sensirion_common.h"
#include "sgp30.h"
//#include"Free_Fonts.h" 
#include"TFT_eSPI.h"
TFT_eSPI tft;

#define BUZZER_PIN WIO_BUZZER /* sig pin of the buzzer */

int beats[] = { 1, 2, 4, 8, 16, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int tempo = 100;

void setup() {
    s16 err;
    u16 scaled_ethanol_signal, scaled_h2_signal;
    Serial.begin(115200);
    Serial.println("serial start!!");
    
    tft.begin();
    tft.setRotation(1);
    digitalWrite(LCD_BACKLIGHT, HIGH); // turn on the backlight
    
    tft.fillScreen(TFT_DARKGREY);

    tft.setTextSize(2);
    tft.setTextColor(TFT_GREENYELLOW, TFT_DARKGREY);
    tft.drawString("A", 7, 4);
    tft.setTextSize(1);
    tft.setTextColor(TFT_LIGHTGREY, TFT_DARKGREY);
    tft.drawString("ir ", 18, 9);
    tft.setTextSize(2);
    tft.setTextColor(TFT_NAVY, TFT_DARKGREY);
    tft.drawString("M", 32, 4);
    tft.setTextSize(1);
    tft.setTextColor(TFT_LIGHTGREY, TFT_DARKGREY);
    tft.drawString("onitor ", 45, 9);
    //tft.fillRoundRect(90,4,20,15,3,TFT_GREENYELLOW);
    tft.drawRoundRect(3,2,80,20,5,TFT_GREENYELLOW);

    tft.setTextSize(3);
    tft.setTextColor(TFT_GREENYELLOW, TFT_DARKGREY);
    tft.drawString("seeed", 225, 185);
    tft.setTextColor(TFT_DARKCYAN, TFT_DARKGREY);
    tft.drawString("studio", 205, 210);

    /*For wio link!*/
    #if defined(ESP8266)
    pinMode(15, OUTPUT);
    digitalWrite(15, 1);
    Serial.println("Set wio link power!");
    delay(500);
    #endif
    /*  Init module,Reset all baseline,The initialization takes up to around 15 seconds, during which
        all APIs measuring IAQ(Indoor air quality ) output will not change.Default value is 400(ppm) for co2,0(ppb) for tvoc*/
    while (sgp_probe() != STATUS_OK) {
        Serial.println("SGP failed");
        while (1);
    }
    /*Read H2 and Ethanol signal in the way of blocking*/
    err = sgp_measure_signals_blocking_read(&scaled_ethanol_signal,
                                            &scaled_h2_signal);
    if (err == STATUS_OK) {
        Serial.println("get ram signal!");
    } else {
        Serial.println("error reading signals");
    }
    err = sgp_iaq_init();
}

void loop() {
    s16 err = 0;
    u16 tvoc_ppb, co2_eq_ppm;
    err = sgp_measure_iaq_blocking_read(&tvoc_ppb, &co2_eq_ppm);
    int bgColor;
    
    if (err == STATUS_OK) {
        Serial.print("tVOC  Concentration:");
        Serial.print(tvoc_ppb);
        Serial.println("ppb");

        Serial.print("CO2eq Concentration:");
        Serial.print(co2_eq_ppm);
        Serial.println("ppm");

        tft.setTextSize(2);
        if(co2_eq_ppm>=1000) {
            bgColor=TFT_RED;
            tft.fillRect(110,50,110,100,bgColor); //A 100x100 black rectangle starting from (110, 70)
            tft.setTextColor(TFT_WHITE,bgColor);
            tft.drawString(" 1000+  ", 115, 75);
            tft.drawString(" Alarm! ", 115, 115);
            Serial.println("1000 alarm!");            
        }
        else if(co2_eq_ppm>=600) {
            bgColor=TFT_ORANGE;
            tft.fillRect(110,50,110,100,bgColor); //A 100x100 black rectangle starting from (110, 70)
            tft.setTextColor(TFT_WHITE,bgColor);
            tft.drawString("  600+  ", 115, 75);
            tft.drawString("Warning!", 115, 115);
            Serial.println("600 warning!");
        }
        else if(co2_eq_ppm>=450) {
            bgColor=TFT_GREEN;
            tft.fillRect(110,50,110,100,bgColor); //A 100x100 black rectangle starting from (110, 70)
            tft.setTextColor(TFT_WHITE,bgColor);
            tft.drawString("  450+  ", 115, 75);
            tft.drawString(" Notice.", 115, 115);
            Serial.println("450 notice.");
        }
        else  {
            bgColor=TFT_BLUE;
            tft.fillRect(110,50,110,100,bgColor); //A 100x100 black rectangle starting from (110, 70)
            tft.setTextColor(TFT_WHITE,bgColor);
            tft.drawString("  400+  ", 115, 75);
            tft.drawString(" Normal.", 115, 115);
            Serial.println("OK!");
        }
        playNote(min((co2_eq_ppm-300)/150,4), beats[min((co2_eq_ppm-300)/150,4)] * tempo);
        Serial.println(min((co2_eq_ppm-300)/150,4));
        
    } else {
        Serial.println("error reading IAQ values\n");
    }
    
      //tft.setFreeFont(&FreeSansBoldOblique7pt7b); //select Free, Sans, Bold, Oblique, 12pt.
      tft.setTextSize(1);
      tft.setTextColor(TFT_WHITE,TFT_DARKGREY);
      tft.drawString("CO2eq(ppm):",10,212);
      tft.drawString(String(co2_eq_ppm)+"    ", 90, 212);
      tft.drawString("tvoc(ppb):", 16, 190);
      tft.drawString(String(tvoc_ppb)+"  ", 90, 190);
      /*          
                xpos += tft.drawChar('0', xpos, ypos, 8);    // Add hours leading zero for 24 hr clock
            xpos += tft.drawNumber(hh, xpos, ypos, 8);             // Draw hours            
                tft.setTextColor(0x39C4, bgColor);        // Set colour to grey to dim colon
                tft.setTextColor(TFT_WHITE, bgColor);    // Set colour back to yellow
      */
    
    delay(1000);
}

void playTone(int tone, int duration) {
    for (long i = 0; i < duration * 1000L; i += tone * 2) {
        digitalWrite(BUZZER_PIN, HIGH);
        delayMicroseconds(tone);
        digitalWrite(BUZZER_PIN, LOW);
        delayMicroseconds(tone);
    }
}
 
void playNote(int note, int duration) {
    int tones[] = { 2095, 1047, 523, 261, 20, 20, 20, 20 };
 
    // play the tone corresponding to the note name
        if (note > 0) {
            playTone(tones[note-1], duration);
            Serial.println(note);
            Serial.println(tones[note-1]);            
            Serial.println(duration);
        }
}

Credits

Minyu
0 projects • 0 followers
Thanks to Yu Min.

Comments