104 lines
3.0 KiB
C++
104 lines
3.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
|
|
*
|
|
* Connect the ESP8266 D1 to ATMEGA A5 and D2 to A4 for I2C communcation.
|
|
*
|
|
* (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
|
|
|
|
#define NUM_SENSORS 4
|
|
float sensor_values[NUM_SENSORS];
|
|
char sensor_names[NUM_SENSORS][5] = {"hmid", "temp", "heat", "airq"};
|
|
int current_value_index = 0;
|
|
|
|
volatile byte* float_ptr;
|
|
|
|
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() {
|
|
byte i2c_data[8];
|
|
float_ptr = (byte*) &sensor_values[current_value_index];
|
|
i2c_data[0] = byte(sensor_names[current_value_index][0]);
|
|
i2c_data[1] = byte(sensor_names[current_value_index][1]);
|
|
i2c_data[2] = byte(sensor_names[current_value_index][2]);
|
|
i2c_data[3] = byte(sensor_names[current_value_index][3]);
|
|
i2c_data[4] = float_ptr[0];
|
|
i2c_data[5] = float_ptr[1];
|
|
i2c_data[6] = float_ptr[2];
|
|
i2c_data[7] = float_ptr[3];
|
|
|
|
Wire.write(i2c_data, 8);
|
|
if (current_value_index < NUM_SENSORS - 1) {
|
|
current_value_index ++;
|
|
} else {
|
|
current_value_index = 0;
|
|
}
|
|
}
|
|
|
|
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");
|
|
|
|
// set the sensor values for global access
|
|
sensor_values[0] = hmid;
|
|
sensor_values[1] = temp;
|
|
sensor_values[2] = heat;
|
|
sensor_values[3] = airq;
|
|
}
|
|
}
|