michwa15yardentd
Published © MIT

Keep Your Home a Safe Place

An indoor air quality monitoring system with dust and air pollution sensors which operates a fan independently

IntermediateProtip392
Keep Your Home a Safe Place

Things used in this project

Story

Read more

Schematics

Demo

Code

Code

C/C++
//#include"AirQuality.h"
#include "thingProperties.h"
#include <Arduino_MKRIoTCarrier.h>
MKRIoTCarrier carrier;

//Colors
uint32_t noColor = carrier.leds.Color( 0, 0, 0); // no color
uint32_t colorRed = carrier.leds.Color(0, 255, 0);   //RED
uint32_t colorGreen = carrier.leds.Color(255, 0, 0);  //GREEN
uint32_t colorWhite = carrier.leds.Color( 255, 255, 255); // WHITE
uint32_t colorYellow = carrier.leds.Color( 255, 255, 0); // yellow
uint32_t colorBlue = carrier.leds.Color( 0, 0, 255); // blue
uint32_t colorPurple = carrier.leds.Color( 255, 0, 255); // purple

//int pir_movement = A4;
int pir_movement_state = 0;

int pir_dust = A6;
int air_quality_sensor = A5 ;


unsigned long duration;
unsigned long start_time;
unsigned long sample_time_ms = 3000;//sample 3s ;
unsigned long low_pulse_occupancy = 0;
float ratio = 0;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
 
  // Defined in thingProperties.h
  initProperties();
 
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  //Get Cloud Info/errors , 0 (only errors) up to 4
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
 
  //Wait to get cloud connection to init the carrier
  while (ArduinoCloud.connected() != 1) {
    ArduinoCloud.update();
    delay(500);
  }
 
  delay(500);
  CARRIER_CASE = false;
  carrier.begin();
  carrier.display.setRotation(0);
  delay(1000);
  pinMode(A5, INPUT);
  pinMode(A6, INPUT);
  //pinMode(A4, INPUT);
  
  start_time = millis();
}

void loop() {
  ArduinoCloud.update();
  carrier.Buttons.update();

  temperature = carrier.Env.readTemperature();
  humidity = carrier.Env.readHumidity();
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(", Humidity: ");
  Serial.println(humidity);
  
  
  duration = pulseIn(pir_dust, LOW);
  low_pulse_occupancy = low_pulse_occupancy + duration;
 
  if ((millis()-start_time) > sample_time_ms)//if the sampel time == 3s
  {
      // operations on the dust sensor
      ratio = low_pulse_occupancy / (sample_time_ms*10.0);  // Integer percentage 0=>100
      concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
      Serial.print("concentration = ");
      Serial.print(concentration);
      Serial.println(" pcs/0.01cf  - ");
      low_pulse_occupancy = 0;
      start_time = millis();
      
      // operations on the air-pollution sensor
      current_quality = analogRead(air_quality_sensor);
      if (current_quality >= 1000){
        Serial.println("Very High Pollution!");
      }
      else if (current_quality >= 700){
        Serial.println("High pollution!");
      }
      else if (current_quality >= 300){
        Serial.println("Low pollution!");
      }
      else{
        Serial.println("Fresh air");
      }
      
      if((concentration > 6000) || (current_quality >= 1000)){
        carrier.display.fillScreen(ST77XX_BLACK); //black background
        carrier.display.setTextColor(ST77XX_BLUE); //blue text
        carrier.display.setTextSize(3); //medium sized text
        //sets position for printing (x and y)
        carrier.display.setCursor(45, 70);
        carrier.display.println("HAZARD!");
        carrier.leds.fill(colorBlue, 0, 5);
        carrier.leds.show();
        
      }
      
      else if((concentration > 4500 && concentration < 10000) || (current_quality <= 1000 && current_quality >= 700)){
        carrier.display.fillScreen(ST77XX_BLACK); //black background
        carrier.display.setTextColor(ST77XX_RED); //red text
        carrier.display.setTextSize(3); //medium sized text
        //sets position for printing (x and y)
        carrier.display.setCursor(50, 70);
        carrier.display.println("HEAVY");
        carrier.leds.fill(colorRed, 0, 5);
        carrier.leds.show();
        
      }
      
      else if ((concentration > 3000 && concentration < 8000) || (current_quality <= 700 && current_quality >= 500)){
        carrier.display.fillScreen(ST77XX_BLACK); //black background
        carrier.display.setTextColor(ST77XX_YELLOW); //yellow text
        carrier.display.setTextSize(3); //medium sized text
        //sets position for printing (x and y)
        carrier.display.setCursor(30, 70);
        carrier.display.println("ACCEPTABLE");
        carrier.leds.fill(colorYellow, 0, 5);
        carrier.leds.show();
        
      }
      
      else if ((concentration > 1500 && concentration < 6000) || (current_quality <= 500 && current_quality >= 300)) {
        carrier.display.fillScreen(ST77XX_BLACK); //black background
        carrier.display.setTextColor(ST77XX_GREEN); //green text
        carrier.display.setTextSize(3); //medium sized text
        //sets position for printing (x and y)
        carrier.display.setCursor(55, 70);
        carrier.display.println("GOOD");
        carrier.leds.fill(colorGreen, 0, 5);
        carrier.leds.show();
      }    
      
      else{
        carrier.display.fillScreen(ST77XX_BLACK); //black background
        carrier.display.setTextColor(ST77XX_WHITE); //white text
        carrier.display.setTextSize(3); //medium sized text
        //sets position for printing (x and y)
        carrier.display.setCursor(55, 70);
        carrier.display.println("CLEAN");
        carrier.leds.fill(colorWhite, 0, 5);
        carrier.leds.show();
      }
      
      if(fan_flag){
        if(concentration > 6000 || current_quality >= 500) carrier.Relay1.open();
        else carrier.Relay1.close(); 
      } 
      
  }
  
  // motion check
  //pir_movement_state = digitalRead(pir_movement); 
   
  //if(pir_movement_state == HIGH) { // if motion detected
  //  onMovementEventChange();
  //}
  
  if(carrier.Button0.onTouchDown()) { // button press
	  fan_flag = !fan_flag;
	  carrier.display.fillScreen(ST77XX_BLACK);
    carrier.display.setTextColor(colorPurple);
    carrier.display.setTextSize(2);
	  if(fan_flag){
      carrier.display.print("fan mode is active");
	  }
	  else{
	     carrier.display.print("fan mode is off");
	  }
  }
}

void onMovementEventChange() {
    carrier.leds.fill(colorPurple, 0, 5);
    carrier.leds.show();  
    carrier.display.fillScreen(ST77XX_BLACK); //black background
    carrier.display.setTextColor(colorPurple); //purple text
    carrier.display.setTextSize(2); //medium sized text
    //sets position for printing (x and y)
    carrier.display.setCursor(20, 40);
    carrier.display.print("Temperature: ");
    carrier.display.println(temperature);
    carrier.display.setCursor(20, 60);
    carrier.display.print("Humidity: ");
    carrier.display.println(humidity);
    carrier.display.setCursor(20, 80);
    carrier.display.print("concentration: "); 
    carrier.display.print(concentration);
    carrier.display.println(" pcs/0.01cf");
    carrier.display.setCursor(20, 100);
    carrier.display.print("Air-Pollute measure: "); 
    carrier.display.print(current_quality);
    delay(3000);
}

void onHumidityChange() {
  
}

void onTemperatureChange() {
  // Do something
}

void onConcentrationChange() {
  // Do something
}

void onCurrentQualityChange() {
  // Do something
}

void onFanFlagChange() {
  // Do something
}

Credits

michwa15
1 project • 1 follower
yardentd
1 project • 2 followers

Comments