#include <Arduino.h> //wg.print
//#include <DFRobot_DHT20.h>
#include <DHT20.h>
#include <SPI.h>
#include <MQUnifiedsensor.h>
#include <EEPROM.h>
#include "time.h"
#include <string>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
struct tm timeinfo; //für time.h
#include "RTClib.h"
RTC_DS3231 rtc;
#include "FS.h"
#include <SD.h>
const int chipSelect = 33; //Modifikation für Himalaya: CS = GPIO33
int WriteCounter = 100;
DFRobot_DHT20 dht20;
//Definitions for MQ-7
/************************Hardware Related Macros************************************/
#define Board ("ESP-32") // Wemos ESP-32 or other board, whatever have ESP32 core.
#define Pin (35) //IO35 for your ESP32 WeMos Board, pinout here: https://i.pinimg.com/originals/66/9a/61/669a618d9435c702f4b67e12c40a11b8.jpg
/***********************Software Related Macros************************************/
#define Type ("MQ-7") //MQ3 or other MQ Sensor, if change this verify your a and b values.
#define Voltage_Resolution (3.3) // 3V3 <- IMPORTANT. Source: https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/
#define ADC_Bit_Resolution (12) // ESP-32 bit resolution. Source: https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/
#define RatioMQ7CleanAir (27.5) //RS / R0 = 27.5 ppm
/*****************************Globals***********************************************/
MQUnifiedsensor MQ7(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
int RedledPin = 4;
int GreenledPin = 2;
float SaveR0 = 0;
void writeFile(fs::FS &fs, const char * path, const char * message)
{
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file)
{
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message))
{
Serial.println("File written");
}
else
{
Serial.println("Write failed");
}
file.close();
}
void appendFile(fs::FS &fs, const char * path, const char * message)
{
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file)
{
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message))
{
Serial.println("Message appended");
}
else
{
Serial.println("Append failed");
}
file.close();
}
void deleteFile(fs::FS &fs, const char * path)
{
Serial.printf("Deleting file: %s\n", path);
if(fs.remove(path))
{
Serial.println("File deleted");
}
else
{
Serial.println("Delete failed");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.println(Sketchname);
pinMode(4, OUTPUT);
pinMode(2, OUTPUT);
tft.init();
tft.setRotation(0); // Adjust Rotation of your screen (0-3)
//tft.fillScreen(TFT_BLACK);
tft.fillScreen(TFT_WHITE);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
// don't do anything more:
//while (1);
}
Serial.println("card initialized.");
delay(500);
//new file for every start
deleteFile(SD, "/CODaten.txt");
writeFile(SD, "/CODaten.txt", "CO Messung\n");
dht20.begin();
//Serial.println("dht");
//Set math model to calculate the PPM concentration and the value of constants
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
MQ7.setA(99.042); MQ7.setB(-1.518); // Configure the equation to calculate CO concentration value
/*
Exponential regression:
GAS | a | b
H2 | 69.014 | -1.374
LPG | 700000000 | -7.703
CH4 | 60000000000000 | -10.54
CO | 99.042 | -1.518
Alcohol | 40000000000000000 | -12.35
*/
MQ7.init();
Serial.println("MQ7init");
/*
//If the RL value is different from 10K please assign your RL value with the following method:
MQ7.setRL(10);
*/
if (!rtc.begin())
{
Serial.println("Couldn't find RTC");
Serial.flush();
//while (1);
}
Serial.println("RTC begin");
rtc.adjust(DateTime(__DATE__, __TIME__));
float calcR0 = 0;
Serial.println("Calibrating please wait.");
for(int i = 1; i<=10; i ++)
{
MQ7.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR0 += MQ7.calibrate(RatioMQ7CleanAir);
Serial.print(".");
}
MQ7.setR0(calcR0/10);
SaveR0 = (calcR0/10);
MQ7.serialDebug(true);
Serial.println(" done!.");
Serial.print("calcR0: ");
Serial.println(calcR0);
delay(500);
/*************** End calibration *******************/
if(isinf(calcR0)) {Serial.println("Warning: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
//MQ Setup and calibration finished
//Test LEDs
digitalWrite(GreenledPin, HIGH);
delay(300);
digitalWrite(GreenledPin, LOW);
digitalWrite(RedledPin, HIGH);
delay(300);
digitalWrite(RedledPin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
float t = dht20.getTemperature();
float h = dht20.getHumidity() * 100;
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) )
{
Serial.println(F("Fail read DHT!"));
return;
}
//RTC
DateTime dt = rtc.now();
// MQ Reading and updating section
MQ7.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ7.readSensor();
float CO = MQ7.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
float PPM = MQ7.getPPM();
Serial.print("CO: ");
Serial.println(CO);
Serial.print("PPM: ");
Serial.println(PPM);
if(CO>35)
{
tft.setTextColor(TFT_RED, TFT_BLACK);
}
else
{
//tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextColor(TFT_BLACK, TFT_WHITE);
}
// x, y
tft.setCursor(50, 50, 4);
tft.println("CO: ");
tft.setCursor(130, 50, 4);
tft.println(CO);
tft.setCursor(50, 80, 4);
tft.println("Temp:");
tft.setCursor(130, 80, 4);
tft.println(t);
tft.setCursor(50, 110, 4);
tft.println("Hum:");
tft.setCursor(130, 110, 4);
tft.println(h);
tft.setCursor(50, 140, 4);
tft.println("PPM:");
tft.setCursor(130, 140, 4);
tft.println(PPM);
delay(500);
if (CO > 35.0)
{
digitalWrite(GreenledPin, LOW);
digitalWrite(RedledPin, HIGH);
}
else
{
digitalWrite(GreenledPin, HIGH);
digitalWrite(RedledPin, LOW);
}
if (WriteCounter > 10)
{
WriteCounter = 0;
const char * datastring = " ";
String Zeit = " ";
Zeit = Zeit + dt.year() + "." + dt.month() + "." + dt.day() + " " + dt.hour() + ":" + dt.minute() + ":" + dt.second() + ", ";
Zeit = Zeit + " R0: " + SaveR0 + " Temp.: " + t + " CO: " + CO + "\n";
// make a string for assembling the data to log:
datastring = Zeit.c_str();
appendFile(SD, "/CODaten.txt", datastring);
}
WriteCounter++;
delay(500);
}