macrocell_office_iot/envisens/atmega_fw/atmega_fw.ino

73 lines
2.0 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;
float hmid = 0; // humidity
float temp = 0; // temperature
float heat = 0; // heat index
float airq = 0; // air quality / gas
void setup() {
Serial.begin(SERIAL_BAUDRATE);
pinMode(MQ135_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
dht.begin();
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void requestEvent() {
Wire.write("he1234");
}
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);}
hmid = dht.readHumidity();
temp = dht.readTemperature();
if (isnan(hmid) || isnan(temp)) {
Serial.println(F("Failed to read from DHT sensor!"));
} else {
heat = dht.computeHeatIndex(temp, hmid, false);
Serial.print(F("Humidity: "));
Serial.print(hmid);
Serial.print(F("% Temperature: "));
Serial.print(temp);
Serial.print(F("°C Heat index: "));
Serial.print(heat);
Serial.print(F("°C "));
}
airq = analogRead(MQ135_PIN);
Serial.print("AirQuality: ");
Serial.print(airq);
Serial.println(" PPM");
}
}