// Licence: WTFPLv2
// Copyright 2015: Julien Moutinho
#include
#include
#include "AirQuality.h"
#include "Arduino.h"
#include "DHT.h"
#define DHTPIN 2
// What pin the DHT is connected to.
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
AirQuality airqualitysensor;
int current_quality = -1;
#define BUZZER 10
int dust_pin = 4;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
unsigned long counter;
float ratio = 0;
float concentration = 0;
void setup() {
Serial.begin(9600);
//while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
// }
Wire.begin(); // Initialize I2C
SeeedGrayOled.init(); // Initialize SEEED OLED display
SeeedGrayOled.clearDisplay(); // Clear the screen and set start position to top left corner
SeeedGrayOled.setNormalDisplay(); // Set display to normal mode (i.e non-inverse mode)
SeeedGrayOled.setVerticalMode();
//SeeedGrayOled.setPageMode(); // Set addressing mode to Page Mode
SeeedGrayOled.setTextXY(0,0); // Set the cursor to Xth Page, Yth Column
SeeedGrayOled.putString("Initializing"); // Print the String
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
airqualitysensor.init(14);
dht.begin();
pinMode(dust_pin,INPUT);
starttime = millis(); // Get the current time
Serial.println("counter;uptime;humidity;temperature;quality;particules");
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
counter = counter + 1;
SeeedGrayOled.setTextXY(8,0);
// Check if returns are valid,
// if they are NaN (not a number)
// then something went wrong!
Serial.print(counter);
Serial.write(';');
Serial.print(millis());
if (isnan(t) || isnan(h)) {
SeeedGrayOled.putString("DHT ERROR");
Serial.write(';');
Serial.write(';');
}
else {
SeeedGrayOled.putString("Humidity:");
SeeedGrayOled.setTextXY(9,0);
SeeedGrayOled.putString(" ");
SeeedGrayOled.putFloat(h);
SeeedGrayOled.putString("%");
SeeedGrayOled.setTextXY(10,0);
SeeedGrayOled.putString("Temp.:");
SeeedGrayOled.setTextXY(11,0);
SeeedGrayOled.putString(" ");
SeeedGrayOled.putFloat(t);
SeeedGrayOled.putString("*C");
Serial.write(';');
Serial.print(h);
Serial.write(';');
Serial.print(t);
}
Serial.write(';');
Serial.print(airqualitysensor.first_vol,DEC);
// Checking Air Quality
current_quality=airqualitysensor.slope();
if (current_quality >= 0) {
if (current_quality <= 1)
// Air quality is bad.
// Let's revert display to make some light!
SeeedGrayOled.setInverseDisplay();
else
SeeedGrayOled.setNormalDisplay();
SeeedGrayOled.setTextXY(2,0);
SeeedGrayOled.putString("Air Quality:"); // Print the String
SeeedGrayOled.setTextXY(3,3);
SeeedGrayOled.putNumber(airqualitysensor.first_vol); // Print the String
SeeedGrayOled.putString(" "); // Clear any old character
SeeedGrayOled.setTextXY(0,0);
if (current_quality==0)
SeeedGrayOled.putString("Emergency ");
else if (current_quality==1)
SeeedGrayOled.putString("Hi Pollution");
else if (current_quality==2)
SeeedGrayOled.putString("Low Polution");
else if (current_quality==3)
SeeedGrayOled.putString("Air OK :-) ");
if (current_quality<1)
digitalWrite(BUZZER, HIGH);
else
digitalWrite(BUZZER, LOW);
}
// Checking Dust Sensor
duration = pulseIn(dust_pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
Serial.write(';');
if ((millis()-starttime) > sampletime_ms) {
//if the sample time == 30s
ratio = lowpulseoccupancy / (sampletime_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(lowpulseoccupancy);
Serial.write(';');
Serial.print(ratio);
*/
Serial.print(concentration);
SeeedGrayOled.setTextXY(4,0);
SeeedGrayOled.putString("Particles:");
SeeedGrayOled.setTextXY(5,3);
SeeedGrayOled.putString(" ");
SeeedGrayOled.setTextXY(5,3);
SeeedGrayOled.putNumber(concentration);
lowpulseoccupancy = 0;
starttime = millis();
}
Serial.println();
}
ISR(TIMER2_OVF_vect) {
if(airqualitysensor.counter==122) {
// set 2 seconds as a detected duty
airqualitysensor.last_vol=airqualitysensor.first_vol;
airqualitysensor.first_vol=analogRead(A0);
airqualitysensor.counter=0;
airqualitysensor.timer_index=1;
//PORTB=PORTB^0x20;
}
else {
airqualitysensor.counter++;
}
}