63 lines
1.7 KiB
C++
63 lines
1.7 KiB
C++
/* Macrocell environmental sensor project EnviSens
|
|
*
|
|
* This firmware provides features to use an ATMEGA µC as an environmental sensor provider.
|
|
* Currently supported sensors:
|
|
* - digital pin D2: DHT22 temperature and humidity sensor
|
|
* - analog pin A0: MQ135 gas and air quality sensor
|
|
*
|
|
* (C) 2019 Macrocell - Environmental Sensing Solutions (MESS)
|
|
* proudly presented by Macrocell - FPGA Innovators
|
|
*/
|
|
|
|
#include <Wire.h>
|
|
#include "DHT.h"
|
|
#include "configuration.h"
|
|
|
|
#define MQ135_PIN A0
|
|
#define DHT_PIN 2 // Digital pin connected to the DHT sensor
|
|
#define DHT_TYPE DHT22 // DHT 22 (AM2302), AM2321
|
|
DHT dht(DHT_PIN, DHT_TYPE);
|
|
|
|
int sensorValue;
|
|
long last_sample = 0;
|
|
|
|
void setup() {
|
|
Serial.begin(SERIAL_BAUDRATE);
|
|
pinMode(MQ135_PIN, INPUT);
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
dht.begin();
|
|
}
|
|
|
|
void loop() {
|
|
long now = millis();
|
|
if (now - last_sample > OPERATION_PERIOD / 2) {
|
|
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, LOW);}
|
|
}
|
|
if (now - last_sample > OPERATION_PERIOD) {
|
|
last_sample = now;
|
|
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);}
|
|
|
|
float h = dht.readHumidity();
|
|
float t = dht.readTemperature();
|
|
|
|
if (isnan(h) || isnan(t)) {
|
|
Serial.println(F("Failed to read from DHT sensor!"));
|
|
} else {
|
|
float hi = dht.computeHeatIndex(t, h, false);
|
|
Serial.print(F("Humidity: "));
|
|
Serial.print(h);
|
|
Serial.print(F("% Temperature: "));
|
|
Serial.print(t);
|
|
Serial.print(F("°C Heat index: "));
|
|
Serial.print(hi);
|
|
Serial.println(F("°C "));
|
|
}
|
|
|
|
sensorValue = analogRead(MQ135_PIN);
|
|
Serial.print("AirQuality=");
|
|
Serial.print(sensorValue, DEC);
|
|
Serial.println(" PPM");
|
|
}
|
|
}
|