From 1df37cb910bbf84ede0ff62395c8aa6a2caf35d2 Mon Sep 17 00:00:00 2001 From: bzi Date: Thu, 21 Feb 2019 18:53:43 +0100 Subject: [PATCH] Implement initial firmware version of envisens --- envisens/atmega_fw/atmega_fw.ino | 62 ++++++++++++++++++++++++++++++ envisens/atmega_fw/configuration.h | 11 ++++++ 2 files changed, 73 insertions(+) create mode 100644 envisens/atmega_fw/atmega_fw.ino create mode 100644 envisens/atmega_fw/configuration.h diff --git a/envisens/atmega_fw/atmega_fw.ino b/envisens/atmega_fw/atmega_fw.ino new file mode 100644 index 0000000..289b35e --- /dev/null +++ b/envisens/atmega_fw/atmega_fw.ino @@ -0,0 +1,62 @@ +/* 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 +#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"); + } +} diff --git a/envisens/atmega_fw/configuration.h b/envisens/atmega_fw/configuration.h new file mode 100644 index 0000000..f7b6159 --- /dev/null +++ b/envisens/atmega_fw/configuration.h @@ -0,0 +1,11 @@ +/* Macrocell environmental sensor project EnviSens + * + * This header provides the necessary configurations for the ATMEGA firmware. + * + * (C) 2019 Macrocell - Environmental sensing solutions + * proudly presented by Macrocell - FPGA Innovators + */ + +#define SERIAL_BAUDRATE 115200 +#define OPERATION_BLINK_EN true // blink continuously if in correct operation +#define OPERATION_PERIOD 2000 // sensor reading period in milliseconds